How to make login code in java -


i'm trying make login code in java there's problem in code. if enter correct data or wrong 1 still can't enter loop go next frame. full code it's have exception it's not problem .

connection conn = null ; try  {     conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/db?"+                           "user=name&password=pass&characterencoding=utf8");      string query = ("select user_name user user_name = '" + txtusername + "'                     , password = '" + passwordfield.getpassword().tostring() + "' ; ");      preparedstatement stmt = conn.preparestatement(query);     resultset rs = stmt.executequery();     while (rs.wasnull()) {         system.out.println("true");         frame2.setvisible(true);         frame.setvisible(false);         break;     } } catch (sqlexception ee) {     joptionpane.showmessagedialog(frame2, "wrong inf ... please try again "); } 

i try still not working.

connection conn  = null ; try  {     conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/db?"+                         "user=name&password=pass&characterencoding=utf8");      preparedstatement stmt = null;     string query = "select user_name user user_name =? , password=?";     stmt = conn.preparestatement(query);     stmt.setstring(1, txtusername.gettext());     stmt.setstring(2, passwordfield.getpassword().tostring());     resultset rs = stmt.executequery();      while (rs.next()) {         system.out.println("true");         frame2.setvisible(true);         frame.setvisible(false);         break;     }                        } catch (sqlexception ee) {     joptionpane.showmessagedialog(frame2, "wrong inf ... please try again "); } 

i'm not java programmer quick glance @ code , descriptions of resultset::wasnull() resultset::next() show me you're misusing or misunderstanding how work.

  1. wasnull() tells if current column contains null. empty set not null!
  2. next() moves cursor forward 1 row , returns true if new current row valid. false otherwise. in other words, if have 0 or 1 valid row, next() return false.

so, let's put things together.

case 1: using wasnull():

  • enter valid data. resultset contains non-null result. wasnull() returns false, don't enter while loop.
  • enter invalid data. resultset contains empty set result. wasnull() returns false, don't enter while loop.

case 2: using next():

  • enter valid data. resultset contains single non-null result. next() moves next row [which invalid] , returns false, don't enter while loop.
  • enter invalid data. resultset contains single empty set result. next() moves next row [which invalid] , returns false, don't enter while loop.

might suggest spending time reading resultset api documentation: https://docs.oracle.com/javase/7/docs/api/java/sql/resultset.html


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -