| Author |
java querying a database
|
Ariane Bogain
Greenhorn
Joined: Apr 25, 2003
Posts: 15
|
|
hello again! I have a log in box and I'm trying to verify that the password the user enters is correct by comparing it to his/her password held in a database. Here is my code: import java.sql.*; import java.awt.*; import java.awt.event.*; import java.io.*; class Staff extends Panel implements ActionListener { private TextField id=new TextField(20); private TextField pas=new TextField(20); private Button submit=new Button("Submit"); private Button nonvalid=new Button("Wrong password. Please try again"); private Label instruction=new Label ("Please enter your I.D and password"); public void setEchoChar(char c) {pas.setEchoChar('#'); } public Staff() { add(id); add(pas); pas.setEchoChar('#'); add(instruction); add(submit); submit.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == submit) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url, user, password; url ="jdbc dbc:STUDENT"; user = "MSC030"; password = "bagpuss"; Connection conn =DriverManager.getConnection(url, user, password); String sta=id.getText(); String spass=pas.getText(); String query="SELECT PASSWORD FROM MSC030.STAFF WHERE STAFF_NO="+ sta; ResultSet theResult; Statement stmt=conn.createStatement(); theResult=stmt.executeQuery(query); String valid=theResult.getString("PASSWORD"); if (valid.equals(spass)) { System.out.print ("hi"); } else {add(nonvalid); } } catch(SQLException s) {System.out.print("SQL Error:" +s.toString() + "" + s.getErrorCode() + "" + s.getSQLState()); } catch(ClassNotFoundException s) {System.out.print("Class not found"); } } } I am unable to get the code working: There is no compiling error and nothing happens on the screen when I press 'Submit' Could anyone help?? Thank you
|
 |
John Spindler
Greenhorn
Joined: Feb 12, 2002
Posts: 28
|
|
Two things I think: Change this: String query="SELECT PASSWORD FROM MSC030.STAFF WHERE STAFF_NO="+ sta; To: String query="SELECT PASSWORD FROM MSC030.STAFF WHERE STAFF_NO='"+ sta + "'"; SQL looks for the '' Also I think that you have to take the ResultSet pointer to the first record by executing next() like so: String valid; while(theResult.next()) { valid=theResult.getString("PASSWORD"); } I think if you change those two things you should be good, however your JDBC url came in with Graemlins face on it so I can't verify the url syntax, might want to just double check that. -Brian
|
 |
Ariane Bogain
Greenhorn
Joined: Apr 25, 2003
Posts: 15
|
|
|
It's working! Thank you!
|
 |
 |
|
|
subject: java querying a database
|
|
|