EasyManuals Logo

IBM DB2 User Manual

IBM DB2
585 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #51 background imageLoading...
Page #51 background image
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 = 1
con.prepareStatement("Select * from TestQBatch where col1 = ?");
pstmt.setInt(1,1); 2a
pstmt.addBatch(); 2b
// 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();
3
} catch(BatchUpdateException b) {
// process BatchUpdateException
}
while(pstmt.getMoreResults()) {
java.sql.ResultSet rs = pstmt.getResultSet(); 4
while (rs.next()) { 4a
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:
Chapter 3. JDBC application programming 35

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the IBM DB2 and is the answer not in the manual?

IBM DB2 Specifications

General IconGeneral
DeveloperIBM
Initial Release1983
LicenseProprietary
Written inC, C++
CategoryDatabase Management System (RDBMS)
Operating SystemLinux, Windows
Programming LanguagesC, C++
Data ModelsRelational
EditionsCommunity, Standard, Advanced
Key Featureshigh availability, scalability