Package net.tirasa.connid.commons.db
Class SQLUtil
java.lang.Object
net.tirasa.connid.commons.db.SQLUtil
The SQL helper/util class.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
attribute2jdbcValue
(Object value, int sqlType) Convert the attribute to expected jdbc type using java conversions Some database strategy sets all attributes as string, other convert them first and than set as native.static byte[]
blob2ByteArray
(Blob blobValue) Make a blob conversion.static void
closeQuietly
(Connection conn) Ignores any exception thrown by theConnection
parameter when closed, it also checks fornull
.static void
closeQuietly
(ResultSet rset) static void
closeQuietly
(Statement stmt) Ignores any exception thrown by theStatement.close()
method.static void
Ignores any exception thrown by theConnection
parameter when closed, it also checks fornull
.static String
date2String
(Date value) Date to string.static int
executeUpdateStatement
(Connection conn, String sql, SQLParam... params) Executes DML sql statement.getColumnValues
(ResultSet resultSet) Read one row from database result set and convert a columns to attribute set.static Connection
getDatasourceConnection
(String datasourceName) Get the connection from the datasource.static Connection
getDatasourceConnection
(String datasourceName, String user, GuardedString password) Get the connection from the dataSource with specified user and password.static Connection
getDatasourceConnection
(String datasourceName, String user, GuardedString password, Properties env) Get the connection from the dataSource with specified user and password.static Connection
getDatasourceConnection
(String datasourceName, Properties env) Get the connection from the datasource.static Connection
getDriverMangerConnection
(String driver, String url, String login, GuardedString password) Gets aConnection
using the basic driver manager.static Class
<?> getSQLAttributeType
(int sqlType) Convert database type to connector supported set of attribute types Can be redefined for different databases.static SQLParam
getSQLParam
(ResultSet resultSet, int i, String name, int sqlType) Retrieve the SQL value from result set.static Object
jdbc2AttributeValue
(Object value) The conversion to required attribute type.static String
The null param vlaue normalizator.static void
rollbackQuietly
(Connection conn) Ignores any exception thrown by theConnection
parameter when closed, it also checks fornull
.static void
Ignores any exception thrown by theDatabaseConnection
parameter when closed, it also checks fornull
.selectRows
(Connection conn, String sql, SQLParam... params) Selects all rows from select.static Object
selectSingleValue
(Connection conn, String sql, SQLParam... params) Selects single value (first column) from select.static void
setGuardedStringParam
(PreparedStatement stmt, int idx, GuardedString guard) The helper guardedString bind method.static void
setParams
(CallableStatement statement, List<SQLParam> params) Binds the "?"static void
setParams
(PreparedStatement statement, List<SQLParam> params) Binds the "?"static void
setSQLParam
(PreparedStatement stmt, int idx, SQLParam parm) Set a parameter to statement.static Boolean
string2Boolean
(String val) Convert String to boolean.static Date
string2Date
(String param) String to Date.static Time
string2Time
(String param) String to Time.static Timestamp
string2Timestamp
(String param) Convert string to Timestampstatic String
time2String
(Time value) Time to String format.static String
timestamp2String
(Timestamp value) Convert timestamp to string.
-
Method Details
-
getDatasourceConnection
Get the connection from the datasource.- Parameters:
datasourceName
- datasource JNDI nameenv
- properties- Returns:
- the connection get from default jndi context
-
getDatasourceConnection
public static Connection getDatasourceConnection(String datasourceName, String user, GuardedString password, Properties env) Get the connection from the dataSource with specified user and password.- Parameters:
datasourceName
- datasource JNDI nameuser
- DB userpassword
- DB passwordenv
- properties- Returns:
- the connection get from dataSource
-
getDatasourceConnection
public static Connection getDatasourceConnection(String datasourceName, String user, GuardedString password) Get the connection from the dataSource with specified user and password.- Parameters:
datasourceName
- datasource JNDI nameuser
- DB userpassword
- DB password- Returns:
- the connection get from dataSource
-
getDatasourceConnection
Get the connection from the datasource.- Parameters:
datasourceName
- datasource JNDI name- Returns:
- the connection get from default jndi context
-
getDriverMangerConnection
public static Connection getDriverMangerConnection(String driver, String url, String login, GuardedString password) Gets aConnection
using the basic driver manager.- Parameters:
driver
- jdbc driver nameurl
- jdbc connection urllogin
- jdbc login namepassword
- jdbc password- Returns:
- a valid connection
-
rollbackQuietly
Ignores any exception thrown by theConnection
parameter when closed, it also checks fornull
.- Parameters:
conn
- JDBC connection to rollback.
-
rollbackQuietly
Ignores any exception thrown by theDatabaseConnection
parameter when closed, it also checks fornull
.- Parameters:
conn
- DatabaseConnection to rollback.
-
closeQuietly
Ignores any exception thrown by theConnection
parameter when closed, it also checks fornull
.- Parameters:
conn
- JDBC connection to close.
-
closeQuietly
Ignores any exception thrown by theConnection
parameter when closed, it also checks fornull
.- Parameters:
conn
- DatabaseConnection to close.
-
closeQuietly
Ignores any exception thrown by theStatement.close()
method.- Parameters:
stmt
-Statement
to close.
-
closeQuietly
- Parameters:
rset
-ResultSet
to close quitely.
-
date2String
Date to string.- Parameters:
value
- Date value- Returns:
- String value
-
time2String
Time to String format.- Parameters:
value
- Time value- Returns:
- String value
-
timestamp2String
Convert timestamp to string.- Parameters:
value
-Timestamp
- Returns:
- the string value
-
string2Time
String to Time.- Parameters:
param
- String- Returns:
- the Time value
-
string2Date
String to Date.- Parameters:
param
- the String value- Returns:
- Date value
-
string2Timestamp
Convert string to Timestamp- Parameters:
param
- String value- Returns:
- Timestamp value
-
string2Boolean
Convert String to boolean.- Parameters:
val
- string value- Returns:
- Boolean value
-
normalizeNullValues
The null param vlaue normalizator.- Parameters:
sql
- SQL queryparams
- listout
- out param list- Returns:
- the modified string
-
blob2ByteArray
Make a blob conversion.- Parameters:
blobValue
- blob- Returns:
- a converted value
- Throws:
SQLException
- if anything goes wrong
-
setParams
public static void setParams(PreparedStatement statement, List<SQLParam> params) throws SQLException Binds the "?" markers in SQL statement with the parameters given as values. It concentrates the replacement of all params.GuardedString
are handled so the password is never visible.- Parameters:
statement
- SQL statementparams
- aList
of the object arguments- Throws:
SQLException
- an exception in statement
-
setParams
public static void setParams(CallableStatement statement, List<SQLParam> params) throws SQLException Binds the "?" markers in SQL statement with the parameters given as values. It concentrates the replacement of all params.GuardedString
are handled so the password is never visible.- Parameters:
statement
- SQL statementparams
- aList
of the object arguments- Throws:
SQLException
- an exception in statement
-
getColumnValues
Read one row from database result set and convert a columns to attribute set.- Parameters:
resultSet
- database data- Returns:
- The transformed attribute set
- Throws:
SQLException
- if anything goes wrong
-
getSQLParam
public static SQLParam getSQLParam(ResultSet resultSet, int i, String name, int sqlType) throws SQLException Retrieve the SQL value from result set.- Parameters:
resultSet
- the result seti
- indexname
- param namesqlType
- expected SQL type or Types.NULL for generic- Returns:
- the object return the retrieved object
- Throws:
SQLException
- any SQL error
-
getSQLAttributeType
Convert database type to connector supported set of attribute types Can be redefined for different databases.- Parameters:
sqlType
- #Types
- Returns:
- a connector supported class
-
setSQLParam
Set a parameter to statement. The conversion to required database type is expected to be done.- Parameters:
stmt
- the statement to setidx
- index of the parameterparm
- theSQLParam
value- Throws:
SQLException
- something wrong
-
jdbc2AttributeValue
The conversion to required attribute type.- Parameters:
value
- to be converted to an attribute- Returns:
- a attribute's supported object
- Throws:
SQLException
- something is not ok
-
attribute2jdbcValue
Convert the attribute to expected jdbc type using java conversions Some database strategy sets all attributes as string, other convert them first and than set as native.- Parameters:
value
- the value to be convertedsqlType
- the target sql type- Returns:
- the converted object value
- Throws:
SQLException
- any SQL error
-
setGuardedStringParam
public static void setGuardedStringParam(PreparedStatement stmt, int idx, GuardedString guard) throws SQLException The helper guardedString bind method.- Parameters:
stmt
- to bind toidx
- index of the objectguard
- aGuardedString
parameter- Throws:
SQLException
- any SQL error
-
selectSingleValue
public static Object selectSingleValue(Connection conn, String sql, SQLParam... params) throws SQLException Selects single value (first column) from select. It fetches only first row, does not check whether more rows are returned by select. If no row is returned, returns null- Parameters:
conn
- JDBC connectionsql
- Select statement with or without parametersparams
- Parameters to use in statement- Returns:
- first row and first column value
- Throws:
SQLException
- any SQL error
-
selectRows
public static List<Object[]> selectRows(Connection conn, String sql, SQLParam... params) throws SQLException Selects all rows from select. It usesResultSet.getMetaData()
to find columns count and useResultSet.getObject(int)
to retrieve column value.- Parameters:
conn
- JDBC connectionsql
- SQL select with or without paramsparams
- SQL parameters- Returns:
- list of selected rows
- Throws:
SQLException
- any SQL error
-
executeUpdateStatement
public static int executeUpdateStatement(Connection conn, String sql, SQLParam... params) throws SQLException Executes DML sql statement. This can be useful to execute insert/update/delete or some database specific statement in one call- Parameters:
conn
- connectionsql
- SQL queryparams
- SQL parameters- Returns:
- number of rows affected as defined by
PreparedStatement.executeUpdate()
- Throws:
SQLException
- any SQL error
-