abhimanyuaryan
abhimanyuaryan

Reputation: 4044

SQL syntax in Type1: java jdbc odbc bridge

I am trying to commit into Microsoft Access 2014. I have created an old version of .mdb file with 2014. As I don't get .accdb option while creating System DSN. I have some problems with committing into Access Database.

Problems:

1) I have done little bit of SQL but this SQL we use here is quite different .Where can I read more this SQL syntax? i.e. int vs number, etc

2) CREATE TABLE user throws SQL Syntax error

 java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in
 CREATE TABLE statement.
        at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
        at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
        at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3111)
        at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338)
        at sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
        at Test.main(t.java:28)

I don't understand what's the syntax error

3)I want to write b/w function like this:

CREATE TABLE user(
ID int PRIMARY KEY AUTO_INCREMENTED NOT NULL, 
name varchar(30) ); 

in multiple line, not in a single long line. I tried using \ after every line split but gave me compile time error.

4) In case of CREATE TABLE batch, this programs runs perfect. Creates a BATCH table in MS Access database but doesn't insert data into there respective tables. ( I have commented all stuff related to batch table in code. So, i can see errors only for user table )

NOTE: db is the DSN name that I created in C:\Windows\SysWOW64\odbcad32.exe

CODE

import java.sql.*; 
import java.util.*;

class Test{
      public static void main(String args[]){
           try{
               Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
               System.out.println("Driver Loaded"); 
               Connection myConnection = DriverManager.getConnection("jdbc:odbc:db"); 
               System.out.println("connection established");

               Statement myStatement = myConnection.createStatement();
               myStatement.executeUpdate("CREATE TABLE user(ID number NOT NULL AUTO_INCREMENTED,LastName char NOT NULL,FirstName char NOT NULL, Tweet char,PRIMARY KEY (ID))");

               //myStatement.executeUpdate("CREATE TABLE batch(name char, age number)");

               System.out.println("Created Table"); 
               myStatement.executeUpdate("INSERT INTO user VALUES 
               //myStatement.executeUpdate("INSERT INTO batch values('rakesh', 23)");
              System.out.println("Commited in table"); 
              myStatement.close();
              myStatement.close(); 

          }catch(SQLException e){ e.printStackTrace();}           
          catch(ClassNotFoundException e){ e.printStackTrace();}
     }
}

Upvotes: 0

Views: 155

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123819

Unfortunately, the documentation for CREATE TABLE and related DDL statements in Access SQL is quite sparse and scattered, but it can be found (in its various places) with enough digging.

In your case

  • the int column type is not required for an AutoNumber field and is causing the "syntax error",
  • AUTO_INCREMENTED is not the correct type specifier for an AutoNumber field,
  • and (as Mark hinted in his comment) the column attributes must be written in the correct order:
String sql = 
        "CREATE TABLE user ( " +
        "id AUTOINCREMENT PRIMARY KEY, " +
        "name VARCHAR(30) " +
        ");";

Upvotes: 1

Related Questions