Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Expected result not coming in an application

 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranch,
Below code is one of the functionality in the application i am developing

my concern is that when i enter user id and password whether correct ,incorrect or left blank i am taken to the next frame kindly suggest on this so that i can proceed with my development
LoginInformation is the ms access database table i am using.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
User_ID = jTextField1.getText().trim();
Password = jTextField2.getText().trim();
boolean b;
try {
pstmt = con.prepareStatement("Select UserID From LoginInformation where UserId=' "+User_ID+" ' and Password=' "+Password+" ' ");
b = pstmt.execute();
if(b==true)
{
HomePage hp = new HomePage();
hp.setVisible(true);
this.setVisible(false);
} else if (User_ID.isEmpty() || Password.isEmpty()) {
JOptionPane.showMessageDialog(null, "UserId and password cannot be left blank.");
} else {
JOptionPane.showMessageDialog(null, "User ID and Password are not correct");
}
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the idiom for checking if a result is returned:



Note that it calls executeQuery not execute.

Also, if this code is anything more than a toy, read up on SQL Injection. In particularly your query should be
Select UserID From LoginInformation where UserId=? and Password=?
and use pstmt.setString() to pass in the user id and password.

Similarly, if this is more than a toy, you won't want to store plain text passwords in the database.
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply

in another functionality of the same application i want to use the userid value of the previous frame which is login frame to the current frame which is welcome frame how can this be achieved.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String deposit = ("Amount Credited Successfully");
k = jTextField1.getText().trim();
j = Integer.parseInt(k);
try {

if (j > 0) {
f1 = f1 + j;
h = String.valueOf(f1);
pstmnt = con.prepareStatement("Update Transaction set deposit=? where User_ID=?");

pstmnt.setString(4,User_ID);
pstmnt.setString(1, h);
pstmnt.executeUpdate();
JOptionPane.showMessageDialog(null, deposit);
} else if (k.isEmpty() == false) {
JOptionPane.showMessageDialog(null, "Deposit field cannot be left blank", "Null Value", 2, null);
} else {
String o = ("Deposit Field is left blank");
JOptionPane.showMessageDialog(null, o);
}

}

Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic