我需要一种方法来查看从我的Java程序发送到PostgreSQL的查询。我正在使用c3p0
JDBC连接池来获取连接。当我在PreparedStatement上使用toString
方法时,它会给我
com.mchange.v2.c3p0.impl.NewProxyPreparedStatement@577b57a3
我的代码:
public void readData1() {
Connection con = null;
try
{
con = DatabaseFactory.getInstance().getConnection();
con.setAutoCommit(false);
java.sql.Timestamp ts= new java.sql.Timestamp(new java.util.Date().getTime());
String query= "INSERT INTO TEST_DATA values(?,?,?)";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setDate(1,new java.sql.Date(System.currentTimeMillis()));
stmt.setInt(2, 100);
stmt.setTimestamp(3,ts );
System.out.println(stmt.toString());
stmt.executeUpdate();
con.commit();
stmt.close();
}
catch (Exception e)
{
System.out.println("Could not read from DB, error: "+e.getMessage());
e.printStackTrace();
}
finally
{
try { con.close(); } catch (Exception e) {}
}
}
有人可以帮我解决如何从PreparedStatement获取SQL查询吗?
由于
答案 0 :(得分:4)
如果您使用的是c3p0-0.9.5的预发布版本,则可以尝试...
System.out.println(stmt.unwrap(PreparedStatement.class).toString());
可能你不是,所以你可以试试这个......
printInnerStatement( (C3P0ProxyStatement) stmt );
...与
import com.mchange.v2.c3p0.*;
static void printInnerStatement( C3P0ProxyStatement stmt )
{
java.lang.reflect.Method m = java.io.PrintStream.class.getMethod("println", new Class[]{Object.class});
stmt.rawStatementOperation(m, System.out, new Object[]{ C3P0ProxyStatement.RAW_STATEMENT });
}
要了解这里发生了什么(并修复了我不可避免的错误),请参阅Raw Connection and Statement Operations。