CREATE TABLE中的AUTO_INCREMENT抛出JDBC.SQLSERVEREXCEPTION

时间:2014-10-20 21:05:38

标签: java mysql servlets jdbc

我正在尝试在我的SQL表中创建一个通过SQL Statement.execute创建的AUTO_INCREMENT字段,但它继续抛出com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'AUTO_INCREMENT'

以下是我创建表格的代码:

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)

任何帮助将不胜感激! 干杯

1 个答案:

答案 0 :(得分:3)

看起来您使用的是错误的JDBC驱动程序。 create table语法显然是MySQL,但您显然使用的是MS SQL Server JDBC驱动程序。使用正确的驱动程序 - 你应该没事。

您可以从此处下载MySQL JDBC驱动程序:http://dev.mysql.com/downloads/connector/j/5.5.html