Example of a trace program under the IBM Data Server Driver for
JDBC and SQLJ
You might want to write a single class that includes methods for tracing under the
DriverManager interface, as well as the DataSource interface.
The following example shows such a class. The example uses IBM Data Server
Driver for JDBC and SQLJ type 4 connectivity.
public class TraceExample
{
public static void main(String[] args)
{
sampleConnectUsingSimpleDataSource();
sampleConnectWithURLUsingDriverManager();
}
private static void sampleConnectUsingSimpleDataSource()
{
java.sql.Connection c = null;
java.io.PrintWriter printWriter =
new java.io.PrintWriter(System.out, true);
// Prints to console, true means
// auto-flush so you don't lose trace
try {
javax.sql.DataSource ds =
new com.ibm.db2.jcc.DB2SimpleDataSource();
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setServerName("sysmvs1.stl.ibm.com");
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setPortNumber(5021);
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setDatabaseName("san_jose");
((com.ibm.db2.jcc.DB2BaseDataSource) ds).setDriverType(4);
ds.setLogWriter(printWriter); // This turns on tracing
// Refine the level of tracing detail
((com.ibm.db2.jcc.DB2BaseDataSource) ds).
setTraceLevel(com.ibm.db2.jcc.DB2SimpleDataSource.TRACE_CONNECTS |
com.ibm.db2.jcc.DB2SimpleDataSource.TRACE_DRDA_FLOWS);
// This connection request is traced using trace level
// TRACE_CONNECTS | TRACE_DRDA_FLOWS
c = ds.getConnection("myname", "mypass");
// Change the trace level to TRACE_ALL
// for all subsequent requests on the connection
((com.ibm.db2.jcc.DB2Connection) c).setJccLogWriter(printWriter,
com.ibm.db2.jcc.DB2BaseDataSource.TRACE_ALL);
// The following INSERT is traced using trace level TRACE_ALL
java.sql.Statement s1 = c.createStatement();
s1.executeUpdate("INSERT INTO sampleTable(sampleColumn) VALUES(1)");
s1.close();
// This code disables all tracing on the connection
((com.ibm.db2.jcc.DB2Connection) c).setJccLogWriter(null);
// The following INSERT statement is not traced
java.sql.Statement s2 = c.createStatement();
s2.executeUpdate("INSERT INTO sampleTable(sampleColumn) VALUES(1)");
s2.close();
c.close();
Figure 62. Example of tracing under the IBM Data Server Driver for JDBC and SQLJ
486 Application Programming Guide and Reference for Java
â„¢