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 Recent java Faqs
- How to avoid an java.util.ConcurrentModificationException with ArrayList?
- How to convert a given array to a list in Java?
- How to make Java objects eligible for garbage collection?
- What are local variables in Java?
- What are instance variables in Java?
- How many backslashes?
- What are class variables in Java?
Most Viewed java Faqs
- How to use HttpURLConnection POST data to web server?
- What is runtime polymorphism in Java?
- How to add BASIC Authentication into HttpURLConnection?
- What is String literal pool?
- Can the run() method be called directly to start a thread?
- What does Class.forname method do?
- How to read input from console (keyboard) in Java?