0

我需要一种查看preparedStatement 以进行调试的方法,这是我希望将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));

}

} 
4

2 回答 2

1

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

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

//Write stmt to console: 
System.out.println(stmt.toString());
于 2012-12-07T14:28:08.230 回答
1

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

于 2012-12-07T14:22:06.767 回答