File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
RabbitMQ in Depth
this week in the
JDBC and Relational Databases
Reusing PreparedStatement as Statement
Joined: Apr 19, 2002
Feb 15, 2007 10:10:00
With regular statement we can easily reuse the same Statement object to execute a series of unique queries/updates.
Statement stmt = con.createStatement(); stmt.executeQuery("sql1"); stmt.executeUpdate("sql2"); stmt.executeQuery("sql3"); ...
It is apparently not the case for PreparedStatement, as the creation requires a SQL
as argument, and there's no API to reset a PreparedStatement instance to a different SQL template.
But, is it possible to keep reusing PreparedStatement as if it as a Statement?
PreparedStatement stmt = con.prepareStatement("sql"); stmt.setXXX(); stmt.setXXX(); stmt.setXXX(); stmt.executeUpdate(); stmt.setXXX(); stmt.setXXX(); stmt.setXXX(); stmt.executeUpdate(); // Reusing PreparedStatement as statement stmt.executeQuery("sql1"); stmt.executeUpdate("sql2"); stmt.executeQuery("sql3"); ...
Joined: Oct 14, 2005
Feb 15, 2007 10:21:00
It seems to me you should be able to write a little
program to see if that code does what you expect it to do. Why not try that?
Joined: Apr 19, 2002
Feb 15, 2007 10:31:00
True. Thanks. It doesn't work. The PreparedStatement cannot be reused as general Statement.
In oracle the following error is caused on subsequent execute:
ORA-01006: bind variable does not exist
I agree. Here's the link:
subject: Reusing PreparedStatement as Statement
java.sql.SQLException: ORA-01858: a non-numeric character was found where a numeric was expected
Are there any problems using the same statement for multiple queries?
Can i use preparestatement's addBatch() to batch exec multiple sql statements?
jsp and jdbc
how to insert into 2 different tables
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015