How to enable JDBC tracing?
A good way to find out what JDBC calls are doing is to enable JDBC tracing. The JDBC trace contains a detailed listing of the activity occurring in the system that is related to JDBC operations.
If you use the DriverManager facility to establish your database connection, you use the DriverManager.setLogWriter method to enable tracing of JDBC operations.
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Program {
public static void main(String... args) {
Connection con = null;
try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
DriverManager.setLogWriter(new PrintWriter(System.out));
}
catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
return ;
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb",
"usr", "sql");
if(!con.isClosed())
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");
} catch(Exception e) {
System.out.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
}
catch(SQLException e) {
}
}
}
}
If you use a DataSource object to connect to a data source, you use the DataSource.setLogWriter method to enable tracing. (For pooled connections, you use the ConnectionPoolDataSource.setLogWriter method, and for connections that can participate in distributed transactions, you use the XADataSource.setLogWriter method.)
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?(15019)
- What is runtime polymorphism in Java?(9496)
- What is String literal pool?(8792)
- Can the run() method be called directly to start a thread?(8253)
- How to add BASIC Authentication into HttpURLConnection?(7483)
- Can transient variables be declared as 'final' or 'static'?(6303)
- Can static methods be overridden?(4965)
Most Recent java Faqs
- What is the difference between an enum type and java.lang.Enum?
- Which replace function works with regex?
- Why does TreeSet.add throw ClassCastException?
- What is variable hiding and shadowing?
- Can private method be overridden?
- How to enable JDBC tracing?
- How to Retrieve Automatically Generated Keys in JDBC?