Results 1 to 2 of 2
  1. #1
    mohsen.noor is offline Member
    Join Date
    Dec 2012
    Posts
    30
    Rep Power
    0

    Default Servlet get NullPointerException for DataBase Connection object

    Hello evryone
    I wrote the code which insert data to database and it work in console application
    But when I used in servlet I receive NullPointerException
    The result in browser:
    java.lang.NullPointerException
    database.PersonTable.insert(PersonTable.java:27)
    mytest.ServletTest.doGet(ServletTest.java:27)

    PersonTable.java
    Java Code:
            package database; 
            import java.sql.DriverManager;
            import java.sql.Connection;
            import java.sql.PreparedStatement; 
            import java.sql.SQLException;
            public class PersonTable {
                private static final String DB_DRIVER = "oracle.jdbc.driver.OracleDriver";
                private static final String DB_CONNECTION = "jdbc:oracle:thin:@172.16.78.200:1631:pnatest";
                private static final String serverAddress = "172.16.78.200";
                private static final String portMap = "1631";
                private static final String dbSid = "pnatest";
                String url  ="jdbc:oracle:thin:@"+serverAddress+":"+portMap+":"+dbSid;
                String id   ="test_1";
                String pass ="test_1";
            
            
                public int insert()throws SQLException  {
                    Connection dbConnection = null;
                    PreparedStatement preparedStatement = null;
                    String insertSQL = "INSERT INTO PERSON (ID,FNAME, LNAME) VALUES (?,?, ?)";
                    dbConnection = getDBConnection();
                    preparedStatement = dbConnection.prepareStatement(insertSQL);
                    int result = 0;
                    try {
            
            
                        preparedStatement.setString(1,"5");
                        preparedStatement.setString(2, "Raje");
                        preparedStatement.setString(3, "rock");
                        result = preparedStatement.executeUpdate();
                        preparedStatement.close();
                        dbConnection.close();
            
                    } catch (SQLException e) {
            
                        System.out.println(e.getMessage());
            
                    } finally {
            
                        if (preparedStatement != null) {
                            preparedStatement.close();
                        }
            
                        if (dbConnection != null) {
                            dbConnection.close();
                        }
            
                    }
                    return  result;
                }
            private  Connection getDBConnection() {
            
                    Connection dbConnection = null;
            
                    try {
            
                        Class.forName(DB_DRIVER);
            
                    } catch (ClassNotFoundException e) {
            
                        e.printStackTrace();
            
                    }
            
                    try {
            
                        dbConnection = DriverManager.getConnection(DB_CONNECTION, id, pass);
            
                        } catch (SQLException e) {
            
                        System.out.print("here");
                        e.printStackTrace();
            
                    }
                    return dbConnection;
            
                }
    Servlet Code:doGet Method


    Java Code:
         protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                PersonTable pt = new PersonTable();
                try {
                        pt.insert();
                    } catch (SQLException sql){
                        sql.printStackTrace();
                      }
    Thanks in advance

  2. #2
    mohsen.noor is offline Member
    Join Date
    Dec 2012
    Posts
    30
    Rep Power
    0

    Default Re: Servlet get NullPointerException for DataBase Connection object

    I find it
    In tomcat library I didnot define jdbc for database connectivty
    thanks

Similar Threads

  1. Replies: 1
    Last Post: 11-14-2013, 10:29 AM
  2. SSL connection from the servlet to the server
    By RealWotan in forum Java Servlet
    Replies: 1
    Last Post: 12-11-2012, 06:32 PM
  3. servlet,connection,bean
    By tanu in forum New To Java
    Replies: 2
    Last Post: 01-13-2012, 01:47 PM
  4. NullPointerException in Servlet
    By techsing14 in forum Java Servlet
    Replies: 4
    Last Post: 05-31-2011, 10:25 AM
  5. Servlet to Servlet connection through URLConnection
    By bsrjavaforum in forum Networking
    Replies: 0
    Last Post: 04-01-2009, 04:12 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •