java - Servlet - MySQL Error -


i'm trying enter data mysql database following code. appreciated can't work out why there exception. request parameters supplied html form , there don't appear issues @ all, data gets through fine. issue seems occurring after servlet done , somewhere in dao or connection manager. in advance!

relevant servlet code:

            userregistrationbean user = new userregistrationbean();             user.setusername(request.getparameter("username"));             user.setpassword(request.getparameter("password"));             user.setemail(request.getparameter("email"));              user = userdao.register(user);              if (user.getexists() == true) {                  errormessage = "the user name entered has been registered!";                  request.setattribute("errormessage", errormessage);                  request.getrequestdispatcher("/web-inf/register.jsp").forward(request, response);                  return;              } 

userdao:

public static userregistrationbean register(userregistrationbean bean) {      statement stmt = null;          string username = bean.getusername();         string password = bean.getpassword();       string email = bean.getemail();      string searchquery = "select * tblusers username='"                         + username                         + "'";      string insertquery = "insert tblusers (username, password, email) values ('"                          + username +                          "', '"                          + password +                          "', '"                          + email +                         "')";      try {          currentcon = connectionmanager.getconnection();         stmt = currentcon.createstatement();          //check if user exists          resultset searchrs = stmt.executequery(searchquery);          //user name available         if (searchrs.next() == false) {              bean.setexists(false);          }           //user name not available         else if (searchrs.next() == true) {              bean.setexists(true);          }          if (bean.getexists() == true) {              //return error , prevent registration              bean.setsuccess(false);              return bean;          }          else {              stmt.executeupdate(insertquery);              bean.setsuccess(true);          }      }      catch(exception ex) {          //exception available here      }      return bean;  } 

connectionmanager:

public class connectionmanager {      static connection connection;     static string url;      public static connection getconnection() {          try {              string url = "jdbc:odbc:" + "localhost:3306";               class.forname("com.mysql.jdbc.driver");              try {              connection = drivermanager                       .getconnection("jdbc:mysql://localhost:3306/the_atrium_beauty?"                           + "user=system&password=system~9");          }              catch (sqlexception ex) {                  //stack trace available here                 //ex.printstacktrace();              }          }          catch(classnotfoundexception e) {              //exception check available here             //system.out.println(e);          }          return connection;      } } 

try catch block:

catch(throwable exception) {              string errormessage = exception.getmessage();             throwable errorcause = exception.getcause();             string errorlocation = this.getservletname();              request.setattribute("errormessage", errormessage);             request.setattribute("errorcause", errorcause);             request.setattribute("errorlocation", errorlocation);              request.getrequestdispatcher("/web-inf/errordisplay.jsp").forward(request, response);          } 

error display jsp:

<body>      <% final string errormessage = (string)request.getattribute("errormessage"); %>     <% final throwable errorcause = (throwable)request.getattribute("errorcause"); %>     <% final string errorlocation = (string)request.getattribute("errorlocation"); %>      <h1>an error occurred...</h1>      <p>          <%= errormessage %><br><br>          <%= errorcause %><br><br>          <%= errorlocation %>      </p>  </body> 

stack trace:

java.lang.nullpointerexception     @ com.atrium.userservlets.userregistrationservlet.dopost(userregistrationservlet.java:231)     @ javax.servlet.http.httpservlet.service(httpservlet.java:647)     @ javax.servlet.http.httpservlet.service(httpservlet.java:728)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:305)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.tomcat.websocket.server.wsfilter.dofilter(wsfilter.java:51)     @ org.apache.catalina.core.applicationfilterchain.internaldofilter(applicationfilterchain.java:243)     @ org.apache.catalina.core.applicationfilterchain.dofilter(applicationfilterchain.java:210)     @ org.apache.catalina.core.standardwrappervalve.invoke(standardwrappervalve.java:222)     @ org.apache.catalina.core.standardcontextvalve.invoke(standardcontextvalve.java:123)     @ org.apache.catalina.authenticator.authenticatorbase.invoke(authenticatorbase.java:502)     @ org.apache.catalina.core.standardhostvalve.invoke(standardhostvalve.java:171)     @ org.apache.catalina.valves.errorreportvalve.invoke(errorreportvalve.java:100)     @ org.apache.catalina.valves.accesslogvalve.invoke(accesslogvalve.java:953)     @ org.apache.catalina.core.standardenginevalve.invoke(standardenginevalve.java:118)     @ org.apache.catalina.connector.coyoteadapter.service(coyoteadapter.java:408)     @ org.apache.coyote.http11.abstracthttp11processor.process(abstracthttp11processor.java:1041)     @ org.apache.coyote.abstractprotocol$abstractconnectionhandler.process(abstractprotocol.java:603)     @ org.apache.tomcat.util.net.jioendpoint$socketprocessor.run(jioendpoint.java:310)     @ java.util.concurrent.threadpoolexecutor.runworker(unknown source)     @ java.util.concurrent.threadpoolexecutor$worker.run(unknown source)     @ java.lang.thread.run(unknown source) 

i think problem lies here (condensed code readability):

resultset searchrs = stmt.executequery(searchquery);  //user name available if (searchrs.next() == false) {     bean.setexists(false); }   //user name not available else if (searchrs.next() == true) {     bean.setexists(true); }  if (bean.getexists() == true) {     //return error , prevent registration     bean.setsuccess(false);     return bean; } 

next() moves resultset's internal cursor on next row. implication of subsequent calls next() not guaranteed return same result.

so this:

if (searchrs.next() == false) { 

might not return same thing few lines later:

else if (searchrs.next() == true) { 

this means there potential pathway through code neither condition ever true. specifically, if first invokation of next() returns true , second invokation returns false, neither branch execute.

there's whole bunch of approaches fixing this. this:

// check true condition first, no need == true if (searchrs.next()) {     // user exists     bean.setexists(true);     bean.setsuccess(false);      // returning bean object isn't strictly necessary because of side-effects     return bean; } else {     // user doesn't exist     bean.setexists(false);     stmt.executeupdate(insertquery);      // you'll need commit transaction work properly:     currentcon.commit();     bean.setsuccess(true);      return bean; } 

essentially, because boolean comparisons in context can only ever have 2 outcomes, can use if {} else {} instead of if {} else if {}.

note: assuming return type of getexists() boolean instead of boolean (because boolean can't null). in mind, believe you're not initialising variable sits behind getexists() because of problem above, it's still set @ default value, objects null.

because changes bean side effects, don't need return method, changes object should visible outside of method well.


for exception handling (very) useful if didn't swallow without reporting it. presumably calling method needs know unusual has gone wrong, rather catch , nothing, instead:

try {     // database access code here } catch (sqlexception e) {     // don't want persist dud data, rollback connection     currentcon.rollback();      // propagates exception upward calling method     throw new registrationexception("error registering user", e); } 

where registrationexception custom exception class create (you should define @ least constructor takes string , throwable arguments). have modify method signature declare exception well:

public static userregistrationbean register(userregistrationbean bean)         throws registrationexception { 

this forces calling method either re-throw (thus further propagating) exception or handle it.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -