Package io.inversion.jdbc
Class JdbcUtils
java.lang.Object
io.inversion.jdbc.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 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 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 intselectInt(Connection conn, String sql, Object... vals)static io.inversion.utils.Rows.RowselectRow(Connection conn, String sql, Object... vals)static io.inversion.utils.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)static List<io.inversion.utils.Rows.Row>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
-
selectRows
public static io.inversion.utils.Rows selectRows(Connection conn, String sql, Object... vals) throws SQLException- Throws:
SQLException
-
selectRow
public static io.inversion.utils.Rows.Row selectRow(Connection conn, String sql, Object... vals) throws SQLException- Throws:
SQLException
-
selectInt
- 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<io.inversion.utils.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
-