SQL语法错误

时间:2016-03-25 16:08:41

标签: java mysql jdbc

我正在尝试使用Java连接到数据库。这是一个简单的程序。

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/servlets","root","");
smt = con.createStatement();
query = "select pass from users where uname = "+uname;
System.out.println(query);
rs = smt.executeQuery(query);
if((rs.getString("pass"))==pass){
    out.println("correct pass...logged in..");
}
else {
    out.println("Incorrect pass...not logged in..");
}

但它说

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:你有一个   SQL语法错误;查看与您的手册相对应的手册   MariaDB服务器版本,用于在' @ gmail.com'附近使用正确的语法   在第1行

我正在尝试验证特定电子邮件ID的密码。

2 个答案:

答案 0 :(得分:5)

在这一行

name@gmail.com

您尚未引用select pass from users where uname = name@gmail.com ,因此如果值为PreparedStatement,则会导致语法错误。即发送给DB的实际语句是

query = "select pass from users where uname = ?";
PreparedStatement ps = con.prepareStatement(query);
ps.setString(1,uname);
ResultSet rs = ps.executeQuery();

无效。您应该使用function SomeSrvc($http, ...other deps...) { var srvc = this; // Verbose way of storing dependencies srvc.$http = $http; srvc.dep2 = dep2; srvc.dep3 = dep3; srvc.dep4 = dep4; } SomeSrvc.prototype.doSomething = function() { var srvc = this; // Do stuff with srvc.$http and other srvc.deps... }; 代替

  let strArr = str.characters.split{$0 == " "}.map(String.init)

        for item in strArr {
           let components = item.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet)

                let part = components.joinWithSeparator("")

                    if let intVal = Int(part) {
                        print("this is a number -> \(intVal)")
                      }
              }

答案 1 :(得分:1)

替换此

contenteditable

        query = "select pass from users where uname = "+uname;

或尝试

       query = "select pass from users where uname = "'+uname+'"  ";