📄 sqlutil.java
字号:
* Intelligently sets the commit mode. When called for the first time on
* availiable connection it stores default values.
*
* @exception SQLException
*/
private void setCommitMode() throws SQLException {
if (conn == null) {
return;
}
// remember default
if (storedAutoCommit == null) { // very first time on this connection
if (defaultAutoCommit == null) {
storedAutoCommit = Boolean.valueOf(conn.getAutoCommit()); // remember default auto-commit mode...
} else {
storedAutoCommit = defaultAutoCommit; // ...or use specified default mode
}
}
// set new if required
boolean newMode;
if (currentAutoCommit != null) { // is there a new setting?
newMode = currentAutoCommit.booleanValue();
} else {
newMode = storedAutoCommit.booleanValue();
}
if (newMode != conn.getAutoCommit()) { // If new mode is different then current mode..
conn.setAutoCommit(newMode); // ...then specify new, specified setting
}
}
/**
* Resets connection commit mode to default, when connection is returned back
* to database pool.
*/
private void resetCommitMode() {
if (conn == null) {
return;
}
try {
if (currentAutoCommit != null) {
boolean currentMode = conn.getAutoCommit(); // if current mode...
boolean storedMode = storedAutoCommit.booleanValue(); // ...and stored mode...
if (currentMode != storedMode) { // ...are not equals...
conn.setAutoCommit(storedMode); // ...set mode to remembered default mode
}
}
} catch (SQLException sex) {
} finally {
currentAutoCommit = null;
storedAutoCommit = null;
}
}
/**
* Makes all changes made since the previous commit/rollback permanent and
* releases any database locks currently held by this object. This method
* should be used only when auto-commit mode has been disabled.
*
* @exception SQLException
*/
public void commit() throws SQLException {
conn.commit();
}
/**
* Undoes all changes made in the current transaction and releases any
* database locks currently held by this object. This method should be used
* only when auto-commit mode has been disabled.
*/
public void rollback() {
try {
conn.rollback();
} catch (SQLException sex) {
}
}
// ---------------------------------------------------------------- debug sql
private static boolean debugMode = false;
/**
* Sets debug mode.
*
* @param b
*/
public static void setDebugMode(boolean b) {
debugMode = b;
}
/**
* Returns current debug mode.
*
* @return current debug mode
*/
public static boolean getDebugMode() {
return debugMode;
}
private void createDebugSql(String sql) {
if (getDebugMode() == false) {
debugSql = new String[] {sql};
return;
}
debugSql = sql.split("\\?");
debugSqlParams = new String[debugSql.length];
int i = 0;
int limit = debugSql.length - 1;
while (i < limit) {
debugSqlParams[i] = "?";
i++;
}
if (sql.endsWith("?")) {
debugSqlParams[i] = "?";
} else {
debugSqlParams[i] = "";
}
}
private void setDebugSql(int ndx, String s) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = s;
}
private void setDebugSql(int ndx, int i) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = Integer.toString(i);
}
private void setDebugSql(int ndx, boolean i) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = Boolean.toString(i);
}
private void setDebugSql(int ndx, long i) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = Long.toString(i);
}
private void setDebugSql(int ndx, byte i) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = Byte.toString(i);
}
private void setDebugSql(int ndx, double i) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = Double.toString(i);
}
private void setDebugSql(int ndx, float i) {
if (getDebugMode() == false) {
return;
}
debugSqlParams[ndx - 1] = Float.toString(i);
}
/**
* Returns formated pseudo-SQL string. When <code>PreparedStatement</code>
* is used, all ? are replaced with String representation of assigned values.
* Just for debugging purposes.
*
* @return formated pseudo-SQL string
*/
public String toString() {
if (getDebugMode() == false) {
return debugSql[0];
}
StringBuffer result = new StringBuffer();
int i = 0;
while (i < debugSql.length) {
result.append(debugSql[i]);
result.append(debugSqlParams[i]);
i++;
}
return result.toString();
}
// ---------------------------------------------------------------- prepared statement (primitives)
/**
* Sets value of int parameter in sql string.
*
* @param ndx 1-based index od parameter
* @param value int value
*
* @exception SQLException
*/
public void setInt(int ndx, int value) throws SQLException {
pst.setInt(ndx, value);
setDebugSql(ndx, value);
}
/**
* Sets value of boolean parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setBoolean(int ndx, boolean value) throws SQLException {
pst.setBoolean(ndx, value);
setDebugSql(ndx, value);
}
/**
* Sets value of long parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setLong(int ndx, long value) throws SQLException {
pst.setLong(ndx, value);
setDebugSql(ndx, value);
}
/**
* Sets value of byte parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setByte(int ndx, byte value) throws SQLException {
pst.setByte(ndx, value);
setDebugSql(ndx, value);
}
/**
* Sets value of double parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setDouble(int ndx, double value) throws SQLException {
pst.setDouble(ndx, value);
setDebugSql(ndx, value);
}
/**
* Sets value of float parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setFloat(int ndx, float value) throws SQLException {
pst.setFloat(ndx, value);
setDebugSql(ndx, value);
}
// ---------------------------------------------------------------- prepared statement (objects)
/**
* Sets value of String parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setString(int ndx, String value) throws SQLException {
if (value != null) {
pst.setString(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.VARCHAR);
}
setDebugSql(ndx, value);
}
/**
* Sets value of java.sql.Date parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setDate(int ndx, java.sql.Date value) throws SQLException {
if (value != null) {
pst.setDate(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.DATE);
}
setDebugSql(ndx, value.toString());
}
/**
* Sets value of java.sql.Timestamp parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setTimestamp(int ndx, java.sql.Timestamp value) throws SQLException {
if (value != null) {
pst.setTimestamp(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.TIMESTAMP);
}
setDebugSql(ndx, value.toString());
}
/**
* Sets value of Array parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setArray(int ndx, Array value) throws SQLException {
if (value != null) {
pst.setArray(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.ARRAY);
}
setDebugSql(ndx, value.toString());
}
/**
* Sets value of BigDecimal parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setBigDecimal(int ndx, java.math.BigDecimal value) throws SQLException {
if (value != null) {
pst.setBigDecimal(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.NUMERIC);
}
setDebugSql(ndx, value.toString());
}
/**
* Sets value of Blob parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setBlob(int ndx, java.sql.Blob value) throws SQLException {
if (value != null) {
pst.setBlob(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.BLOB);
}
setDebugSql(ndx, value.toString());
}
/**
* Sets value of Clob parameter in sql string.
*
* @param ndx 1-based index of parameter
* @param value parameter value
*
* @exception SQLException
*/
public void setClob(int ndx, java.sql.Clob value) throws SQLException {
if (value != null) {
pst.setClob(ndx, value);
} else {
pst.setNull(ndx, java.sql.Types.CLOB);
}
setDebugSql(ndx, value.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -