Search code examples
sqlderby

java.sql.SQLSyntaxErrorException?


I'm having a little trouble with my codes since it displays this Error

'java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns.'

I read similar problems but still confused on how mine still displays the error. Also i tried putting "," like this in between the values but still no good. And what is prepared statement?

    //CONNECTION
    Connection conn = null;
    try{
    conn = 
    DriverManager.getConnection("jdbc:derby://localhost:1527/MetroEventsDB", 
    "root", "root");
    System.out.println("Connected");

    //Insertion
    Statement stmt = (Statement) conn.createStatement();
    stmt.execute("INSERT INTO Users(UserID, Password, FirstName, LastName, Gender, Birthdate) VALUES('"+txtUserID.getText()+txtPassword.getText()+txtFirstName.getText()+txtLastName.getText()+gender+txtBirthdate.getText()+"')");
    }catch(SQLException e){
            System.err.println(e);
            }

Solution

  • You have syntax error in stmt.execute. The correct syntax is,

     stmt.execute("INSERT INTO Users(UserID, Password, FirstName, LastName, Gender, Birthdate) VALUES('"+txtUserID.getText()+"','"+txtPassword.getText()+"','"+txtFirstName.getText()+"','"+txtLastName.getText()+"','"+gender+"','"+txtBirthdate.getText()+"')");