Why does the following statement cause a SyntaxErrorException?

69 Views Asked by At

I am trying to create a table using JDBC but i am getting syntax error at the query .

Statement statement = con.createStatement();
String query = "CREATE TABLE accounts (user VARCHAR2(255) NOT NULL,
                                       password VARCHAR2(255) NOT NULL,
                                       dateRegistered date "+
                                      "primary key (user));";

statement.executeUpdate(query);
System.out.println("part 2:DONE !"); 

This is the 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 'VARCHAR2(255) NOT NULL, password VARCHAR2(255) NOT NULL, dateRegistered date, pr' at line 1    
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)    
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)    
at java.lang.reflect.Constructor.newInstance(Unknown Source)    
at com.mysql.jdbc.Util.handleNewInstance(Util.java:389)    
at com.mysql.jdbc.Util.getInstance(Util.java:372)    
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)    
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)    
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)    
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)    
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)    
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2531)    
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2489)    
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:848)    
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:742)    
at Test.main(Test.java:26)
1

There are 1 best solutions below

2
On BEST ANSWER

Assuming you are using MySQL

MySQL does not support VARCHAR2, so replace it with VARCHAR

And add a comma before primary key.

String query = "CREATE TABLE accounts (user VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, dateRegistered date, primary key (user));";