This is a small Java library for logging SQL statements of JDBC connections.
The JDBC drivers from the driver manufacturers
normally don't have logging functions so you can not
see which statements are sent to the database
server.
This library solves this problem. It wraps around a
real Connection object and prints the statements
out.
You need a Java Runtime environment with a minimum version 1.5 aka 5.0. You can download it from here.
Extract the zip file to a directory and include the file jsqllogger.jar in the classpath of your application. Example:
java.exe -cp "jsqllogger.jar" -jar <Application Jar file>
The library must wrap around a "real" Connection
object which is returned by the
java.sql.DriverManager.
The following example shows the usage with a HSQL database.
import java.sql.*;
import de.volkeralthaus.jsqllogger.*;
import de.volkeralthaus.jsqllogger.logger.*;
...
LogConnection conn = null;
try {
Properties p = new Properties();
p.put("user", "sa");
p.put("password", "");
Class.forName("org.hsqldb.jdbcDriver").newInstance();
java.sql.Connection realConnection = java.sql.DriverManager.getConnection("jdbc:hsqldb:file:testdb", p);
conn = new de.volkeralthaus.jsqllogger.LogConnection(realConnection);
conn.addLogger(LoggerStatementLogger.getDefaultLogger());
conn.setLogtime(true);
} catch (Exception e) {
throw new Exception("Error on connecting to database", e);
}
...
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO test (id,name,ts) VALUES (?,?,?);");
pstmt.setInt(1, 10,);
pstmt.setString(2, "foo");
pstmt.setTimestamp(3, new java.sql.Timestamp(System.currentTimeMillis()));
pstmt.executeUpdate();
...
This creates the following output:
> 25.02.2006 16:47:33 de.volkeralthaus.jsqllogger.logger.LoggerStatementLogger logStatement > INFO: INSERT INTO test (id,name,ts) VALUES (1,'foo','2006-02-25 16:47:33.696000000') > 25.02.2006 16:47:34 de.volkeralthaus.jsqllogger.logger.LoggerStatementLogger logTime > INFO: Execution time: 00:00.016 (16 ms)
Every object which implements the
de.volkeralthaus.jsqllogger.logger.StatementLogger
interface can be used for output.
Two logger objects are included in this package:
LoggerStatementLogger for using the
Java logging API
java.util.logging.*. All messages
are printed on the Level INFO.
OutputStreamStatementLogger for
printing on OutputStream objects,
System.out for example. The static
method
OutputStreamStatementLogger#getDefaultLogger()
returns a preconfigured Logger on the
System.out stream.
PreparedStatement#setAsciiStream(int,
java.io.InputStream, int).
:wq!