您的sql语法有错误“近”[c@f8d6792“”在第1行

k3bvogb1  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(257)

我搞不懂mysql的语法错误。
我在eclipse中的java程序中运行log-in screen,得到以下错误消息:
com.mysql.jdbc.exceptions.mysqlsyntaxerrorexception:您的sql语法有错误;查看与您的mysql服务器版本对应的手册,以获得使用near的正确语法[c@f8d6792“”在第1行
后面的7个字符 C@ 每次运行程序并引发异常时都进行更改。我试着看手册,但过了一会儿就泄气了。
以下是引发异常的部分的代码:

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);
}
nzkunb0c

nzkunb0c1#

sql语句中缺少撇号

String sql = "select * from users where user_name = '" + textField_UserName.getText() + "' AND password = '" + passwordField_Password.getPassword().toString() + "'";
sauutmhj

sauutmhj2#

问题可能在这里:

String sql = "select * from users where user_name = '" + textField_UserName.getText() + " AND password = '" + passwordField_Password.getPassword().toString() + "'";
                                                                                         ^

您没有关闭用户名的报价,很可能此查询已生成:

select * from users where user_name = 'tom AND password='pw';
                                          ^

为了使这项工作,你可以只添加缺少的报价,这就是它。
更好的方法是不要串联字符串并使用参数来构建查询。这将更安全,更容易维护。

相关问题