I'm getting this error in eclipse
我在eclipse中遇到这个错误
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 '? , login = ? , pwd = ? WHERE login = 'pp'' at line 1
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法? ,登录=? ,pwd =?在第1行登录='pp''
This is my query in my source code:
这是我在源代码中的查询:
String query2 = "UPDATE usuarios SET nombre = ? , login = ? , pwd = ? WHERE login = '" + login2 + "'";
And this is the whole code from my method:
这是我方法的全部代码:
private void modificar() {
// Prints the content of the table
String query = "SELECT * FROM usuarios";
try {
pst = con.Conectar().prepareStatement(query);
rs = pst.executeQuery(query);
// Itarate over the registries
int i = 0;
while (rs.next()) {
i++;
//print them
System.out.println(i + " " + rs.getString("id") + " " + rs.getString("login"));
}
//There are X registries
System.out.println("Existen " + i + " usuarios actualmente");
pst.close();
//Wich registry do you need to modify?
System.out.println("Ingrese el login del usuarios a modificar");
String login2 = scanner.nextLine();
System.out.println("Ingrese datos a modificar");
System.out.print("Nombre: ");
nombre = scanner.nextLine();
System.out.print("Login: ");
login = scanner.nextLine();
System.out.print("Password: ");
pwd = scanner.nextLine();
String query2 = "UPDATE usuarios SET nombre = ? , login = ? , pwd = ? WHERE login = '" + login2 + "'";
pst = con.Conectar().prepareStatement(query2);
pst.setString(1, nombre);
pst.setString(2, login);
pst.setString(3, pwd);
/*
* Aqui da error de sintaxis en query2
*/
pst.executeUpdate(query2);
pst.close();
String query3 = "SELECT * FROM usuarios where login =" + login;
pst = con.Conectar().prepareStatement(query3);
rs = pst.executeQuery(query3);
rs.next();
System.out.println("ahora quedo asi " + rs.getString("login"));
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
} finally {
cerrarConsultas();
}
}
But is working fine when I use it in MySQL Workbench, this is my test in the workbench.
但是当我在MySQL Workbench中使用它时工作正常,这是我在工作台中的测试。
prepare insertar from "UPDATE usuarios SET nombre = ?, login = ?, pwd = ? WHERE login = 'pp'";
-- "UPDATE usuarios SET nombre = ?, login = ?, pwd = ? WHERE login = 'pablo'";
set @nombre = 'pp';
set @login = 'pp';
set @pwd = 'pp';
execute insertar using @nombre, @login, @pwd;
deallocate prepare insertar;
I've tried even with literal qoutes and still doesn't work.
我甚至尝试过文字qoutes但仍然无法正常工作。
String query2 = "UPDATE usuarios SET `nombre` = ? , `login` = ? , `pwd` = ? WHERE login = '" + login2 + "'";
Also tried:
还尝试过:
String query2 = "UPDATE usuarios SET `nombre` = ? , `login` = ? , `pwd` = ? WHERE login = "+ login2;
Same result.
结果相同。
2 个解决方案
#1
1
Replace pst.executeUpdate(query2);
with pst.executeUpdate();
替换pst.executeUpdate(query2);用pst.executeUpdate();
Otherwise you will end up ignoring the parameter binding you did with with the various pst.setString(...)
hence the db engine will receive a query with ?
instead of the values you meant to bind.
否则你最终会忽略你用各种pst.setString(...)进行的参数绑定,因此db引擎会收到一个查询?而不是你想绑定的值。
#2
0
Why not using a parameter for your WHERE clause?
为什么不为WHERE子句使用参数?
String query2 = "UPDATE usuarios SET nombre = ? , login = ? , pwd = ? WHERE login = ?";
pst = con.Conectar().prepareStatement(query2);
pst.setString(1, nombre);
pst.setString(2, login);
pst.setString(3, pwd);
pst.setString(4, login2 );
#1
1
Replace pst.executeUpdate(query2);
with pst.executeUpdate();
替换pst.executeUpdate(query2);用pst.executeUpdate();
Otherwise you will end up ignoring the parameter binding you did with with the various pst.setString(...)
hence the db engine will receive a query with ?
instead of the values you meant to bind.
否则你最终会忽略你用各种pst.setString(...)进行的参数绑定,因此db引擎会收到一个查询?而不是你想绑定的值。
#2
0
Why not using a parameter for your WHERE clause?
为什么不为WHERE子句使用参数?
String query2 = "UPDATE usuarios SET nombre = ? , login = ? , pwd = ? WHERE login = ?";
pst = con.Conectar().prepareStatement(query2);
pst.setString(1, nombre);
pst.setString(2, login);
pst.setString(3, pwd);
pst.setString(4, login2 );