aspose file tools*
The moose likes JDBC and the fly likes Database connection problem in JSP application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Database connection problem in JSP application" Watch "Database connection problem in JSP application" New topic
Author

Database connection problem in JSP application

catrine ohm
Greenhorn

Joined: Mar 07, 2012
Posts: 13

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

Joined: Nov 28, 2012
Posts: 39
    
    1
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

Joined: Mar 07, 2012
Posts: 13

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

Joined: Nov 28, 2012
Posts: 39
    
    1
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
Bartender

Joined: Oct 14, 2005
Posts: 18124
    
    8

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.
 
Don't get me started about those stupid light bulbs.
 
subject: Database connection problem in JSP application
 
Similar Threads
writing the user defined function in servlet
Setting Location for database to be used by javadb
Error when deploying an EAR on JBoss 5.1.0
cant connect to mysql
jasperreports does not work in executable jar file