0

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);
    }

    }
3
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by mith_cool
0

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

PreparedStatement pstmt= connection.prepareStatement(sql);
0

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);
    }

    }
0

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

0

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

0

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

Edited by mith_cool: n/a

0

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

Edited by peter_budo: n/a

0
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.

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.