Results 1 to 3 of 3

Thread: [RESOLVED] Java - SQL Exception - Create table syntax error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Resolved [RESOLVED] Java - SQL Exception - Create table syntax error


    Hi everybody,
    I had a clean compile with an SQL Exception from a possible syntax error, but I can't figure out what I'm doing wrong. See the Error msg below:

    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Syntax error in CREATE TABLE statement.
    Thanks.


    Code:
    import java.lang.*;
    import java.lang.String;
    import java.awt.*;
    import java.sql.*;
    
     // @author GIXTIAN
    
    public class NewConn 
        {
        public static void main(String[]args)
                {
                try
                    {  //Specify Driver to be used as a connection bridge
                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                       //Establish connecton to the database (in this case "TPersonsDatabase.mdb"
                    Connection conn = DriverManager.getConnection(
         "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/TPersonsDatabase.mdb"
          );
    
                    
                      //Create Sql statement using the "statement function":
                    Statement stmt = conn.createStatement();
                    
                    stmt.executeUpdate("CREATE TABLE TPERSONS" + 
                            "FIRSTNAME VARCHAR(20), LASTNAME VARCHAR(20), SSN VARCHAR(9)" +
                            "SALARY INTEGER");
                    
                    stmt.executeUpdate("INSERT INTO TPERSONS VALUES('GIFT', 'XTIAN', '148942851' 9000");
                    stmt.executeUpdate("INSERT INTO TPERSONS VALUES('PROF.', 'DEVI', '199952584', 10000");
                    stmt.executeUpdate("INSERT INTO TPERSONS VALUES('JASPER', 'CHUCK', '879458974', 5000");
                    stmt.executeUpdate("INSERT INTO TPERSONS VALUES('NOBLE', 'JACKSON', 145854477', 11000");
                    
                    stmt.executeQuery("SELECT * FROM TPERSONS");
                    }
                
                catch(ClassNotFoundException ex)
                    {
                    System.out.println(ex.toString());
                    }
                
                catch(SQLException ex)
                    {
                    System.out.println(ex.toString());
                    }
                }
        }
    Gift

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Java - SQL Exception - Create table syntax error

    Think about what that string will look like when it is concatenated:
    "CREATE TABLE TPERSONSFIRSTNAME VARCHAR(20), LASTNAME VARCHAR(20), SSN VARCHAR(9)SALARY INTEGER"

    That doesn't look quite right to me.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: Java - SQL Exception - Create table syntax error

    Good morning "Si". I didn't realize I was missing "(" in the query.
    It's working now.
    Thanks.
    Gift.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width