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 Problem with preapred Statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with preapred Statement" Watch "Problem with preapred Statement" New topic
Author

Problem with preapred Statement

Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
I am writing a batch program and I have Prepared Statement class where I would like to prepare all the queries before I start processing the files. But the problem is When I try to execute the Prepared statement, it becomes null . For example it is null in getData(String value) method of below code snippet.



Thanks
Maria

[edited to add code tags]
[ February 03, 2005: Message edited by: Jeanne Boyarsky ]
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
pstmt = conn.prepareStatement(Queries.query[0]);


May be Queries.query[0] is returning null.
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
Nope I am able to prepare the statement. But I get SQLException in getData(String Value) method
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
SQLException in getData(String Value) method


If you are getting sql exception then its relating to sql statement in query. If the query is update then check for all columns values in sql statement. I mean for not null values there has to be some values and in case of select statement whether the column names are correct in relation to column name in table.
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
I am sorry I am getting java.lang.NullPointerException. And I am sure there is no problem with prepared statement. Because
if I exceute Prepare statement in the constructor itself I don't get any error message.
But if I try to excute statement later after some processing is done, as I metioned aboe I get null pointer exception.
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
Ok then the problem might be in order of calling PrepareQuery() and getData().
Maria Smith
Ranch Hand

Joined: Apr 01, 2004
Posts: 40
Thank you for your help
I changed my class a bit. I am preparing statements in a method instead of a costructor
I call the method Test() from my driver class to prepare the Statment.
and I am calling getData(String value) method later from different program.

Do you know how can i hold the connection after preparing the statement. Because
for some reason connection becomes null and it is causing the problem



[edited to add code tags]
[ February 03, 2005: Message edited by: Jeanne Boyarsky ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30138
    
150

Maria,
The connection object should not be null (unless you are assigning null to it somewhere.) Try searching the full code for "connection =".

Also, I added UBB code tags to your posts. It makes code easier to read. For future reference, the UBB tags are right below the editor on the edit screen.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
conn = sqlManager.getConnection();


Where have you declared conn variable. I suppose its declared as class variable. Then better way would be to instantiate it in constructor and not close it in other methods. or open and close it in each method which have to use the Connection but that way would be better in case of connection pooling.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with preapred Statement
 
Similar Threads
Resultset Problem ,Please help
Problem in bmp entity bean while running client?
problem to create instance
Database Connection
howto modify database structure ?