0

I have a problem inserting data into MYSQL database. Using code below I get an error:

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

But with this code the insert works normally:

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

I want to know why does it throws error while using first part of code Thank you in advance!

1

1 Answer 1

0

You are trying to execute update with the string although you have already created the statement.

You have to use:

preparedStatement.executeUpdate();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.