obter consulta PreparedStatement do Derby
Estou tentando me conectar a um banco de dados, executar uma consulta e imprimi-la. Até agora, o que tenho funciona, mas preciso obter a saída e atribuir uma parte específica a umaString
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();
}
}
estou a usarlog4jdbc para espionar minhas consultas.
No momento eu recebo a saída registrada como:
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 net.sf.log4jdbc.PreparedStatementSpy@7d70d1b1 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
Eu quero atribuirSELECT * FROM foo WHERE ID = 1
parausedSql
. Como posso fazer isso?