• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with preapred Statement

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 346
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic