Discovering the SQL of a Resultset

Asked

Viewed 644 times

3

I have a Resultset java object that I don’t know the SQL/parameters that generated it. Would you like to know how to get the SQL used to create it? would have something like :

  rs.getStm.getSql

?

  • 1

    Your question is very confusing, you want something like this : timeStamp = rs.getTimestamp("data"); ? or wants to know which table generated the result ?

  • 1

    @Wellingtonavelino I think he wants to know which was the SQL which gave rise to its ResultSet

  • That’s right @Dener, I want Resultset’s SQL

3 answers

2

You should get SQL from java.sql.Preparedstatement:

System.out.println(ps);

Just call toString Preparestatement.

  • This works in contexts where the driver is not used JDBC mysql?

  • What is your driver? I have tested it now on mysql and I know it works on postgre as well.

  • =/ unfortunately in Jaybird does not work, I think this is an implementation depending on the driver and the intelligence of the developer who created the driver

  • In this one I did not test. But try to debug (with eclipse, netbeans, etc.). Maybe in debug you find some property that is saving the information you need (sql, in this case).

  • I tried to debug to see if I found something, but I found nothing

2

I’m writing this based on this javadoc.

For the javadoc, the class FirebirdResultSet inherits from the ResultSet library java.sql.

The class ResultSet has the method getStatement() that returns an object Statement; then you can follow the user’s reply @Afonso, and call the method toString class Statement.

  • in the case of Firebird the Firebirdstatement (returned by getStatement) is not implemented the toString returning those hachs/memory address( I never knew what it is that) naughties of when a class does not implement toString leaving the pattern

  • 1

    So my suggestion is to go into debug mode and search where the query is in Fbstatement, and then print the query.

0

Only complementing Lord Afonso. With Postgre works also. Only use the toString() method with Preparedstatement.

preparedStatement = conection.prepareStatement("SELECT * FROM usuario");
System.out.println(preparedStatement.toString());

Console:

SELECT * FROM usuario

With Statement common does not work because sql is inserted directly with the executaeQuery method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.