📄 executesql.java
字号:
}
}
return retVal;
}
/** Executes JDBC update statement for SQL statements of type INSERT, UPDATE, DELETE.<BR>
* Method is wrapper invoking rowUpdate(Connection, String, boolean), where
* the boolean can be set by the method setPrintStringSQL(), its default value == false.
* Prints SQL string to System.out before execution, if isPrintStringSQL == true.<P>
* Returns number of rows modified, a return value of -1 indicates an error condition.
* @see #isPrintStringSQL()
* @see #setPrintStringSQL(boolean)
* @see #lockTableForUpdate(Connection, String, String)
*/
public static int rowUpdate(Connection conn, String sql) {
return rowUpdate(conn, sql, printStringSQLFlag);
}
/** Executes JDBC update statement for SQL statements of type INSERT, UPDATE, DELETE.<BR>
* Prints SQL string to System.out before execution, if print == true.<P>
* Returns number of rows modified, a return value of -1 indicates an error condition.
* @see #lockTableForUpdate(Connection, String, String)
*/
public static int rowUpdate(Connection conn, String sql, boolean print) {
int nrows = 0;
if (conn == null) {
System.err.println("ExecuteSQL rowUpdate: JDBC connection null;" +
" application must first instantiate a connection class" +
" (see JDBConn(String url, String driverName, String user, String passwd)");
return -1;
}
Statement sm = null;
try {
sm = conn.createStatement();
if (print) System.out.println("ExecuteSQL.rowUpdate SQL string:\n" + sql);
nrows = sm.executeUpdate(sql);
}
catch (SQLException ex) {
System.err.println("ExecuteSQL: rowUpdate executeUpdate statement SQLException");
System.err.println("ExecuteSQL.rowUpdate SQL string:\n" + sql);
SQLExceptionHandler.prtSQLException(ex);
nrows = -1;
}
finally {
try { if (sm != null) sm.close(); }
catch (SQLException ex) {
SQLExceptionHandler.prtSQLException(ex);
}
}
return nrows;
}
/** Returns an Object array whose elements are also Object []'s resulting from the parsing each row of the input ResultSet.
* Method iterates through the ResultSet object and closes the ResultSet upon reaching the end of the results.
* Returned Object array can be used as input to org.trinet.jdbc.table.DataTableRow methods to extract pertinent data.<P>
* Row parsing begins at the column offset specified by the offset input argument. Specify 0 to begin at the first column.
* Row parsing stops after parsing the number of objects specified by the length input argument.
* Specify a length = 0 to retrieve all of the column objects from the input offset to the end of the ResultSet row.<P>
*<PRE>
* Returns null:
* if the input ResultSet is null.
* if the input offset or length are < 0.
* if offset+length > ResultSet column count.
* if an SQLException occurs during processing of the ResultSet.
*</PRE><P>
* <em>For Oracle an database: NUMBER => java.math.BigDecimal, VARCHAR => java.lang.String, and DATE => java.sql.Timestamp.</em>
* @see org.trinet.jdbc.table.DataTableRow#objectToDataObject(Object [],int)
* @see org.trinet.jdbc.table.DataTableRow#dataObjectToFields(DataObject[])
*/
public static Object [] parseResults(ResultSet rs, int offset, int length) {
// Oracle NUMBER is returned as BigDecimal, VARCHAR is returned as String, and DATE is returned as java.sql.Timestamp.
if (rs == null) {
System.err.println("ExecuteSQL parseResults: null ResultSet input argument.");
return null;
}
if (offset < 0) {
System.err.println("ExecuteSQL parseResults: input offset < 0");
return null;
}
if (length < 0) {
System.err.println("ExecuteSQL parseResults: input length < 0");
return null;
}
int maxColumns;
try {
ResultSetMetaData rsmd = rs.getMetaData();
maxColumns = rsmd.getColumnCount();
if (maxColumns < offset + length) {
System.err.println("ExecuteSQL parseResults: ResultSet columns < offset+length");
return null;
}
}
catch (SQLException ex) {
SQLExceptionHandler.prtSQLException(ex);
System.err.println("ExecuteSQL parseResults: cannot get ResultSetMetaData");
return null;
}
int arrayLength;
if (length <= 0) arrayLength = maxColumns - offset;
else arrayLength = length;
int nrows = 0;
Object [] row = new Object[arrayLength];
Vector vtr = new Vector();
try {
while ( rs.next() ) {
for (int index = 0; index < arrayLength; index++) {
try {
row[index] = rs.getObject(index + 1 + offset); // ResultSet indexes start at 1, but arrays start at 0.
}
catch ( SQLException ex) {
SQLExceptionHandler.prtSQLException(ex);
System.err.println("ExecuteSQL parseResults: error at column index value: " + index);
return null;
}
catch ( IndexOutOfBoundsException ex) {
ex.printStackTrace();
return null;
}
}
vtr.add(row);
nrows++;
}
}
catch (SQLException ex) {
System.err.println("ExecuteSQL parseResults: ResultSet.next() SQLException");
SQLExceptionHandler.prtSQLException(ex);
return null;
}
try {
rs.close();
}
catch (SQLException ex) {
System.err.println("ExecuteSQL parseResults: ResultSet.close() SQLException");
SQLExceptionHandler.prtSQLException(ex);
return null;
}
if ( vtr.size() > 0) return vtr.toArray();
else return null;
}
/** Returns an array object derived from parsing all rows of the input ResultSet into instances of the specified input Class.
* The returned object can be cast to an array of the Class type specified by the input Class argument.
* Starts row parse at the column at the specified offset. After parsing all rows, the the input ResultSet is closed.
* Sets the connection object of the DataTableRow to the specified Connection object, if not null, else it is set to null.
* Sets the mutability state of the returned DataTableRow instance to false if isSelectForUpdate() == false.
* Sets the mutability state for the DataObject in any declared table key column to false.
* Returns null if input ResultSet is null or input offset < 0.
* Returns null if input Class type is not an extension of org.trinet.jdbc.table.DataTableRow.
* Returns null if input offset+table_row_columns (i.e. TableRowXXX.MAX_FIELDS) > number of columns returned in the ResultSet row.
* Returns null if an SQLException occurs during processing.
* @see #parseOneRow(ResultSet, int, Class, Connection)
* @see org.trinet.jdbc.table.DataTableRow
* @see org.trinet.jdbc.table.DataTableRow#parseOneRow(ResultSetDb, int)
*/
public static Object parseAllRows(ResultSet rs, int offset, Class type, Connection conn) {
Vector vtr = new Vector();
try {
while ( rs.next() ) {
DataTableRow dtr = parseOneRow(rs, offset, type, conn);
if (dtr == null) return null;
vtr.add(dtr);
}
}
catch (SQLException ex) {
System.err.println("ExecuteSQL: parseResults ResultSet.next() SQLException");
SQLExceptionHandler.prtSQLException(ex);
return null;
}
try {
rs.close();
}
catch (SQLException ex) {
System.err.println("ExecuteSQL: parseResults ResultSet.close() SQLException");
SQLExceptionHandler.prtSQLException(ex);
return null;
}
return recast(vtr, type);
}
/** Returns a DataTableRow object derived from parsing one row of the input ResultSet into instance of specified input Class.
* Starts row parse at the column at the specified offset. After row parse, does no other JDBC operations on ResultSet.
* Sets the connection object of the DataTableRow to the specified Connection object, if not null, else it is set to null.
* Sets the mutability state of the returned DataTableRow instance to false if isSelectForUpdate() == false.
* Sets the mutability state for the DataObject in any declared table key column to false.
* Returns null if input ResultSet is null or input offset < 0.
* Returns null if input Class type is not an extension of org.trinet.jdbc.table.DataTableRow.
* Returns null if input offset+table_row_columns (i.e. TableRowXXX.MAX_FIELDS) > number of columns returned in the ResultSet row.
* Returns null if an SQLException occurs during processing.
* @see org.trinet.jdbc.table.DataTableRow
* @see org.trinet.jdbc.table.DataTableRow#parseOneRow(ResultSetDb, int)
*/
public static DataTableRow parseOneRow(ResultSet rs, int offset, Class type, Connection conn) {
if (rs == null ) {
System.err.println("ExecuteSQL parseOneRow: null ResultSet input argument.");
return null;
}
if (offset < 0) {
System.err.println("ExecuteSQL parseOneRow: invalid offset input argument.");
return null;
}
if (! DataTableRow.class.isAssignableFrom(type)) {
System.err.println("ExecuteSQL parseOneRow: invalid class input argument; must be able to cast as DataTableRow.");
return null;
}
DataTableRow row = null;
try {
row = (DataTableRow) type.newInstance(); // create new instance of tableName class
}
catch (IllegalAccessException ex) {
System.err.println("ExecuteSQL parseOneRow: class or initializer not accessible: " +
type.getName());
return null;
}
catch (InstantiationException ex) {
System.err.println("ExecuteSQL parseOneRow: cannot instantiate class check type: " +
type.getName());
return null;
}
try {
ResultSetMetaData rsmd = rs.getMetaData();
if (rsmd.getColumnCount() < offset + row.getMaxFields() ) {
System.err.println("ExecuteSQL parseOneRow: offset+totalRowColumns exceeds the total columns in ResultSet.");
return null;
}
}
catch (SQLException ex) {
SQLExceptionHandler.prtSQLException(ex);
System.err.println("ExecuteSQL parseOneRow: cannot get ResultSetMetaData");
return null;
}
if (conn != null) row.setConnection(conn);
// Remember ResultSet column indexes start at 1, but arrays start at 0.
for (int index = 0; index < row.getMaxFields(); index++) {
try {
row.setValue(index, rs.getObject(index+1+offset) );
}
catch ( SQLException ex) {
SQLExceptionHandler.prtSQLException(ex);
System.err.println("ExecuteSQL parseOneRow at column index value: " + index + " columnName: "
+ row.getFieldNames()[index] + " fieldClass: " + row.DATA_CLASSES[row.getFieldClassIds()[index]]
+ " offset: " + offset);
return null;
}
}
for (int index = 0; index < row.getKeyIndex().length; index++) {
row.setMutableValue(row.getKeyIndex()[index], false); // key columns are not modifiable
}
if (! isSelectForUpdate()) row.setMutable(false); // read-only mode, do not modify row contents
return row;
}
/** Returns a new array of input Class type whose elements are the respective elements of the input List object.
* Returns null if the List object is null or empty.
*/
public static Object recast(List list, Class type) {
Object retVal = null;
if (list == null) return null;
if ( list.size() > 0) {
retVal = java.lang.reflect.Array.newInstance(type, list.size());
for (int index = 0; index < list.size(); index++) {
java.lang.reflect.Array.set(retVal, index, list.get(index));
}
}
return retVal;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -