2

i have been trying to obtain the query that is being executed when running the executeBatch() in the jdbc. When using, the executeQuery(), i can obtain the correct query using originalSql variable or sql (implemented from the Statement interface), but when i print the originalSql variable for executeBatch() it prints only the original sql as the name itself, with all the ? marks.

String insertTableSQL = "INSERT INTO Employees"
                + "(id, age, first, last) VALUES"
                + "(?,?,?,?)";
PreparedStatement preparedStatement = conn.prepareStatement(insertTableSQL);

        preparedStatement.setInt(1, 101);
        preparedStatement.setInt(2, 67);
        preparedStatement.setString(3, "niky");
        preparedStatement.setString(4, "baa");
        preparedStatement.addBatch();

        int[] rs2 = preparedStatement.executeBatch();

I have been going through the code of the executeBatch() as well. But still i am unable to find the exact location where it fill in the variables with input values. I am stuck in this for days.

Some help on this would be really appreciated. (If there is some method that i called by all three methods, executeQuery, executeBatch and executeUpdate, it would make my work easier)

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
udani
  • 1,243
  • 2
  • 11
  • 33
  • Thanks for the link, that really address what i was looking for. It looks like i won't be able to find such a method by going through the code then.... (i wish i came across that thread before....) – udani Sep 10 '15 at 09:49

1 Answers1

3

It never does. A prepared statement means that the query is sent to the database first without any parameter values. The parameters are then sent separately. This is done so that you can send many similar queries without incurring overhead in the database every time.

You can read more about prepared statements on wikipedia

Petter
  • 4,053
  • 27
  • 33
  • Thanks, but there should be a some place where it fills in those parameter values in order to run the query and do whatever it says in the query? because the object this, shows the entire query with parameters filled into correct places. I just want to find that location where i does that? Will there be any way to find that method? – udani Sep 10 '15 at 09:36
  • @ransi That place is at the server, after it has received the statement and the parameters. – user207421 Sep 10 '15 at 09:47
  • Although your answer is generally true for JDBC drivers, the default for MySQL Connector/J is to rewrite the statement with the (escaped) parameter values as literals on execution. You need to explicitly enable server side prepared statements to get the behavior you describe. – Mark Rotteveel Sep 10 '15 at 13:41