File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatement" Watch "PreparedStatement" New topic
Author

PreparedStatement

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

PreparedStatement pstmt =
con.prepareStatement("select at_test.nextval into ? from dual");
How come i can use above, query ? I did setInt as below, where chartID is defined as 0.
// Setting an integer.
pstmt.setInt(1,chartID);
// Submit a query, creating a ResultSet object
ResultSet rs = pstmt.executeQuery();

chartID = rs.getInt(1);
At this immediate above line i got, NullPointerException.
So how come i can solve this problem ?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
That's not how you use a PreparedStatement. You can do something like this:
PreparedStatement pstmt = con.prepareStatement("UPDATE EMPLOYEES SET SALARY = ? WHERE ID = ?");
pstmt.setBigDecimal(1, 153833.00)
pstmt.setInt(2, 110592)
You can't do a "select into" the way you are using it. In fact, I can't quite figure what exactly you are trying to do.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Thomas,
That's a valid use of a PreparedStatement. Vikram is just indicating that he's going to pass in different values for the "where clause" each time he executes it.
There's a couple of things wrong with Vikram's example.
1. I'm not sure his SQL is valid a valid SQL statement, unless he's trying to implement a SELECT..INTO.
2. Assuming that there is an error in the SQL statement and he just wants to do a SELECT and pass in different values for the where clause, then he's missing one step after executing the query.

-Peter

[This message has been edited by Peter Tran (edited January 11, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Vikram is just indicating that he's going to pass in different values for the "where clause" each time he executes it.
Look again. Vikram's statement doesn't have a where clause. You are correct that he needs an rs.next() but that still won't help. Look at the statement. If chartID (an int) is 5 then he would generate a statement that looks like this: "select at_test.nextval into 5 from dual". That is not a valid SQL statement. But you can't do that anyway. The ? can only be the right hand assignment of a where clause.
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Thomas,
I am in total agreement with you. His SQL is invalid, but I was going on the assumption that if it was valid, that he can use a PreparedStatement to execute a "SELECT" SQL statement. Notice, my first point: "I'm not sure his SQL is valid a valid SQL statement, unless he's trying to implement a SELECT..INTO." Even if he was trying to do a select..into, his SQL is still wrong.
When I read your first post, you said "That's not how you use a PreparedStatement. You can do something like this:" I thought you meant you could only use a PreparedStatement with update/insert SQL only.
-Peter
Ps. We're both in agreement that Vikram SQL is incorrect, and he's missing the rs.next().
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The example I used happened to be from the Sun API. The rule is that the question mark can only be used as the right hand of a comparison in a where clause.
 
wood burning stoves
 
subject: PreparedStatement