aspose file tools*
The moose likes JDBC and the fly likes Select From Where Clause using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Select From Where Clause using JDBC" Watch "Select From Where Clause using JDBC" New topic
Author

Select From Where Clause using JDBC

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

Hi Ranch,
I am developing standalone GUI bank application
in the log in window the action i am trying to perform for the jButton1 is that when the user enters the UserId and Password and hit the Submit Button the following is expected
1)the database i am using is MsAccess 2007
2) when user hits the submit button then userid and password should be search and if it is there then user gets to next window
3)otherwise a dialog box should be displayed

But the result i am getting
1) when the user enters the UserId and Password and hit the Submit Button i am not getting the desired output because i think the sql query i am using is not executing at all.

SQL Query----("Select UserID,Password "+"From LoginInformation where UserID='"+i+"' and Password='"+j+"'");

kindly suggest on this and also the flaw if any is there in query


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

String i = jTextField1.getText();
String j = jTextField2.getText();
try {
pstmt = con.prepareStatement("Select UserID,Password "+"From LoginInformation where UserID='"+i+"' and Password='"+j+"'");
/*pstmt.setString(1, i);
pstmt.setString(2, j);*/
rs = pstmt.executeQuery();
while (rs.next()) {
if (rs.getString(1).equals(i) && rs.getString(2).equals(j)) {
HomePage hp = new HomePage();
hp.setVisible(true);
close();
} else if (i.isEmpty() || j.isEmpty()) {
JOptionPane.showMessageDialog(null, "UserId and password cannot be left blank.");

} else {
JOptionPane.showMessageDialog(null, "Enter Correct UserId and Password");
}
}
} catch (Exception e) {
}
}

Thanks
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
1.) Don't do

At least do

to see if there are exceptions being thrown.

2.) Don't add string using + for sql statement parameters. Use PreparedStatement parameters instead to set the values.
3.) Read up on swing and threading to find out how to use swing without performing long running or IO operations on the EDT.
 
wood burning stoves
 
subject: Select From Where Clause using JDBC