The very same sql statement works fine in MySQL Workbench but fails in Java program. All I've done was setting a breakpoint before the execution of this statement in Java and paste the same statement ("select * from administrator where AdministratorID = 'Admin'") from Java to the Workbench, where showed ONE record. I surrounded the invoking with try-catch, it turned out successfully called but returned nothing (resultset.getRow() returns 0). Here is the Java code that executes the query:
public ResultSet executeQuery(String tableName, String condition) {
String sql = "select * from " + tableName + " where " + condition;
ResultSet rs = null;
Connection conn = null;
try {
conn = getConn();
Statement statement = conn.createStatement();
rs = statement.executeQuery(sql);
statement.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return rs;
}
And the getConn() function:
public Connection getConn() {
Connection conn = null;
try {
Class.forName(name);
String url = "jdbc:mysql://" + host + ":" + port + "/" + dbName;
conn = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
where name = "com.mysql.jdbc.Driver", host = "127.0.0.1", port = "3306". dbName, user and password were provided correctly.
Can anyone help tell me what went wrong?