Rich_Dad_Yu
Rich_Dad_Yu

Reputation: 125

How can I fix syntax erroor "("?

Now I use postgreSQL and make stockmanagement program.
While I make table, the error has occured.
My Code like this!

import java.sql.Connection;
import java.sql.Statement;

public class Create_Table {

    public static void main(String[] args) {
        
        Connection connection = null;
        Statement statement = null;
        
        ConnectDB obj_ConnectDB = new ConnectDB();
        
        connection = obj_ConnectDB.get_connection();
        
        try {
            String query = "CREATE TABLE IF NOT EXISTS stockmanagement ("
                    + "ID SERIAL primary key, "
                    + "Name varchar(20), "
                    + "Unit_price numeric(15,1), "
                    + "Qty INT(20),"
                    + "Imported_Date Date,"
                    + ")";
            statement = connection.createStatement();
            statement.executeUpdate(query);
            System.out.println("finished");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And the contents of error are as follows.
error : syntax error, "("
How can I fix this error?
If you know this solution, can you teach me?

Upvotes: 0

Views: 43

Answers (1)

Bjarni Ragnarsson
Bjarni Ragnarsson

Reputation: 1781

You have two errors in the string.

  1. Extra comma at the end
  2. INT(20) is not legal

This should work

            String query = "CREATE TABLE IF NOT EXISTS stockmanagement ("
                    + "ID SERIAL primary key, "
                    + "Name varchar(20), "
                    + "Unit_price numeric(15,1), "
                    + "Qty INT,"
                    + "Imported_Date Date"
                    + ")";

Upvotes: 2

Related Questions