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

PreparedStatementcreateInQueryStatement(String query, long[] ids, Connection conn)
Creates new PreparedStatement and fill it up with the ids.
String actualQuery = query + createQuestionMarks(ids.length);
PreparedStatement pstmt = conn.prepareStatement(actualQuery);
for (int i = 0; i < ids.length; i++) {
    pstmt.setLong(i + 1, ids[i]);
return pstmt;
ResultSetexecuteCsvQuery(Connection csv, String csvTableName)
Executes a query on provided CSV connection.
ResultSet expected = csv.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)
        .executeQuery("SELECT * FROM " + csvTableName);
expected.beforeFirst();
return expected;
voidexecuteDDL(String query, Connection connection)
execute DDL
try (Statement st = connection.createStatement()) {
    st.executeUpdate(query);
ResultSetexecuteForResult(Connection conn, String query, String... args)
execute For Result
if (args == null || args.length == 0)
    return conn.createStatement().executeQuery(query);
else {
    PreparedStatement stmt = createPreparedStatement(conn, query, args);
    return stmt.executeQuery();
intExecuteNoneQuerys(String cmdtext, Object[] parms)
Execute None Querys
PreparedStatement pstmt = null;
Connection conn = null;
try {
    conn = getConnections();
    pstmt = conn.prepareStatement(cmdtext);
    prepareCommand(pstmt, parms);
    return pstmt.executeUpdate();
} catch (Exception ex) {
...
ResultSetexecuteQuery(Connection c, String query)
executeQuery
ResultSet rs = null;
Statement st = null;
try {
    st = c.createStatement();
    rs = st.executeQuery(query);
} catch (SQLException ee) {
    if (st != null) {
        st.close();
...
ResultSetExecuteQuery(Connection conn, PreparedStatement preparedStatement, PreparedStatement lockStatement, String tablename)
Execute Query
String tablename_array[] = { tablename };
return ExecuteQuery(conn, preparedStatement, lockStatement, tablename_array);
voidexecuteQuery(Connection connection)
execute Query
String sql01 = "INSERT INTO Persons (P_Id,LastName,FirstName,Address,City) VALUES (1,'Hansen','Ola','Timoteivn,10','Sandnes')";
String sql02 = "INSERT INTO Persons (P_Id,LastName,FirstName,Address,City) VALUES (2,'Svendson','Tove','Borgvn,23','Sandnes')";
String sql03 = "INSERT INTO Persons (P_Id,LastName,FirstName,Address,City) VALUES (3,'Pettersen','Kari','Storgt,20','Stavanger')";
String sql04 = "INSERT INTO Orders (O_Id,OrderNo,P_Id) VALUES (1,77895,3)";
String sql05 = "INSERT INTO Orders (O_Id,OrderNo,P_Id) VALUES (2,44678,3)";
String sql06 = "INSERT INTO Orders (O_Id,OrderNo,P_Id) VALUES (3,22456,2)";
String sql07 = "INSERT INTO Orders (O_Id,OrderNo,P_Id) VALUES (4,24562,1)";
Statement statement = null;
...
voidexecuteQuery(Connection connection, String query)
execute Query
Statement statement = connection.createStatement();
int i = statement.executeUpdate(query);
if (i == -1) {
    System.err.println("database error : " + query);
statement.close();
StatementexecuteQuery(Connection dbConnection, String selectString)
Executes a select query.
Statement statement = dbConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
statement.executeQuery(selectString);
return statement;