I got a jsp page which forwards request of two textfields txtname and txtpass to a servlet and the servlet then inserts the data into sql db and the name of the db is online_store. The db has two columns. When i checked the values are passing to the servlet post method But for some reason am getting null pointer exception. Help me out

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
             PrintWriter out = response.getWriter();
        String name= request.getParameter("txtname");
        String pass= request.getParameter("txtpass");
        Connection connection=null;
        ResultSet rs;
        if(name !=null && pass!=null)
        try{

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String connectionurl= "jdbc:sqlserver://MITH-PC; database=Online_Store; user=sa; password=mith1234";
            String sql="Insert into login values(?,?)";
            PreparedStatement pstmt= connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
            int no= pstmt.executeUpdate();
            pstmt.close();
            }


        catch(ClassNotFoundException e){
      out.println("problem with the driver: " + e.getMessage());
    }
    catch(SQLException e){
      out.println("SQLException caught: " + e.getMessage());
    }
    catch (Exception e){
      out.println(e);
    }

    }

Not that helpful.. Please can you fix my code??? the problem is in this line

PreparedStatement pstmt= connection.prepareStatement(sql);

I have altered the code and got fixed the problem but now am getting an exception as
SQLException caught: No suitable driver found for jdbc:sqlserver://localhost:45316; database=Online_Store; user=sa; password=mith1234

here is my code

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
             PrintWriter out = response.getWriter();
             
        String name= request.getParameter("txtname");
        String pass= request.getParameter("txtpass");
        Connection connection=null;
        ResultSet rs;
        if(name !=null && pass!=null)
        try{


            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String connectionurl= "jdbc:sqlserver://localhost:45316; database=Online_Store; user=sa; password=mith1234";
            connection = DriverManager.getConnection(connectionurl);
            String sql="INSERT INTO login VALUES(?,?)";
            PreparedStatement pstmt= connection.prepareStatement(sql);
            pstmt.setString(1, name);
            pstmt.setString(2, pass);
            pstmt.executeUpdate();
            pstmt.close();
            }


        catch(ClassNotFoundException e){
      out.println("Couldn't load database driver: " + e.getMessage());
    }
    catch(SQLException e){
      out.println("SQLException caught: " + e.getMessage());
    }
    catch (Exception e){
      out.println(e);
    }

    }

also i want to ask from where i can get that driver and where should i keep it in my hard disk????

What database you using? Driver is usually better to keep together with project so that in case you try to deploy it on different pc you do not have to go in search for drivers. Drivers are usually kept inside PROJECT_NAME/WEB-INF/lib directory, but sometimes these can be also kept in Tomcat/lib directory. However avoid having same driver in both directories as this can produce strange error messages

I am using sqlserver as databse and netbeans as java programmming software

Here is latest Microsoft documentation in regards of driver which includes link where to "Get it" (includes installation details) and also "Beginner's guide"

protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String name= request.getParameter("txtname");
String pass= request.getParameter("txtpass");
Connection connection=null;

ResultSet rs;

if(name !=null && pass!=null)

try{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String connectionurl= "jdbc:sqlserver://localhost:45316; database=Online_Store; user=sa; password=mith1234";
connection = DriverManager.getConnection(connectionurl);
Statement stat=connection.createStatement();
}
catch(SQLException e)
{
     System.out.println("Message:"+e.getMessage);
String sql=("insert into login values('"+name+"','"+pass+"')");
stat.executeUpdate();
connection.close
}

//Adding Drivers

1) Each and every database has got its own driver
2) Download the driver for your respected database
3) If you are working with netbeans right click on the project click Buildpath
4) Select Addexternal Jars and Browse for the path where you have the jar files or libraries.
5) One more thing is you have to paste the jar file in your server also go to the server folder
6) open bin and click lib and past all the jar or libraries.


This is how you have to import all the jar files.

Thank you for the help guys. The problem is fixed now. :D

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.