有没有办法将preparedStatement打印到控制台System.out.println

时间:2012-12-07 14:16:06

标签: java jdbc

我需要一种方法来查看prepareStatement以进行调试这里是我希望将preparedStatement打印到控制台的java代码。

public class UnitTestDMUtility_Select {


@Test
public void testQUERY_CHECKBOTHPARTS() throws Exception{

    UnitTestHelper helper = new UnitTestHelper();
    Connection con = helper.getConnection(helper.sourceDBUrl);
    Connection conTarget = helper.getConnection(helper.targetDBUrl);


    PreparedStatement stmt = con.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
    stmt.setInt(1, 101);
    ResultSet sourceVal = stmt.executeQuery();
    //Here is the QUERY
    //select count(*) from tr_demand where demandtypeid=101
    stmt = conTarget.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
    stmt.setInt(1, 101);
    ResultSet targetVal = stmt.executeQuery();

    assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));

}

} 

2 个答案:

答案 0 :(得分:1)

对于查询调试,我通常使用库jamon,它是一个JDBC代理,用于记录每个语句被调用

答案 1 :(得分:1)

要回答您的问题,请执行以下操作,将查询输出到日志文件(和控制台):

PreparedStatement stmt = con.prepareStatement(DMUtility.QUERY_CHECKBOTHPARTS);
stmt.setInt(1, 101);

//Write stmt to console: 
System.out.println(stmt.toString());