2014-10-20 55 views
0

我想在我的SQL表中創建一個AUTO_INCREMENT字段,通過SQL Statement.execute創建它,但它繼續拋出一個com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'AUTO_INCREMENT'CREATE TABLE中的AUTO_INCREMENT引發JDBC.SQLSERVEREXCEPTION

這裏是我的代碼創建我的表:

public class CreateTable { 
Connection con; 
Statement stmt = null; 
ResultSet queryResults = null; 

public CreateTable() { 

    con = DatabaseUtil.getConnection(); 

    try { 
     stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, 
       ResultSet.CONCUR_UPDATABLE); 

     stmt.execute("IF OBJECT_ID('A00776920_Members') IS NOT NULL DROP TABLE A00776920_Members"); 

     String createTable = "CREATE TABLE A00776920_Members (" 
       + "memberid INT NOT NULL AUTO_INCREMENT, " 
       + "firstName VARCHAR(30) DEFAULT NULL, " 
       + "lastName VARCHAR(30) DEFAULT NULL, " 
       + "address VARCHAR(40) DEFAULT NULL, " 
       + "city VARCHAR(40) DEFAULT NULL, " 
       + "code CHAR(7) DEFAULT NULL, " 
       + "country VARCHAR(40) DEFAULT NULL, " 
       + "phoneNumber CHAR(14) DEFAULT NULL, " 
       + "email VARCHAR(40) DEFAULT NULL, " 
       + "PRIMARY KEY (memberid))"; 
     stmt.execute(createTable); 

     stmt.close(); 
     con.close(); 

    } catch (SQLException ex) { 
     ex.printStackTrace(); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    new CreateTable(); 
} 

}

是在我被拋出確切的錯誤是:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'AUTO_INCREMENT'. 
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(Unknown Source) 
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(Unknown Source) 
at com.microsoft.sqlserver.jdbc.SQLServerStatement.execute(Unknown Source) 
at a00776920.assignment.dao.CreateTable.<init>(CreateTable.java:40) 
at a00776920.assignment.dao.CreateTable.main(CreateTable.java:53) 

任何幫助,將不勝感激! 乾杯

回答

3

看起來您正在使用錯誤的JDBC驅動程序。 create table語法顯然是MySQL,但您顯然正在使用MS SQL Server JDBC Driver。使用正確的驅動程序 - 你應該沒問題。

你可以從這裏下載MySQL的JDBC驅動程序:http://dev.mysql.com/downloads/connector/j/5.5.html

+0

感謝您!我會馬上檢查 – user2184723 2014-10-20 21:09:40

+0

我將jar文件添加到我的項目中,但我仍然收到相同的錯誤。 – user2184723 2014-10-20 21:35:32

+0

這不僅僅是JAR文件 - 你的代碼中的某個地方必須用'Class.forName'或類似的方法加載驅動程序 - 確保你正在加載正確的驅動程序:'Class.forName(「com.mysql.jdbc.Driver」 );' – 2014-10-20 21:37:27

相關問題