This question already has answers here:
Get query from java.sql.PreparedStatement [duplicate]
(8 answers)
Closed 9 years ago.
When we create a PreparedStatement we use '?' chars to then be replaced by setted parameters.
How can we see the final SQL string after these parameters are set?
There is no final SQL string, the version with placeholders is what is actually sent to server. The data is sent completely separately from it as you execute queries on the prepared statement.
You can log the string with placeholders, and then each dataset individually.
Your code could combine them in the log to an actual SQL string if that's what you want:
String query = "SELECT a FROM b WHERE c = ?";
...
pstmt.setString(1, "asd");
logSQL( query, "asd");
logSQL would then actually log "SELECT a FROM b WHERE c = 'asd'". Could be that someone has actually implemented this before...
The easiest way (that works with any JDBC driver) is to use log4jdbc. It's a proxy that wraps the driver, creates a readable SQL string by combining the SQL and its parameters and logs it, then passes the SQL and parameters on to the underlying driver.
Related
I'm having issues dealing with the single quote while using it in a prepared statement in JAVA via Oracle JDBC.
Let's say we have a table Restaurant with a column restaurant_name with 1 value : Jack's Deli
I want to use a simple prepared statement query like this:
String result = "Jack\'\'s Deli"
String sqlStatement = "select * from Restaurant where restauraunt_name like ? escape '\\' ";
PreparedStatement pStmt = conn.prepareStatement(sqlStatement);
pstmt.setString(1, result);
The result shows 0 returned values, however when I directly search the query in the database (ORACLE) it works fine and retrieves the result. (Oracle uses two single quotes as an escape for the first)
I am thinking that the value is not being passed properly to the database. Or there is some other formatting issue.
The point of prepared statements is that you don't need any escaping.
.setString(1, "Jack's Deli") will get it done.
This question already has answers here:
preparedStatement syntax error
(2 answers)
Closed 4 years ago.
I have written a prepared statement but its giving a syntax error at ?. I am not able to understand whats wrong.It should pass a movie name and get the result as directors of that movie
stmt=getConnection().createStatement();
String sql="SELECT directors FROM moviedata WHERE moviedata.title = ?";
PreparedStatement preparedStatement=conn.prepareStatement(sql);
preparedStatement.setString(1,movieName);
rs=preparedStatement.executeQuery(sql);
The problem is here:
rs=preparedStatement.executeQuery(sql);
You shouldn't pass the SQL String to executeQuery(), since the prepared statement already contains the SQL String with the ? placeholder replaced by the value of movieName.
Use:
rs=preparedStatement.executeQuery();
The problem is not in using Preparedstatement >Because the prepared statement object holds the sql query. The why again unnecessarily passing Sql string to executequery() method.PreparedStatement preparedstatement=conn.PrepareStatement(sql). This holds the sql query and when executequery() method is called The query is evaluated.
This question already has answers here:
Execute "sp_msforeachdb" in a Java application
(3 answers)
Closed 1 year ago.
I am executing the following query from Microsoft SQL Server Studio, which works fine and displays results:
SELECT *
INTO #temp_table
FROM md_criteria_join
WHERE user_name = 'tecgaw'
UPDATE #temp_table
SET user_name = 'tec'
WHERE user_name != 'tec'
SELECT *
FROM md_criteria_join
WHERE user_name = 'tec'
AND view_name NOT IN (SELECT view_name
FROM md_criteria_join
WHERE user_name = 'tecgaw')
UNION
SELECT *
FROM #temp_table
ORDER BY view_name,
user_name,
crit_usage_seq,
crit_join_seq
However, if I execute the same query in Java, an Exception is thrown stating
The statement did not return a result set.
Here's the Java code:
statement = conn.getConnection().createStatement();
resultSet = stmt.executeQuery(sql.toString());
Is that because I cannot do multiple SQL queries in one statement (I.e., Creating the #temp_table, updating it, and then using for it my select statement)?
JDBC is getting confused by row counts.
You need to use SET NOCOUNT ON.
Use execute statement for data manipulation like insert, update and delete and
executeQuery for data retrieval like select
I suggest you to separate your program into two statements one execute and one executeQuery.
If you do not wish to do that, try separating the statements with semi-colon. But I am not sure about this action if this gives you a resultset or not.
I have found similar question in StackOverflow here. You should enable connection to support multiple statements and separate them using ;. For concrete examples see that answer. However it is for MySql only.
Also I think you can rewrite your SQL into single query
SELECT columnA, columnB, 'tec' as user_name from md_criteria_join
WHERE (
user_name = 'tec'
AND view_name NOT IN (
SELECT view_name
FROM md_criteria_join
WHERE user_name = 'tecgaw')
)
OR user_name = 'tecgaw'
ORDER BY view_name, user_name, crit_usage_seq, crit_join_seq
Another option is to move your statements to stored procedure and ivoke it from JDBC using CallableStatement
Or maybe you should try executing it with multiple jdbc statements like this
Connection conn = conn.getConnection(); //just to make sure its on single connection
conn.createStatement("SELECT INTO #temp_table").executeUpdate();
conn.createStatement("UPDATE #temp_table").executeUpdate();
conn.createStatement("SELECT ...").executeQuery();
Note you have to close resources and maybe for better performance you could use addBatch and executeBatch methods
in ms sql you also have to do set nocount on right at the beginning of the stored procedure along with terminating select / update/ insert block statement with ";"
This question already has answers here:
Passing parameter to Cassandra CQL query using DataStax client
(2 answers)
Closed 9 years ago.
I have Java code to access and process data in Cassandra. How do I pass a Java variable to Cassandra CQL query written in Java. Here is the code:
My code goes like this:
itemname="Item01";
com.datastax.driver.core.PreparedStatement result =
session.execute("select itemname from demodb.retail_transaction where itemnamw = itemname;");
But it gives the following error:
no viable alternative at input ';'
i think you should bind your variables to the prepared statement like this:
itemname="Item01";
itemtype="type01";
com.datastax.driver.core.PreparedStatement result =
session.execute("select itemname from demodb.retail_transaction where itemnamw = ? and
itemtype =?;");
BoundStatement boundStatement = new BoundStatement(statement);
ResultSet results = session.execute(boundStatement.bind(
itemname,itemtype));
This question already has answers here:
Get query from java.sql.PreparedStatement [duplicate]
(8 answers)
Closed 10 years ago.
EDIT: I am using Oracle
I am Writing a dynamic query using PreparedStatement which goes something like this
String query="UPDATE <tablename> SET
column1=?,column2=?,.....,coulmn7=? WHERE "
+ "column5=TO_DATE(?,'DD/MM/YYYY') AND "
+ "column6=? AND "
....
+ "column7=?";
PreparedStatement prest2=con2.PrepareThisStatement(query);
while(true){
//loop through and bind values
if(end of list) break;
}
prest2.executeUpdate(query);
I need to keep the track of the queries that get fired at the database and log them in a table.
But I cannot get the entire query when using the PreparedStatement, nor can I store the values in a separate field. Is there a way to get the final query that was fired at the database.
You Can, using 1prest2.toString();