0

having an error with the log in from html/jsp to servlet accessing database..

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
    com.DemoLogin.LoginServlet.service(LoginServlet.java:55)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

it says at line 55 tried to debug it but it still doesnt work here is my code

public class LoginServlet extends HttpServlet {
  Connection con;
  String user;
  String pass;

  @Override
  public void init(){
      try{
          Class.forName("com.mysql.jdbc.Driver");
          con = DriverManager.getConnection("jdbc:mysql:127.0.0.1","admin","");
      }catch(ClassNotFoundException | SQLException e){
          System.out.println("Error while loading connection"+ e);
      }           
  }

  @Override
  public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
      PrintWriter out = response.getWriter();
      try{
          user=request.getParameter("uname");
          pass=request.getParameter("pwd");
          String q="select * from employee where empname='"+user+"'";
          Statement st=con.createStatement();
          ResultSet rs=st.executeQuery(q);
          String username=null;
          String password=null;

          while(rs.next()){
              username=rs.getString(2);
              password=rs.getString(3);
          }

Line 55 contains the block of Statement st = con.createStatement() no errors with the codes yet I have no idea what the problem is

As you can see I took a reference from youtube and made some adjustments a lot of videos in youtube doesnt show a working sample for netbeans + wamp/ampache tomcat tutorials so I decide to ask for help here, hope someone can help me I'm making a Web Application for my thesis

  • 1
    That's a lot of code to read through. Please try breaking your problem into chunks and provide a specific question that we can help you with. Debug your code first and try and find where the problem is, then post a question. – Vedda Dec 17 '15 at 22:52
  • Please you use PreparedStatement instead of Statement for query parameter. – Gurkan Yesilyurt Dec 17 '15 at 23:06
  • I'm sorry I'm just new I'll put everything down the error is on the Statement I will try using PreparedStatement I thought they were just the same I only do self study since they don't teach everything in our school thanks for the guide tho – Mark Prince Lawrence Decinal Dec 17 '15 at 23:16
  • I tried using the preparedStatement it's still the same – Mark Prince Lawrence Decinal Dec 17 '15 at 23:27
  • I tried various combinations and here's my last one --- try{ user=request.getParameter("uname"); pass=request.getParameter("pwd"); pst=con.prepareStatement("select * from employee where empname='"+user+"'"); ResultSet rs=pst.executeQuery(); String username=null; String password=null; while(rs.next()){ username=rs.getString(2); password=rs.getString(3); } if(username.equals(user)&&password.equals(pass)){ ------and it stil doesnt work – Mark Prince Lawrence Decinal Dec 17 '15 at 23:54

1 Answers1

1

If you want to dynamic query, you should use PreparedStatement.

Please try it;

String sql = "select * from employee where empname=?";//question mark
user=request.getParameter("uname");
pass=request.getParameter("pwd");
try {
  PreparedStatement pStmt = con.prepareStatement(sql);
  pStmt.setString(1, user); // set first question mark
  ResultSet  rs = pStmt.executeQuery();
  String username=null;
  String password=null;
  if(rs.next()){
    username=rs.getString(2);
    password=rs.getString(3);
  }
}

See also Difference between Statement and PreparedStatement

Community
  • 1
  • 1
Gurkan Yesilyurt
  • 2,635
  • 2
  • 20
  • 21