0

I have a Java class which connects to an Oracle database and a JSP page, in which I am trying to call the function which connects to the database

I am getting a NullPointerException when the function is called.

My Java code:

public class DBConnection 
{
public Connection getConnection() throws Exception
  {   

 Connection con = null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");

      con = DriverManager.getConnection(the correct code);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        throw e;

    }
    catch (SQLException e) {
        e.printStackTrace();
        throw e;
    }
 }

} 

and my JSP page:

<% DBConnection db=new DBConnection(); 

db.getConnection(); 
 %>

Where am I going wrong?

5
  • did you add the necessary jars? Jun 12, 2014 at 10:16
  • @FlorescentTicker yes all the jars are added
    – amol
    Jun 12, 2014 at 10:18
  • 2
    you should not access database through jsp. Instead use servlets for that purpose. Post the stacktrace. Jun 12, 2014 at 10:22
  • 2
    Your function does not return anything! Put your real code please.
    – StephaneM
    Jun 12, 2014 at 12:05
  • 1
    Either give the real code or complete error trace
    – Devesh
    Jun 12, 2014 at 18:26

1 Answer 1

0

Use Class.forName("oracle.jdbc.OracleDriver"); instead. oracle.jdbc.driver.OracleDriver is deprecated starting from Oracle 9i.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.