Package io.inversion.utils
Class JdbcUtils
java.lang.Object
io.inversion.utils.JdbcUtils
A collection of super helpful JDBC utility methods with SQL injection attack defense built in.
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddSqlListener(JdbcUtils.SqlListener listener) static StringbuildInsertSQL(Connection conn, String tableName, Object[] columnNameArray) static StringbuildUpdateSQL(Connection conn, String tableName, Object[] setColumnNameArray, Object[] whereColumnNames) static Stringstatic voidstatic charcolQuote(Connection conn) static <T> Tstatic Objectexecute(Connection conn, String sql, Object... vals) static StringgetColumnStr(Connection conn, Object[] columnNameArray) static StringgetColumnStr(Connection conn, List columnNameArray) static StringgetDbType(Connection conn) static StringgetQuestionMarkStr(int numQMarks) static StringgetQuestionMarkStr(Object[] columnNameArray) static StringgetWhereColumnStr(Connection conn, Object[] columnNameArray, String sep) static ObjectinsertMap(Connection conn, String tableName, Map row) static ListinsertMaps(Connection conn, String tableName, List maps) static booleanstatic booleanstatic booleanstatic voidstatic voidnotifyBefore(String method, String sql, Object args) static voidnotifyError(String method, String sql, Object args, Exception ex) static Objectstatic StringquoteCol(Connection conn, Object str) readSql(InputStream ddlStream) Breaks the input stream up into a list of sql statements where statements are terminated by ";".static voidremoveSqlListener(JdbcUtils.SqlListener listener) static voidrunSql(Connection conn, InputStream ddlStream) static voidrunSql(Connection conn, String sqlString) static voidrunSql(Connection con, String[] sql) static voidrunSql(Connection con, List<String> sql) static longselectLong(Connection conn, String sql, Object... vals) static <T> TselectObject(Connection conn, String sql, Class<T> clazz, Object... vals) static <T> TselectObject(Connection conn, String sql, T o, Object... vals) static ListselectObjects(Connection conn, String sql, Class type, Object... vals) static Rows.RowselectRow(Connection conn, String sql, Object... vals) static RowsselectRows(Connection conn, String sql, Object... vals) static ObjectselectValue(Connection conn, String sql, Object... vals) update(Connection conn, String tableName, List<String> primaryKeyCols, List<Map<String, Object>> rows) updateBatch(Connection conn, String tableName, List<String> keyCols, List<Map<String, Object>> rows) upsert(Connection conn, String tableName, List<String> primaryKeyCols, List<Map<String, Object>> rows) Batchesrowsinto groups containing identical keys and then inserts rows that are missing indexCols key values or attempts an upsert for rows that have the key values...the row could have the key but still not exist in the db in cases where the key is not an autoincrement number.
-
Constructor Details
-
JdbcUtils
public JdbcUtils()
-
-
Method Details
-
getDbType
-
colQuote
-
quoteCol
-
addSqlListener
-
removeSqlListener
-
notifyBefore
-
notifyError
-
notifyAfter
-
execute
- Throws:
SQLException
-
isSelect
-
selectObject
public static <T> T selectObject(Connection conn, String sql, Class<T> clazz, Object... vals) throws Exception - Throws:
Exception
-
selectObject
- Throws:
Exception
-
selectObjects
public static List selectObjects(Connection conn, String sql, Class type, Object... vals) throws Exception - Throws:
Exception
-
poplulate
-
getFields
-
convert
-
selectRows
- Throws:
SQLException
-
selectRow
- Throws:
SQLException
-
selectLong
- Throws:
SQLException
-
selectValue
- Throws:
SQLException
-
isInsert
-
buildInsertSQL
-
insertMap
- Throws:
SQLException
-
insertMaps
- Throws:
SQLException
-
isUpdate
-
update
public static List<Integer> update(Connection conn, String tableName, List<String> primaryKeyCols, List<Map<String, Object>> rows) throws Exception- Throws:
Exception
-
updateBatch
public static List<Integer> updateBatch(Connection conn, String tableName, List<String> keyCols, List<Map<String, Object>> rows) throws SQLException- Throws:
SQLException
-
buildUpdateSQL
public static String buildUpdateSQL(Connection conn, String tableName, Object[] setColumnNameArray, Object[] whereColumnNames) -
upsert
public static List<Rows.Row> upsert(Connection conn, String tableName, List<String> primaryKeyCols, List<Map<String, Object>> rows) throws SQLExceptionBatchesrowsinto groups containing identical keys and then inserts rows that are missing indexCols key values or attempts an upsert for rows that have the key values...the row could have the key but still not exist in the db in cases where the key is not an autoincrement number.- Parameters:
conn- the jdbc connectiontableName- the table to upsert intoprimaryKeyCols- a unique key for the table that will constrain the upsertrows- the data to upsert- Returns:
- the primaryKeyCols values for all rows upserted
- Throws:
SQLException- when the upsert fails
-
runSql
- Throws:
SQLException
-
runSql
- Throws:
SQLException
-
readSql
- Throws:
SQLException
-
readSql
Breaks the input stream up into a list of sql statements where statements are terminated by ";". Lines starting with "--" or "#" are considred comments are are ignored.- Parameters:
ddlStream- a stream of ddl text- Returns:
- the text broken into individual sql statements
- Throws:
SQLException- when io fails
-
runSql
- Throws:
SQLException
-
runSql
- Throws:
SQLException
-
getWhereColumnStr
-
getColumnStr
-
getColumnStr
-
getQuestionMarkStr
-
getQuestionMarkStr
-
check
-
close
-