在JavaFX中插入MYSQL数据库时出错

时间:2014-08-15 12:47:32

标签: java mysql jdbc insert

我在将数据插入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 

CODE:

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

我想知道为什么在使用代码的第一部分时会抛出错误 提前谢谢!

1 个答案:

答案 0 :(得分:0)

您尝试使用字符串执行更新,尽管您已经创建了语句。

你必须使用:

preparedStatement.executeUpdate();