• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java querying a database

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's working! Thank you!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic