【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-UpdateableResultSetExample.java,希望本篇文章对您学习有所帮助。
/*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*/
package ejava.jdbcch10;
import java.util.Properties;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.lang.ClassNotFoundException;
import java.sql.DatabaseMetaData;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.
sql.Types;
import java.sql.PreparedStatement;
import ejava.jdbcch09.ConnectToDatabase;
/**
* This call shows how to use Updateable ResultSet
*/
public class UpdateableResultSetExample extends ConnectToDatabase {
/**
* Constructor
*
*
* @see
*/
public UpdateableResultSetExample() {
super();
}
/**
* Constructor
*
*
* @param propertiesFile
*
* @see
*/
public UpdateableResultSetExample(String propertiesFile)
throws FileNotFoundException, IOException {
super(propertiesFile);
}
/**
* if it is not connected to database, connect to database and
* create a preparestatement and returns the Statement
* @param sqlString queryString to make a statement
* @param scrollType should be ResultSet.TYPE_FORWARD_ONLY
* or ResultSet.TYPE_SCROLL_INSENSITIVE
* or ResultSet.TYPE_SCROLL_SENSITIVE
* @param updateType should be ResultSet.CONCUR_READ_ONLY or
* ResultSet.CONCUR_UPDATABLE ;
*/
protected PreparedStatement getStatement(String sqlString,
int scrollType,
int updateType) throws SQLException,
Exception {
Connection connection = super.getConnection();
// for cloudscape to work with scrollable resultsets, the autocommit
// should be false.
connection.setAutoCommit(false);
PreparedStatement preparedStatement =
connection.prepareStatement(sqlString, scrollType, updateType);
return preparedStatement;
}
/**
* if it is not connected to database, connect to database and
* create a statement and returns the Statement
* @param scrollType should be ResultSet.TYPE_FORWARD_ONLY
* or ResultSet.TYPE_SCROLL_INSENSITIVE
* or ResultSet.TYPE_SCROLL_SENSITIVE
* @param updateType should be ResultSet.CONCUR_READ_ONLY or
* ResultSet.CONCUR_UPDATABLE ;
*/
protected Statement getStatement(int scrollType, int updateType)
throws SQLException, Exception {
Connection connection = super.getConnection();
// for cloudscape to work with scrollable resultsets, the autocommit
// should be false.
connection.setAutoCommit(false);
Statement statement = connection.createStatement(scrollType,
updateType);
return statement;
}
/**
* this method executes the statement and resuts the resultSet due to
* execution
* of Statement.only SELECT Statemet is applied here.
*/
public ResultSet executeQueryScrollInsensitiveUpdateable(String sqlString)
throws SQLException, Exception {
Statement statement = getStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Execute a SQL statement that returns a single ResultSet.
ResultSet rs = statement.executeQuery(sqlString);
return rs;
}
/**
* this method executes the statement and returns the resultSet due to
* execution of Statement. only SELECT Statement is applied here.
*/
public ResultSet executeQueryScrollForwardOnlyUpdateable(String sqlString)
throws SQLException, Exception {
Statement statement = getStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_UPDATABLE);
// Execute a SQL statement that returns a single ResultSet.
ResultSet rs = statement.executeQuery(sqlString);
return rs;
}
/**
* This meethod returns Scroll Sensitive Updateable ResultSet
*/
public ResultSet executeQueryScrollSensitiveUpdateable(String sqlString)
throws SQLException, Exception {
Statement statement = getStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Execute a SQL statement that returns a single ResultSet.
ResultSet rs = statement.executeQuery(sqlString);
return rs;
}
/**
* insert a Row into Orders table using Updateable ResultSet.
*/
public void insertNewRowIntoOrdersTableUsingColumnNames(ResultSet rs)
throws SQLException {
// this method works only if the table is Orders Table in the Demo
// database.
// if the table is different you should change most of this code
// for that purpose.
rs.moveToInsertRow();
rs.updateInt("CUSTOMER_ID_FK", 101)