File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Expected result not coming in an application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Expected result not coming in an application" Watch "Expected result not coming in an application" New topic
Author

Expected result not coming in an application

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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

Joined: May 26, 2003
Posts: 30596
    
154

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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
 
GeeCON Prague 2014
 
subject: Expected result not coming in an application