File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please Help! I Have Worked On IT For Days.

 
JiaPei Jen
Ranch Hand
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been struggling with this problem for days.
I am using (Tomcat) connection pool to access database. I am sure that the pool is configured correctly and DataSource is properly referenced in the web.xml. I am positive about it because I have run the CPServlet.java. And I am able to retrieve data from database and print the data out.
CPServlet.java:

Problem occurs when I try to split CPServlet.java into a connection bean (DBConnection.java) and pass Connection object to a business class (VerificationServlet.java). I did not get any error message. What I got is a blank page (no datum was written out). I am lost.
DBConnection.java

VerificationServlet.java

Moreover, I do not see anything in the log messages that points to problems. Please help.
[ July 15, 2003: Message edited by: JiaPei Jen ]
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your getDBConnection() method, it looks as if you are opening a new connection and then closing it within the finally block before returning the reference, although this would seem to be null. I think you're getting a NullPointerException when you try conn.createStatement(). Try sending the output to STDOUT instead of STDERR, just in case your servlet container is swallowing this stuff somewhere (perhaps in a log file?).
 
Craig Jackson
Ranch Hand
Posts: 405
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I would have to agree. It appears you are setting the connection reference to null in your finally clause. You should be receiving a null pointer exception.
Craig
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How should I change the program so that the reference to the Connection object is not "null"? (i.e. Currently, I have
Statment stmt = null.createStatement
although I do not have the NullPointer Exception error message.)
When I have InitialContext, DataSource, Connection, Statement, ResultSet in the same Servlet, I am able to retrieve data from database via the configured connection pool.
Please help!
 
Ron Perich
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't close your connection in the getDBConnection() method. Instead, pass the open connection back to your servlet and let it do its thing. Of Course, don't forget to close your connection in the servlet. While the Garbage Collector will close it upon running, there is no guarantee as to when it gets collected and you don't want the connection sitting open for any longer than you need it.
 
Craig Jackson
Ranch Hand
Posts: 405
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right.
The one thing you don't wont to do is close the connection and then set it to null before you send it back to the calling program. That is what is happening everytime you get a good connection.

Closing of the connection should be handled elsewhere.
 
JiaPei Jen
Ranch Hand
Posts: 1309
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your attention and help. I took out:
conn.close(); and conn = null;
in the DBConnection.java. My program works fine now.
[ July 16, 2003: Message edited by: JiaPei Jen ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic