
January 16th, 2013, 04:31 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 1
Time spent in forums: 41 m 10 sec
Reputation Power: 0
|
|
|
PreparedStatement using two clauses return empty set
Hi, all. I have login table
DROP TABLE IF EXISTS login;
CREATE TABLE login (
id BIGINT AUTO_INCREMENT,
username VARCHAR(20),
password VARCHAR(100),
PRIMARY KEY(id)
);
LOAD DATA LOCAL INFILE 'login.txt' INTO TABLE login;
I want to make a query in Java to match username and password. One of the row value is username='untirta', and password='untirta'.
Code:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ti", uname, pword);
String npm = "untirta";
String passwd = "untirta";
String query = "SELECT * from login WHERE username=? AND password=?";
PreparedStatement stmt = con.prepareStatement(query);
stmt.setString(1, npm);
stmt.setString(2, passwd);
//Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString("username");
String password = rs.getString("password");
System.out.println(name);
System.out.println(password);
}
} catch (SQLException ex) {
Logger.getLogger(Tes.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (!con.isClosed()) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(Tes.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(Tes.class.getName()).log(Level.SEVERE, null, ex);
}
The resultset return null (no value). I also try in mysql command utility and return empty set. Are there something wrong in my code? I developed the code in Neatbeans 7.2.1. and MySQL 5.5.28. Thank you anyone for the time.
|