I had been working with a MySQL database and a JDBC connection. In that case I could easily print a prepared statement using System.out.println(ps);
.
But for a SQL Server database it just prints 'SQLServerPreparedStatement' instead of the actual query that will be sent to the server after the parameters have been substituted into the SQL command. How can I accomplish that with SQL Server JDBC?I do not want to achieve that by manual coding like passing all parameters to a function and then bulding the query inside.I want to print it as soon as the query gets executed with effective parameters
Asked
Active
Viewed 2,180 times
1

JG1991
- 143
- 3
- 14
-
I need the latter one. The 'effective' SQL. This is how I registered my driver:Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn = DriverManager.getConnection(connectionStrSE, idSE, pwSE); – JG1991 Mar 28 '16 at 03:39
-
Possible duplicate of [How can I get the SQL of a PreparedStatement?](http://stackoverflow.com/questions/2382532/how-can-i-get-the-sql-of-a-preparedstatement) – Gord Thompson Mar 28 '16 at 13:08
1 Answers
1
You can't. From https://learn.microsoft.com/en-us/sql/connect/jdbc/reference/sqlserverpreparedstatement-class :
"SQLServerPreparedStatement prepares a statement by using the SQL Server sp_prepare stored procedure"
Therefore the final SQL isn't actually held by the JDBC driver.

Daz
- 2,833
- 2
- 23
- 30