• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Database connection problem in JSP application

 
catrine ohm
Greenhorn
Posts: 13
IBM DB2 Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have an jsp application and I have used MySQL for database, Apache for application server. I have problem about jdbc connection. "select" queries sometimes work but sometimes don't.I take null pointer exception error in jsp page because of poor connection of database.When I run select queries in cpanel many times, it works successfully 0.0022ms but in application, sometimes I get null pointer exception.And I don't know why?what I am doing wrong here?

 
Sresh Rangi
Ranch Hand
Posts: 52
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A NullPointerException is thrown because something is null when it shouldn't be, so first you need to find out what's null. What's the stacktrace of the exception? Also look for any exceptions that are thrown before the NullPointerException. Be careful with methods that can return null (your call to selectQuery for example can throw a NullPointerException).
 
catrine ohm
Greenhorn
Posts: 13
IBM DB2 Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sresh Rangi wrote:A NullPointerException is thrown because something is null when it shouldn't be, so first you need to find out what's null.
What's the stacktrace of the exception? Also look for any exceptions that are thrown before the NullPointerException.
Be careful with methods that can return null (your call to selectQuery for example can throw a NullPointerException).


The problem is that database connection problem I think.The connection close before it reaches last record and then connection parameter is null (but is not null initially). For example it has 6 records but when I open jsp page 5 records list and then I get null pointer exception. Also this problem does not occur everytime.

 
Sresh Rangi
Ranch Hand
Posts: 52
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you trace the null values back to the cause? Maybe once of the "return null" statements in dbConnect.

One problem is that selectQuery is calling dbConnect twice, but only one of these connections is closed. You should only need to call dbConnect once in that method. Every successful call to DriverManager.getConnection needs a corresponding call to close.
 
Paul Clapham
Sheriff
Pie
Posts: 20978
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sresh Rangi wrote:One problem is that selectQuery is calling dbConnect twice...


Yes, you are pointing at a problem with the "dbConnect" method. And that problem is, that it doesn't do just one thing, like a properly-designed method should. Instead it does two things: it creates a connection and it returns a Statement based on that connection. It should be broken into two methods, which do each of those two things separately. This would make writing code based on those methods easier.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic