私はデータベースに接続し、クエリを実行してクエリを出力しようとしています。これまでのところ、私は作品を持っているが、私は私のクエリをスパイするためにlog4jdbcを使用していた出力を取得し、String
DerbyからPreparedStatementクエリを取得
public static void main(String args[]) {
BasicConfigurator.configure();
Logger.getGlobal().setLevel(Level.INFO);
PreparedStatement preparedStatement = null;
try {
connect();
String sql = "SELECT * FROM foo WHERE ID = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 1);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
break;
}
}
//String usedSql = "query should go here";
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
}
disconnect();
}
}
への特定の部分を割り当てる必要があります。私はにSELECT * FROM foo WHERE ID = 1
を割り当てる
594 [main] DEBUG jdbc.foo - 1. Connection.new Connection returned java.sql.DriverManager.getConnection(DriverManager.java:664)
608 [main] DEBUG jdbc.foo - 1. PreparedStatement.new PreparedStatement returned com.example.Test.main(Test.java:63)
608 [main] DEBUG jdbc.foo - 1. Connection.prepareStatement(SELECT * FROM foo WHERE ID = ?) returned [email protected] com.example.Test.main(Test.java:63)
608 [main] DEBUG jdbc.foo - 1. PreparedStatement.setInt(1, 1) returned com.example.Test.main(Test.java:64)
608 [main] DEBUG jdbc.foo - 1. PreparedStatement.setMaxRows(1) returned com.example.Test.main(Test.java:65)
609 [main] DEBUG jdbc.sqlonly - com.example.Test.main(Test.java:66)
1. SELECT * FROM foo WHERE ID = 1
:私はのような出力が記録されます瞬間
。これをやり遂げるにはどうすればいいですか?
'preparedStatement.toStringは()'あなたに何を与えるのでしょうか? – Berger
@Berger: 'net.sf.log4jdbc.PreparedStatementSpy @ 7d70d1b1' – Hooli
これは既に回答済みです[こちら](http://stackoverflow.com/questions/2382532/how-can-i-get-the-sql- – soufrk