我在将数据插入 MYSQL 数据库时遇到问题。使用下面的代码我收到错误:
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 '?,?)' at line 1
代码:
public void signUpUser(Connection conn, String userName, String password) {
String queryString = "INSERT INTO USERS (USER_ALIAS, USER_PASS) VALUES (?,?)";
try {
preparedStatement = (PreparedStatement) conn.prepareStatement(queryString);
preparedStatement.setString(1, userName);
preparedStatement.setString(2, password);
preparedStatement.executeUpdate(queryString);
} catch (SQLException e) {
e.printStackTrace();
}
}
但是使用此代码插入可以正常工作:
public void signUpUser(Connection conn, String userName, String password) {
String queryString = "INSERT INTO USERS (USER_ALIAS, USER_PASS) VALUES ('"+userName+"', '"+password+"')";
try {
preparedStatement = (PreparedStatement) conn.prepareStatement(queryString);
preparedStatement.executeUpdate(queryString);
} catch (SQLException e) {
e.printStackTrace();
}
}
我想知道为什么在使用第一部分代码时会抛出错误 预先感谢您!
最佳答案
尽管您已经创建了语句,但您正在尝试使用字符串执行更新。
你必须使用:
preparedStatement.executeUpdate();
关于java - 在JavaFX中插入MYSQL数据库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25326661/