📄 sqlprocessor.java
字号:
* Execute a query baed ont SQL string given
*
* @param sql The SQL string to be executed
* @return The result set of the query
* @throws GenericEntityException
* @throws GenericDataSourceException
*/
public ResultSet executeQuery(String sql) throws GenericDataSourceException, GenericEntityException {
prepareStatement(sql);
return executeQuery();
}
/**
* Execute updates
*
* @return The number of rows updated
* @throws GenericDataSourceException
*/
public int executeUpdate() throws GenericDataSourceException {
try {
// if (Debug.verboseOn()) Debug.logVerbose("[SQLProcessor.executeUpdate] ps=" + _ps.toString(), module);
return _ps.executeUpdate();
} catch (SQLException sqle) {
// don't display this here, may not be critical, allow handling further up... Debug.logError(sqle, "SQLProcessor.executeUpdate() : ERROR : ", module);
throw new GenericDataSourceException("SQL Exception while executing the following:" + _sql, sqle);
}
}
/**
* Execute update based on the SQL statement given
*
* @param sql SQL statement to be executed
* @throws GenericDataSourceException
*/
public int executeUpdate(String sql) throws GenericDataSourceException {
Statement stmt = null;
try {
stmt = _connection.createStatement();
return stmt.executeUpdate(sql);
} catch (SQLException sqle) {
Debug.logError(sqle, "SQLProcessor.executeUpdate(sql) : ERROR : ", module);
throw new GenericDataSourceException("SQL Exception while executing the following:" + _sql, sqle);
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqle) {
Debug.logWarning("Unable to close 'statement': " + sqle.getMessage(), module);
}
}
}
}
/**
* Test if there more records available
*
* @return true, if there more records available
*
* @throws GenericDataSourceException
*/
public boolean next() throws GenericDataSourceException {
try {
return _rs.next();
} catch (SQLException sqle) {
throw new GenericDataSourceException("SQL Exception while executing the following:" + _sql, sqle);
}
}
/**
* Getter: get the currently activ ResultSet
*
* @return ResultSet
*/
public ResultSet getResultSet() {
return _rs;
}
/**
* Getter: get the prepared statement
*
* @return PreparedStatement
*/
public PreparedStatement getPreparedStatement() {
return _ps;
}
/**
* Execute a query based on the SQL string given. For each record
* of the ResultSet return, execute a callback function
*
* @param sql The SQL string to be executed
* @param aListener The callback function object
*
* @throws GenericEntityException
*/
public void execQuery(String sql, ExecQueryCallbackFunctionIF aListener) throws GenericEntityException {
if (_connection == null) {
getConnection();
}
try {
if (Debug.verboseOn()) Debug.logVerbose("[SQLProcessor.execQuery]: " + sql, module);
executeQuery(sql);
// process the results by calling the listener for
// each row...
boolean keepGoing = true;
while (keepGoing && _rs.next()) {
keepGoing = aListener.processNextRow(_rs);
}
if (_manualTX) {
_connection.commit();
}
} catch (SQLException sqle) {
Debug.logWarning("[SQLProcessor.execQuery]: SQL Exception while executing the following:\n" +
sql + "\nError was:", module);
Debug.logWarning(sqle.getMessage(), module);
throw new GenericEntityException("SQL Exception while executing the following:" + _sql, sqle);
} finally {
close();
}
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(String field) throws SQLException {
//ResultSetMetaData rsmd = this.getResultSetMetaData();
//this doesn't seem to work, query not yet parsed: int colType = rsmd.getColumnType(_ind);
if (field != null) {
//if (field.length() > 4000) {
//Clob clb = new Cl
// doesn't work with Oracle drivers, need the funky work-around: _ps.setCharacterStream(_ind, new StringReader(field), field.length());
//_needClobWorkAroundWrite.put(new Integer(_ind), field);
//_ps.setString(_ind, " ");
//} else {
_ps.setString(_ind, field);
//}
} else {
_ps.setNull(_ind, Types.VARCHAR);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(java.sql.Timestamp field) throws SQLException {
if (field != null) {
_ps.setTimestamp(_ind, field);
} else {
_ps.setNull(_ind, Types.TIMESTAMP);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(java.sql.Time field) throws SQLException {
if (field != null) {
_ps.setTime(_ind, field);
} else {
_ps.setNull(_ind, Types.TIME);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(java.sql.Date field) throws SQLException {
if (field != null) {
_ps.setDate(_ind, field);
} else {
_ps.setNull(_ind, Types.DATE);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(Integer field) throws SQLException {
if (field != null) {
_ps.setInt(_ind, field.intValue());
} else {
_ps.setNull(_ind, Types.NUMERIC);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(Long field) throws SQLException {
if (field != null) {
_ps.setLong(_ind, field.longValue());
} else {
_ps.setNull(_ind, Types.NUMERIC);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(Float field) throws SQLException {
if (field != null) {
_ps.setFloat(_ind, field.floatValue());
} else {
_ps.setNull(_ind, Types.NUMERIC);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(Double field) throws SQLException {
if (field != null) {
_ps.setDouble(_ind, field.doubleValue());
} else {
_ps.setNull(_ind, Types.NUMERIC);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(Boolean field) throws SQLException {
if (field != null) {
_ps.setBoolean(_ind, field.booleanValue());
} else {
_ps.setNull(_ind, Types.NULL); // TODO: really should be Types.BOOLEAN, but that wasn't introduced until Java 1.4... hmmm what to do?
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement.
*
* @param field
*
* @throws SQLException
*/
public void setValue(Object field) throws SQLException {
if (field != null) {
_ps.setObject(_ind, field, Types.JAVA_OBJECT);
} else {
_ps.setNull(_ind, Types.JAVA_OBJECT);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement
*
* @param field
*
* @throws SQLException
*/
public void setValue(Blob field) throws SQLException {
if (field != null) {
_ps.setBlob(_ind, field);
} else {
_ps.setNull(_ind, Types.BLOB);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement
*
* @param field
*
* @throws SQLException
*/
public void setValue(Clob field) throws SQLException {
if (field != null) {
_ps.setClob(_ind, field);
} else {
_ps.setNull(_ind, Types.CLOB);
}
_ind++;
}
/**
* Set the next binding variable of the currently active prepared statement
* to write the serialized data of 'field' to a BLOB.
*
* @param field
*
* @throws SQLException
*/
public void setBinaryStream(Object field) throws SQLException {
if (field != null) {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(field);
oos.close();
byte[] buf = os.toByteArray();
os.close();
ByteArrayInputStream is = new ByteArrayInputStream(buf);
_ps.setBinaryStream(_ind, is, buf.length);
is.close();
} catch (IOException ex) {
throw new SQLException(ex.getMessage());
}
} else {
_ps.setNull(_ind, Types.BLOB);
}
_ind++;
}
protected void finalize() throws Throwable {
try {
this.close();
} catch (Exception e) {
Debug.logError(e, "Error closing the result, connection, etc in finalize SQLProcessor", module);
}
super.finalize();
}
protected void testConnection(Connection con) throws GenericEntityException {
if (SQLProcessor.ENABLE_TEST) {
if (SQLProcessor.CONNECTION_TEST_LIST.contains(con.toString())) {
throw new GenericEntityException("Connection the exact same as index " + SQLProcessor.CONNECTION_TEST_LIST.indexOf(con.toString()));
}
SQLProcessor.CONNECTION_TEST_LIST.add(con.toString());
if (SQLProcessor.CONNECTION_TEST_LIST.size() > SQLProcessor.MAX_CONNECTIONS) {
SQLProcessor.CONNECTION_TEST_LIST.remove(0);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -