简体   繁体   中英

Insert Statement into Derby Database

I am trying to insert a number of values into a table in my java derby database, I am getting the error java.sql.SQLSyntaxErrorException: VALUES clause must contain at least one element. Empty elements are not allowed.

    public boolean registerStudent(Student student)
    {
        try
        {
        Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/AceDrivingSchool", "ADC", "ADC");
        Statement stmt = conn.createStatement();
        stmt.executeUpdate("INSERT INTO ADC.STUDENT(EMAILADDRESS, FORENAME, SURNAME, ADDRESSLINE1, ADDRESSLINE2,"
                + " POSTCODE, TELEPHONENO, GENDER, PASSWORD, ISREGISTERED, ISVERIFIED, PASSEDTHEORY, PASSEDPRACTICAL, DRIVINGLICENSENO, DOB"
                + ") VALUES('" + student.getEmailAddress() +
                "', '" + student.getForename() + "', '" + student.getSurname() + 
                "', '" + student.getAddressLine1() + "', '" + student.getAddressLine2() + "', '" + student.getPostcode() + 
                "', '" + student.getTelephoneNo() + "', '" + student.getGender() + "', '" + student.getPassword() + 
                "', '" + student.isIsRegistered() + "', '" + student.isIsVerified() + "', '" + student.isPassedTheory() + "', '" +
                "', '" + student.isPassedPractical() + "', '" + student.getdrivingLicenseNo() + "', '" + 
                new SimpleDateFormat("yyyy-MM-dd").format(student.getDob()) + "')");   
        conn.close();
        return true;
        }
        catch(SQLException ex)
        {
            ex.printStackTrace();
            return false;
        }
    }

This should right the values to my table

Answer given by @Basil is completely correct, you will have to read and understand it to get basic idea of How to build and execute query.

In your case though, I can see in comments that you're facing java.sql.SQLSyntaxErrorException: The number of values assigned is not the same as the number of specified or implied columns. Reason behind this is your malformed SQL having additional "," .

Remove " , + , and other formatting in sql you will end up with this,

INSERT INTO adc.student 
            ( 
                        emailaddress, 
                        forename, 
                        surname, 
                        addressline1, 
                        addressline2, 
                        postcode, 
                        telephoneno, 
                        gender, 
                        password, 
                        isregistered, 
                        isverified, 
                        passedtheory, 
                        passedpractical, 
                        drivinglicenseno, 
                        dob 
            ) 
            VALUES 
            ( 
                        student.Getemailaddress() , 
                        student.Getforename() , 
                        student.Getsurname() , 
                        student.Getaddressline1() , 
                        student.Getaddressline2() , 
                        student.Getpostcode() , 
                        student.Gettelephoneno() , 
                        student.Getgender() , 
                        student.Getpassword() , 
                        student.Isisregistered() , 
                        student.Isisverified() , 
                        student.Ispassedtheory() , 
                        , 
                        student.ispassedpractical() , 
                        student.getdrivinglicenseno() , 
                        NEW simpledateformat(yyyy-mm-dd).format(student.getdob()) 
            )

SQL parsing fails at line 33. Remove additional + "', '" after student.isPassedTheory() and you will be able to execute query. You should end up with following query format after fixing your code,

"INSERT INTO ADC.STUDENT(EMAILADDRESS, FORENAME, SURNAME, ADDRESSLINE1, ADDRESSLINE2,"
                + " POSTCODE, TELEPHONENO, GENDER, PASSWORD, ISREGISTERED, ISVERIFIED, PASSEDTHEORY, PASSEDPRACTICAL, DRIVINGLICENSENO, DOB"
                + ") VALUES('" + student.getEmailAddress() +
                "', '" + student.getForename() + "', '" + student.getSurname() + 
                "', '" + student.getAddressLine1() + "', '" + student.getAddressLine2() + "', '" + student.getPostcode() + 
                "', '" + student.getTelephoneNo() + "', '" + student.getGender() + "', '" + student.getPassword() + 
                "', '" + student.isIsRegistered() + "', '" + student.isIsVerified() + "', '" + student.isPassedTheory() +
                "', '" + student.isPassedPractical() + "', '" + student.getdrivingLicenseNo() + "', '" + 
                new SimpleDateFormat("yyyy-MM-dd").format(student.getDob()) + "')");   

The correct Answer by Desai discovered your syntax error in the generated SQL statement: misplaced comma.

Here is a better way to write your SQL so you can discover this kind of error for yourself.

You should always build your SQL separately as a piece of text, to facilitate debugging this kind of problem.

Build up your SQL text, and examine

Use StringBuilder to build up the text.

Here is example code. You fill in the xxx with your object getter methods (I'm too lazy).

StringBuilder sql = new StringBuilder() ;
String eol = "\n" ;  // linefeed
String comma = " , " ;  // SPACE + COMMA + SPACE
sql.append( "INSERT INTO ADC.STUDENT ( " ).append( eol ) ;
sql.append( "    EMAILADDRESS, FORENAME, SURNAME, ADDRESSLINE1, ADDRESSLINE2, POSTCODE, TELEPHONENO, GENDER, PASSWORD, ISREGISTERED, ISVERIFIED, PASSEDTHEORY, PASSEDPRACTICAL, DRIVINGLICENSENO, DOB " ).append( eol) ;
sql.append( ") VALUES (" ).append(eol) ;
sql.append( student.getEmailAddress() ).append( comma ) ; // EMAILADDRESS
sql.append( student.getForename()  ).append( comma ) ; // FORENAME
sql.append( xxx ).append( comma ) ; // SURNAME
sql.append( xxx ).append( comma ) ; // ADDRESSLINE1
sql.append( xxx ).append( comma ) ; // ADDRESSLINE2
sql.append( xxx ).append( comma ) ; // POSTCODE
sql.append( xxx ).append( comma ) ; // TELEPHONENO
sql.append( xxx ).append( comma ) ; // GENDER
sql.append( xxx ).append( comma ) ; // PASSWORD
sql.append( xxx ).append( comma ) ; // ISREGISTERED
sql.append( xxx ).append( comma ) ; // ISVERIFIED
sql.append( xxx ).append( comma ) ; // PASSEDTHEORY
sql.append( xxx ).append( comma ) ; // PASSEDPRACTICAL
sql.append( xxx ).append( comma ) ; // DRIVINGLICENSENO
sql.append( xxx ) ; // DOB — IMPORTANT no comma on the last item. 
sql.append( ") ;" ) ;

Then print out that value to verify your SQL looks straight. Compare to other examples of SQL to spot your boo-boo.

We will politely ignore the saving of password without salting and hashing, license number, and date of birth as big security risks.

By the way, you should be representing a date-only value such as date-of-birth on your Java object as a LocalDate object. The terrible Date & SimpleDateFormat classes were supplanted years ago by the java.time classes. The LocalDate.toString() method generates text in standard ISO 8601 format, which for a date-only happens to be the same as is commonly used in SQL.

Prepared Statement

Much better way than the above is to use a prepared statement with placeholders. Executing generated text as you were doing is more complicated to read, and more importantly, exposes you to the risk of a SQL Injection attack.

StringBuilder sql = new StringBuilder() ;
String eol = "\n" ;  // linefeed
sql.append( "INSERT INTO ADC.STUDENT ( " ).append( eol ) ;
sql.append( "    EMAILADDRESS, FORENAME, SURNAME, ADDRESSLINE1, ADDRESSLINE2, POSTCODE, TELEPHONENO, GENDER, PASSWORD, ISREGISTERED, ISVERIFIED, PASSEDTHEORY, PASSEDPRACTICAL, DRIVINGLICENSENO, DOB " ).append( eol) ;
sql.append( ") VALUES (" ).append(eol) ;
sql.append( "? , " ) ; // EMAILADDRESS
sql.append( "? , " ) ; // FORENAME
sql.append( "? , " ) ; // SURNAME
sql.append( "? , " ) ; // ADDRESSLINE1
sql.append( "? , " ) ; // ADDRESSLINE2
sql.append( "? , " ) ; // POSTCODE
sql.append( "? , " ) ; // TELEPHONENO
sql.append( "? , " ) ; // GENDER
sql.append( "? , " ) ; // PASSWORD
sql.append( "? , " ) ; // ISREGISTERED
sql.append( "? , " ) ; // ISVERIFIED
sql.append( "? , " ) ; // PASSEDTHEORY
sql.append( "? , " ) ; // PASSEDPRACTICAL
sql.append( "? , " ) ; // DRIVINGLICENSENO
sql.append( "? " ) ;   // DOB — IMPORTANT no comma on the last item. 
sql.append( ") ; " ) ;

Again, print that out and examine.

String sqlOutput = sql.toString() ;
System.out.println( sqlOutput ) ;

Create a PreparedStatement . On that object call the set… methods.

ps.setString( 1 , student.getEmailAddress() ) ;
…
ps.setObject( 15 , student.getDateOfBirth() ) ;  // Pass `LocalDate` object via `setObject` method using JDBC 4.2 or later.

Note that this can also happen when you put stray syntax into your SQL statement such as:

INSERT INTO table ( a, b, c ) VALUES = ( 1, 2, 3 )

Though the error reported is as per the original question, it isn't mismatched columns and supplied values or even missing commas at fault, but the equal sign you mindlessly put in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM