Share this page 

Enable JDBC loggingTag(s): JDBC


[JDK1.2]

Take the following program.

import java.net.URL;
import java.sql.*;

class JDBCapp  {
  static MyConnection theConn;

  public static void main (String args[]) {
    new JDBCapp().doit();
    }

  public void doit() {
    theConn = new MyConnection();
    theConn.connect("EAS Demo DB V3", "dba", "sql");

PreparedStatement prepstmt;
try {
  prepstmt = theConn.dbConn.prepareStatement
   ("SELECT emp_id FROM employee" );
  prepstmt.execute();
  prepstmt.close();
  }
catch (Exception e) { e.printStackTrace(); }
  theConn.disconnect();
  }
}


class MyConnection {
  Connection dbConn = null;
  void connect(String db, String user, String passw) {
    try {
      Driver d = 
       (Driver)Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
      String URL = "jdbc:odbc:" + db;
      dbConn = DriverManager.getConnection(URL, user, passw);
      java.io.PrintWriter w =
        new java.io.PrintWriter
           (new java.io.OutputStreamWriter(System.out));
      DriverManager.setLogWriter(w);
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }

  void disconnect() {
    try {
      dbConn.close();
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }
}
And the output at the console will look like this :
*Connection.prepareStatement (SELECT emp_id FROM employee)
Allocating Statement Handle (SQLAllocStmt), hDbc=185334988
hStmt=184290392
Setting statement option (SQLSetStmtOption), hStmt=184290392, 
Setting statement option (SQLSetStmtOption), hStmt=184290392, 
Preparing (SQLPrepare),  szSqlStr=SELECT emp_id FROM employee
Number of parameter markers (SQLNumParams), hStmt=184290392
value=0
Registering Statement sun.jdbc.odbc.JdbcOdbcPreparedStatement@19ee1ac
*PreparedStatement.execute
Free statement (SQLFreeStmt), hStmt=184290392, fOption=0
Executing (SQLExecute), hStmt=184290392
Number of result columns (SQLNumResultCols), hStmt=184290392
value=1
*PreparedStatement.close
Free statement (SQLFreeStmt), hStmt=184290392, fOption=1
deregistering Statement sun.jdbc.odbc.JdbcOdbcPreparedStatement@19ee1ac
*Connection.close
0 Statement(s) to close
Disconnecting (SQLDisconnect), hDbc=185334988
Closing connection (SQLFreeConnect), hDbc=185334988
Closing environment (SQLFreeEnv), hEnv=185334828