Java Utililty Methods SQL Query

List of utility methods to do SQL Query

Description

The list of methods to do SQL Query are organized into topic(s).

Method

StringgetSqlQuery(Statement statement, Object[] args)
get Sql Query
return statement instanceof PreparedStatement ? statement.toString() : Arrays.toString(args); 
PreparedStatementgetStatement(Connection con, String query, Object... bits)
get Statement
PreparedStatement stmt = con.prepareStatement(query);
int i = 1;
if (bits != null) {
    for (Object bit : bits)
        stmt.setObject(i++, bit);
return stmt;
ResultSetquery(Connection conn, String sql)
query the specified SQL in database.
PreparedStatement preparedStatement = conn.prepareStatement(sql);
return preparedStatement.executeQuery();
Tquery(Connection conn, String sql, Object[] params, Class beanClass)
query
PreparedStatement ps = null;
try {
    ps = conn.prepareStatement(sql);
    for (int i = 0; i < params.length; i++) {
        ps.setObject(i + 1, params[i]);
    ResultSet rs = ps.executeQuery();
    Field[] fields = beanClass.getDeclaredFields();
...
List>query(Connection connection, String sql, boolean isClose, Object... params)
query
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
PreparedStatement prepareStatement = null;
try {
    sql = sql.replace("&lt;", "<");
    sql = sql.replace("&gt;", ">");
    sql = sql.replace("&amp;", "&");
    sql = sql.replace("&quot;", "\"");
    sql = sql.replace("&apos;", "'");
...
ListqueryForList(Connection conn, String sql, int limit)
query For List
PreparedStatement ps = conn.prepareStatement(sql.trim());
ps.setMaxRows(limit);
ps.setFetchDirection(ResultSet.FETCH_FORWARD);
ResultSet rs = ps.executeQuery();
return toListMap(limit, rs);
ListqueryObjectList(Connection con, String sql, Class objClass)
query Object List
List<T> lists = new ArrayList<T>();
Statement stmt = null;
ResultSet rs = null;
try {
    stmt = con.createStatement();
    rs = stmt.executeQuery(sql);
    label: while (null != rs && rs.next()) {
        Constructor<?>[] constor = objClass.getConstructors();
...
intqueryProjectTicketIdFromTicketId(Connection db, int ticketId)
query Project Ticket Id From Ticket Id
int projectTicketId = -1;
PreparedStatement pst = db.prepareStatement("SELECT key_count " + "FROM ticket " + "WHERE ticket_id = ? ");
pst.setInt(1, ticketId);
ResultSet rs = pst.executeQuery();
if (rs.next()) {
    projectTicketId = rs.getInt("key_count");
rs.close();
...
ResultSetqueryReturnRS(String sql)
query Return RS
if (null == conn) {
    return null;
try {
    preStatement = conn.prepareStatement(sql);
    rs = preStatement.executeQuery();
} catch (SQLException e) {
    e.printStackTrace();
...
intrunQuery(Connection con, String query)
to run an update query such as update, delete
PreparedStatement st = con.prepareStatement(query);
return st.executeUpdate();