Java JDBC - 创建表SQL错误

时间:2017-10-23 11:25:26

标签: java mysql jdbc

这是我的代码,我尝试使用Java中的JDBC在数据库中创建表。

package no.westerdals;

    import java.sql.Connection;
    import java.sql.Statement;
    import java.sql.SQLException;

    public class DBmanager {

        public static void main(String[] args) {
            try {
                createTables();
            }
            catch (SQLException e){
                e.printStackTrace();
            }

        }

        public static void createTables() throws SQLException {

            Connection con = null;
            Statement statement = null;

            String createTableEmneSQL = "CREATE TABLE emne("
                    + "id INTEGER NOT NULL, "
                    + "emneKode VARCHAR(25) NOT NULL, "
                    + "emneNavn VARCHAR(25) NOT NULL, "
                    + "antallStudenter INTEGER NOT NULL, "
                    + "lerer_id INTEGER NOT NULL, "
                    + "beskrivelse VARCHAR(MAX) NOT NULL, " 
                    + "PRIMARY KEY (id)"
                    + ")";

            try {
                con = DBconnector.getConnection();
                statement = con.createStatement();

                System.out.println(createTableEmneSQL);

                statement.execute(createTableEmneSQL);

                System.out.println("Tables created...");
            }
            catch (SQLException e) {
                System.out.println("Tables did not get created, error...");
                e.printStackTrace();
            } finally {

                if (statement != null) {
                    statement.close();
                }

                if (con != null) {
                    con.close();
                }
            }
        }
    }

但是当我运行代码时,我得到一个像这样的SQL错误:

Connecting to database...
Connection successful...
CREATE TABLE emne(id INTEGER NOT NULL, emneKode VARCHAR(25) NOT NULL, emneNavn VARCHAR(25) NOT NULL, antallStudenter INTEGER NOT NULL, lerer_id INTEGER NOT NULL, beskrivelse VARCHAR(MAX) NOT NULL, PRIMARY KEY (id) )
Tables did not get created, 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 'MAX) NOT NULL, PRIMARY KEY (id) )' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
    at com.mysql.jdbc.Util.getInstance(Util.java:387)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2503)
    at com.mysql.jdbc.StatementImpl.executeInternal(StatementImpl.java:839)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:739)
    at no.westerdals.DBmanager.createTables(DBmanager.java:63)
    at no.westerdals.DBmanager.main(DBmanager.java:23)

我已经尝试了很多方法来格式化SQL,但我对此很新,所以我还没有完全掌握它。这是我缺少的东西还是完全错误的东西。有人有任何想法吗?帮助将受到高度赞赏。这是我的学校项目。

0 个答案:

没有答案