我无法用mysql弄清楚我的语法错误。
我在eclipse中的Java程序中运行登录屏幕并收到此错误消息:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您有错误 在你的SQL语法中;查看与MySQL对应的手册 服务器版本,用于在#C; f8d6792'' [C @ f8d6792''在线 1
每次运行程序C@
后更改7个字符并抛出异常。我试着查看手册,但过了一会儿就气馁了。
以下是抛出异常的部分的代码:
Connection conn = null;
String dbName = "test";
String serverip = "localhost";
String serverport = "3306";
String url = "jdbc:mysql://" + serverip + ":" + serverport + "/" + dbName;
String driver = "com.mysql.jdbc.Driver";
String databaseUserName = "root";
String databasePassword = "Pitbu!1s";
Statement stmt = null;
ResultSet rs = null;
String sql = "select * from users where user_name = '" + textField_UserName.getText() + " AND password = '" + passwordField_Password.getPassword().toString() + "'";
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
if (rs.next())
lblUserName_Label.setText(textField_UserName.getText());
lblUserNameOver_Label.setText(textField_UserName.getText() + "'s");
textField_UserName.setText("");
passwordField_Password.setText("");
panel_Main.setVisible(true);
panel_LogIn.setVisible(false);
conn.close();
} catch (Exception i) {
System.out.print(i);
}
答案 0 :(得分:0)
你错过了SQL语句中的撇号
String sql = "select * from users where user_name = '" + textField_UserName.getText() + "' AND password = '" + passwordField_Password.getPassword().toString() + "'";
答案 1 :(得分:0)
可能问题出在这里:
String sql = "select * from users where user_name = '" + textField_UserName.getText() + " AND password = '" + passwordField_Password.getPassword().toString() + "'";
^
您没有关闭userName的引号,很可能是构建了此查询:
select * from users where user_name = 'tom AND password='pw';
^
为了完成这项工作,你可以添加缺失的引用,就是这样。
更好的方法是不连接字符串并使用参数来构建查询。这将更安全,更易于维护。