java - set() method receiving the value but get() method returning null -
i creating login page retrieves user details database , stores in user class. set methods receiving values correctly when try access details using method, it's returning null. see code snippets below , can let me know missing here.
retrieve details db , set values:
class userdao:
user.setid(rs.getstring("id")); user.sethashpassword(rs.getstring("hashpassword")); user.setaccesslevel(rs.getint("accesslevel")); user.setlastdate(rs.getdate("lastaccessdate")); note: connection closed after point.
setting values:
class user:
public class user { private string id; private string hashpassword; private int accesslevel; private date lastaccessdate; public user() { } public void setid(string uid) { this.id = uid; } public string getid() { return this.id; } public void sethashpassword(string uhash) { this.hashpassword = uhash; } public string gethashpassword() { return this.hashpassword; } public void setaccesslevel(int ualevel) { this.accesslevel = ualevel; } public int getaccesslevel() { return this.accesslevel; } public void setlastdate(date uldate) { this.lastaccessdate = uldate; } public date getlastdate() { return this.lastaccessdate; } } i accessing get() values class receiving null.
class login:
system.out.println(user.getid()); // returning null system.out.println(user.gethashpassword()); // returning null system.out.println(user.getaccesslevel()); // returning null system.out.println(user.getlastdate()); // returning null however, when create variables in userdao class (where db returning details) , call them login class, values return correctly.
can me find out error in code?
here complete code of login , userdao classes.
public class login extends javax.swing.jframe { user user = new user(); static string struserid; static string strpwd; public login() { super(); initcomponents(); //jframe jfrm = new jframe("login"); } private void jbutton1actionperformed(java.awt.event.actionevent evt) { passwordencrypt cpe = new passwordencrypt(); user user = new user(); userdao udao = new userdao(); struserid = jtextfield1.gettext(); char[] pwd = jpasswordfield1.getpassword(); strpwd = new string(pwd); if (struserid.isempty()) { joptionpane.showmessagedialog(null, "please enter user id"); jtextfield1.requestfocusinwindow(); } else if (pwd.length == 0) { joptionpane.showmessagedialog(null, "please enter password"); jpasswordfield1.requestfocusinwindow(); } else { try { // authenticate user string strstoredpwd; boolean userexists; udao.authenticate(struserid); strstoredpwd = udao.dhash; // following lines used when user class returning value system.out.println(user.getid()); system.out.println(user.gethashpassword()); system.out.println(user.getaccesslevel()); system.out.println(user.getlastdate()); userexists = cpe.validatepassword(strpwd, strstoredpwd); } } } } public class userdao { user user = new user(); public string dhash; private connection connection; private statement statement; // authenticate existing user public void authenticate(string userid) throws sqlexception { string query = "select * users id = '" + userid + "'"; resultset rs = null; try { connection = connectionfactory.getconnection(); statement = connection.createstatement(); rs = statement.executequery(query); //do thing rs if (rs.next()) { user.setid(rs.getstring("id")); user.sethashpassword(rs.getstring("hashpassword")); user.setaccesslevel(rs.getint("accesslevel")); user.setlastdate(rs.getdate("lastaccessdate")); dhash = rs.getstring("hashpassword"); } } { dbutil.close(rs); dbutil.close(statement); dbutil.close(connection); } } }
you using 2 different objects. here's how can test:
in userdao, add:
system.out.format("userdao: %d%n", system.identityhashcode(user)); and in login, add same
system.out.format("login: %d%n", system.identityhashcode(user)); it hard figure out exact problem because haven't pasted relevant code, should on right track.
Comments
Post a Comment