Hi ,

I am usinga code to connect to database. here is the code--

<html>
<head>
<title>Obtaining a Connection</title>
</head>
<body>
<h1>This Page Obtains a Connection to a Database and executes a query</h1>
<%
    Connection conn = null;
    ResultSet result = null;
    Statement stmt = null;


    try {
      Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch (Exception e) {
      System.out.println("Error occurred " + e);
     }
     try {
       conn = DriverManager.getConnection("jdbc:odbc:namish") ;
     }
     catch (SQLException e) {
        System.out.println("Error occurred " + e);
     }
     try {
        stmt = conn.createStatement();
        result = stmt.executeQuery("SELECT * FROM ARUSER; ");
     }
     catch (SQLException e) {
         System.out.println("Error occurred " + e);
      }

%>

when i am trying to run this i am getting null pointer exception error.
can you please help me in rectifying this problem. I have created a DSN name namish and using that.it is the right way to do it or any other way i should proceed..

Thanks
NT

Recommended Answers

All 3 Replies

Do not connect to databases directly from within a JSP.
Please check out this tutorial for how exactly your web application code must be organized.

Plus another suggestion, all your code should be in one try block rather than multiple try / catch blocks. Cause in the current situation even though your driver loading ( Class.forName("...") ) may fail, your code would still try to proceed with making a database connection which is incorrect (similarly when it cant get a database connection, it would still proceed to try to get a Statement from it using conn.createStatement(); which is also plain wrong.

Also from your code I can get a clear hint that you are new to JDBC so refer to this tutorial first and practice database connectivity in plain old Java and then jump to JSPs.

Hi ,

I am usinga code to connect to database. here is the code--

<html>
<head>
<title>Obtaining a Connection</title>
</head>
<body>
<h1>This Page Obtains a Connection to a Database and executes a query</h1>
<%
    Connection conn = null;
    ResultSet result = null;
    Statement stmt = null;


    try {
      Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch (Exception e) {
      System.out.println("Error occurred " + e);
     }
     try {
       conn = DriverManager.getConnection("jdbc:odbc:namish") ;
     }
     catch (SQLException e) {
        System.out.println("Error occurred " + e);
     }
     try {
        stmt = conn.createStatement();
        result = stmt.executeQuery("SELECT * FROM ARUSER; ");
     }
     catch (SQLException e) {
         System.out.println("Error occurred " + e);
      }

%>

when i am trying to run this i am getting null pointer exception error.
can you please help me in rectifying this problem. I have created a DSN name namish and using that.it is the right way to do it or any other way i should proceed..

Thanks
NT

hi,
from your page one of the feild is going empty to database and database is not able to read the input thats why you are getting null pointer exception i think.Just check it...

First of all do what stephen84s has suggested, don't put java code and logic in jsp. Only code that displays results.

And you should use ONE big try-catch and put everything inside. The idea with try is that you don't put only the line that has the exception, but also everything that you don't want executed if the exception happens:

If you get an error here: Class c = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Then none of the following should be executed:

conn = DriverManager.getConnection("jdbc:odbc:namish") ;
        stmt = conn.createStatement();
        result = stmt.executeQuery("SELECT * FROM ARUSER; ");

Also put the above in a separate class that returns the 'data' of the ResultSet

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.