Last active
December 11, 2021 00:42
-
-
Save jrichardsz/b354e2b2501f05846dd330acf4aef47b to your computer and use it in GitHub Desktop.
java database snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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