Skip to content

Instantly share code, notes, and snippets.

@jrichardsz
Last active December 11, 2021 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrichardsz/b354e2b2501f05846dd330acf4aef47b to your computer and use it in GitHub Desktop.
Save jrichardsz/b354e2b2501f05846dd330acf4aef47b to your computer and use it in GitHub Desktop.
java database snippets
public ArrayList<?> executeSimpleScriptString1(String engine, String host, int port, String sid,
String user, String password, String sqlString) throws Exception {
Connection c = getConnection(engine, host, port, sid, user, password);
try {
/*
* CallableStatement cs = c.prepareCall(sqlString); cs.execute();
*/
Statement stmt = c.createStatement();
stmt.execute(sqlString);
// ResultSet rs = cs.getResultSet();
// if (rs == null) {
// return new ArrayList<Object>();
// }
ArrayList<?> result = new ArrayList<Object>();
// ArrayList<?> result = results2Array(rs);
stmt.close();
c.close();
return result;
} catch (Exception e) {
throw new Exception("Failed to execute sql string: " + sqlString, e);
}
}
private ArrayList<ArrayList<Object>> results2Array(ResultSet rs) throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<ArrayList<Object>> al = new ArrayList<ArrayList<Object>>();
while (rs.next()) {
ArrayList<Object> record = new ArrayList<Object>();
for (int i = 1; i <= columns; i++) {
Object value = rs.getObject(i);
record.add(value);
}
al.add(record);
}
return al;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment