Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with preapred Statement

 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pstmt = conn.prepareStatement(Queries.query[0]);


May be Queries.query[0] is returning null.
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope I am able to prepare the statement. But I get SQLException in getData(String Value) method
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok then the problem might be in order of calling PrepareQuery() and getData().
 
Maria Smith
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34384
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Damanjit Kaur
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic