0
CREATE TABLE "INVOICE" (
"INVOICENO" VARCHAR(200) NOT NULL PRIMARY KEY,
"DATE" DATE,
"COMPANY" VARCHAR(500),
"PRICE" DOUBLE,
"TYPE" VARCHAR(200),
"INVOICETYPE" VARCHAR(200),
"GENERATEDDOCID" INTEGER  GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1) 


);

This is my database table code, when i want to enter values into the table with INSERT INTO ( ) VALUES (?,?,?) it is giving SQLSyntax Error.

I use the code below to insert the values:

      public void addData(addData addData) {
      private String sqlInsertStr = "INSERT INTO 
       NBUSER.INVOICE(INVOICENO,DATE,COMPANY,PRICE,TYPE,INVOICETYPE) VALUES(?,?,?,?,?,?);";
    try {

        stmt = conn.prepareStatement(sqlInsertStr);

        stmt.setString(1, addData.getInvoiceNo());
        stmt.setString(2, addData.getDate());
        stmt.setString(3, addData.getCompany());
        stmt.setDouble(4, addData.getPrice());
        stmt.setString(5, addData.getType());
        stmt.setString(6, addData.getInvoiceType());
        stmt = conn.prepareStatement(sqlInsertStr);
        stmt.executeUpdate();
    } catch (SQLException ex) {
        ex.getMessage();
    }
}
3
  • 1
    It doesn't help that we don't know anything about the kind of database you're using. Additionally, where is the error occurring? Is there any more information? And why are you calling prepareStatement twice? (The second assignment may well be the root of the problem.) Commented Jan 3, 2018 at 17:58
  • I accidentally add the prepared statement twice, i have removed the bottom one, but the problem still exists. I am using Netbeans Derby SQL. Thanks for helping Commented Jan 3, 2018 at 18:02
  • Please post the full exception stacktrace. Commented Jan 3, 2018 at 21:01

1 Answer 1

1

There are few probable source of errors (based on the code review):

  1. The query should not be terminated by a semi-colon. Currently, the sql query in your code is being terminated by a semi-colon within the double-quote.

    private String sqlInsertStr = "INSERT INTO NBUSER.INVOICE
    (INVOICENO,DATE,COMPANY,PRICE,TYPE,INVOICETYPE)
     VALUES(?,?,?,?,?,?)";  // removed semi-colon at the end of query.
    
  2. You shouldn't declare a column in table bearing the name of standard datatypes like DATE. You should rename it to something like INVOICEDATE, etc while declaring the table.

  3. Your second parameter expects a parameter of Date type, whereas you're supplying a string value to the query. It will totally result in exception.

    stmt.setDate(2, new java.sql.Date(addData.getDate());// changed the type.
    
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The semi colon was the one affecting the codes
@AaronLeeHerngYue - You're welcome. But, please make sure that you follow the recommendations mentioned in my answer so that you have a better code quality. Good Luck!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.