mysql - Java - How to check an existing user in the database (for a signup interface)? -


so here's i've done far. connected database username primary key.

protected void processrequest(httpservletrequest request, httpservletresponse response)         throws servletexception, ioexception {     response.setcontenttype("text/html;charset=utf-8");     try {         accounts bean = new accounts();         bean.setusername(request.getparameter("uname"));         bean.setname(request.getparameter("fname"));         bean.setemail(request.getparameter("email"));         bean.setpassword(request.getparameter("password"));         accountsdao ao = new accountsdao();         ao.addaccount(bean);         response.sendredirect("stream.jsp");     }     finally{      } } 

what want is, when try register account same username/email of existing user in database, redirect another page using response.sendredireect("file.jsp"); require user register again username/email. thank in advance!

here accountsdao

public void addaccount(accounts bean) {     try {         bconnector = new dbconnector();         connection c = bconnector.getconnection();         string query = "insert account (username, name, email, password) values (?,?,?,?)";         preparedstatement preparedstatement = c.preparestatement(query);         preparedstatement.setstring(1, bean.getusername());         preparedstatement.setstring(2, bean.getname());         preparedstatement.setstring(3, bean.getemail());         preparedstatement.setstring(4, bean.getpassword());         preparedstatement.executeupdate();     } catch (sqlexception ex) {         logger.getlogger(accountsdao.class.getname()).log(level.severe, null, ex);     } } 

once have username primary not allow insert duplicates . try this

try { //query insert username user } 

if duplicate throw exception ora- handle exception using page redirect using catch block.

catch(exception e) { system.out.print(e); response.sendredirect("file.jsp"); } 

update

declare integer variable check whether have inserted value . change return type of method addaccount(accounts bean) int

public int addaccount(accounts bean) {      int count=0;      try {         bconnector = new dbconnector();         connection c = bconnector.getconnection();         string query = "insert account (username, name, email, password) values (?,?,?,?)";         preparedstatement preparedstatement = c.preparestatement(query);         preparedstatement.setstring(1, bean.getusername());         preparedstatement.setstring(2, bean.getname());         preparedstatement.setstring(3, bean.getemail());         preparedstatement.setstring(4, bean.getpassword());         count=preparedstatement.executeupdate();     } catch (sqlexception ex) {         logger.getlogger(accountsdao.class.getname()).log(level.severe, null, ex);     }  return count; } 

in servlet,

int count=0; try {         accounts bean = new accounts();         bean.setusername(request.getparameter("uname"));         bean.setname(request.getparameter("fname"));         bean.setemail(request.getparameter("email"));         bean.setpassword(request.getparameter("password"));         accountsdao ao = new accountsdao();         count=ao.addaccount(bean);         if(count>0){         response.sendredirect("stream.jsp"); \\success condition here         }         else{          response.sendredirect("file.jsp");   \\ failure condition here        }     }     catch(exception e) { system.out.print(e); } 

hope helps!


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -