IBM DB2 Manual page 51

Table of Contents

Advertisement

java.sql.Connection con = java.sql.DriverManager.getConnection(url, properties);
java.sql.Statement s = con.createStatement();
// Clean up from previous executions
try {
s.executeUpdate ("drop table TestQBatch");
}
catch (Exception e) {
}
// Create and populate a test table
s.executeUpdate ("create table TestQBatch (col1 int, col2 char(10))");
s.executeUpdate ("insert into TestQBatch values (1, 'test1')");
s.executeUpdate ("insert into TestQBatch values (2, 'test2')");
s.executeUpdate ("insert into TestQBatch values (3, 'test3')");
s.executeUpdate ("insert into TestQBatch values (4, 'test4')");
s.executeUpdate ("insert into TestQBatch values (1, 'test5')");
s.executeUpdate ("insert into TestQBatch values (2, 'test6')");
try {
PreparedStatement pstmt =
con.prepareStatement("Select * from TestQBatch where col1 = ?");
pstmt.setInt(1,1);
pstmt.addBatch();
// Add some more values to the batch
pstmt.setInt(1,2);
pstmt.addBatch();
pstmt.setInt(1,3);
pstmt.addBatch();
pstmt.setInt(1,4);
pstmt.addBatch();
((com.ibm.db2.jcc.DB2PreparedStatement)prepStmt).executeDB2QueryBatch();
} catch(BatchUpdateException b) {
// process BatchUpdateException
}
while(pstmt.getMoreResults()) {
java.sql.ResultSet rs = pstmt.getResultSet();
while (rs.next()) {
System.out.print (rs.getInt (1) + " ");
System.out.println (rs.getString (2));
}
System.out.println ();
rs.close ();
}
// Clean up
s.close ();
pstmt.close ();
con.close ();
Related tasks
"Making batch updates in JDBC applications" on page 28
Related reference
"DB2PreparedStatement interface" on page 351
Learning about a ResultSet using ResultSetMetaData methods
You cannot assume that you know the number of columns and data types of the
columns in a table or result set. This is true especially when you are retrieving data
from a remote data source.
When you write programs that retrieve unknown ResultSets, you need to use
ResultSetMetaData methods to determine the characteristics of the ResultSets
before you can retrieve data from them.
ResultSetMetaData methods provide the following types of information:
1
2a
2b
3
4
4a
Chapter 3. JDBC application programming
35

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents