Академический Документы
Профессиональный Документы
Культура Документы
Execute a Query
String query = "SELECT col1, col2, col3 FROM sometable";
con.setAutoCommit(true);
CallableStatement cstmt =
con.prepareCall( "{call getTestData(?, ?)}");
If the stored procedure returns OUT parameters, the JDBC type of each
OUT parameter must be registered before the CallableStatement
object can be executed.
Registering the JDBC type is done with the method
registerOutParameter.
Then after the statement has been executed, CallableStatement's
getXXX methods retrieve the parameter value.
The correct getXXX method to use is the Java type that corresponds to
The following code registers the OUT parameters, executes the
stored procedure called by cstmt, and then retrieves the values
returned in the OUT parameters.
CallableStatement cstmt =
con.prepareCall( "{call getTestData(?, ?)}");
cstmt.registerOutParameter(1, java.sql.Types.TINYINT);
cstmt.registerOutParameter(2, java.sql.Types.DECIMAL, 3);
cstmt.executeQuery();
byte x = cstmt.getByte(1);
java.math.BigDecimal n = cstmt.getBigDecimal(2, 3);
INOUT Parameters
A parameter that supplies input as well as accepts output (an
INOUT parameter) requires a call to the appropriate setXXX
method in addition to a call to the method registerOutParameter
CallableStatement cstmt =
con.prepareCall( "{call reviseTotal(?)}");
cstmt.setByte(1, 25);
cstmt.registerOutParameter(1, java.sql.Types.TINYINT);
cstmt.executeUpdate();
byte x = cstmt.getByte(1);
Java Type JDBC type
String VARCHAR or LONGVARCHAR
java.math.BigDecimal NUMERIC
boolean BIT
byte TINYINT
short SMALLINT
int INTEGER
long BIGINT
float REAL
double DOUBLE
byte[] VARBINARY or
LONGVARBINARY java.sql.Date DATE
java.sql.Time TIME
New Features in the JDBC 2.0 API
With the JDBC 2.0 API, you will be able to do the following:
Scroll forward and backward in a result set or move to a
specific row
Make updates to database tables using methods in the Java
programming language instead of using SQL commands
Send multiple SQL statements to the database as a unit, or
batch
Moving the Cursor in Scrollable Result Sets
The following line of code illustrates one way to create a scrollable
ResultSet object:
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet srs = stmt.executeQuery("SELECT * FROM EMP");
The method getRow lets you check the number of the row where
the cursor is positioned.
srs.absolute(4);
int rowNum = srs.getRow(); // rowNum should be 4
Four additional methods let you verify whether the cursor is at a
particular position. The position is stated in their names:
isFirst , isLast , isBeforeFirst , isAfterLast . These
methods all return a boolean.
Making Updates to Updatable Result Sets
Connection con = DriverManager.getConnection();
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = stmt.executeQuery("SELECT .");
uprs.last();
uprs.updateFloat("PRICE", 10.99);
uprs.updateRow();
Update operations in the JDBC 2.0 API affect column values in the row
where the cursor is positioned, so in the first line the ResultSet uprs
calls the method last to move its cursor to the last row.
The ResultSet. updateXXX methods take two parameters: the
column to update and the new value to put in that column.
Inserting and Deleting Rows Programmatically
Your first step will be to move the cursor to the insert row, which you do
by invoking the method moveToInsertRow .
The next step is to set a value for each column in the row.
You do this by calling the appropriate updateXXX method for each
value.
Finally, you call the method insertRow to insert the row you have just
populated with values into the result set. This one method
simultaneously inserts the row into both the ResultSet object and the
database table from which the result set was selected.
Connection con = DriverManager.getConnection(.");
Statement stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
Note that you can invoke moveToCurrentRow only when the cursor
is on the insert row.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "", "");
stmt =
con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet uprs = stmt.executeQuery("SELECT * FROM EMP");
uprs.absolute(1);
uprs.deleteRow();
uprs.refreshRow();
Making Batch Updates
A batch update is a set of multiple update statements that is submitted to
the database for processing as a batch.
Sending multiple update statements to the database together as a unit
can, in some situations, be much more efficient than sending each update
statement separately.
con.setAutoCommit(false);
Statement stmt = con.createStatement();
stmt.addBatch("INSERT INTO COFFEES" +
"VALUES('Amaretto', 49, 9.99, 0, 0)");
stmt.addBatch("INSERT INTO COFFEES" +
"VALUES('Hazelnut', 49, 9.99, 0, 0)");
int [] updateCounts = stmt.executeBatch();