<分区>
我在使用 SQL 时遇到问题。这是我的代码,我不知道为什么也不知道如何修复它。我使用 Java EE 和 Eclipse 来执行此操作。
public void userLogin(String username,String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtilsWithProperties.getConnection();
String sql = "select * from users where username = ? and password = ?;";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery(sql);
if(rs.next()) {
System.out.println("SUCESS");
} else {
System.out.println("FAIL");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
JDBCUtilsWithProperties.release(rs, ps, conn);
}
}
}
public class JDBCUtilsWithProperties {
private static String driverClass;
private static String url;
private static String username;
private static String password;
private JDBCUtilsWithProperties(){}
static {
try {
readConfig();
Class.forName(driverClass);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void readConfig() {
Properties pp = new Properties();
try {
pp.load(new FileInputStream("src//config.properties"));
driverClass = pp.getProperty("driverClass");
url = pp.getProperty("url");
username = pp.getProperty("username");
password = pp.getProperty("password");
} catch (Exception e) {
// TODO: handle exception
}
}
public static Connection getConnection() {
try {
return DriverManager.getConnection(url,username,password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static Statement createStatement() {
Connection conn = getConnection();
Statement stat = null;
try {
stat = conn.createStatement();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return stat;
}
public static void release(ResultSet rs,Statement stat,Connection conn){
if(rs != null) {
try {
rs.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
rs = null;
}
if(stat != null) {
try {
stat.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
stat = null;
}
if(conn != null) {
try {
conn.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
conn = null;
}
}
public static void release(Statement stat,Connection conn) {
if(stat != null){
try {
stat.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stat = null;
}
if(conn != null) {
try {
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
}
}
运行时会出现一个通知:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '? AND password=?' at line 1"