1

This is my code where I try create a table in a database with JDBC in Java.

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();
                }
            }
        }
    }

But when I run the code I get a SQL error like this:

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)

I have tried many ways to format the SQL, but I am pretty new to this so I haven't grasped it completely. Is it something I am missing or is completely wrong. Anyone got any idea? Help would be highly appreciated. It is for school project of mine.

oml
  • 11
  • 1
  • You should try running the create table query directly in the SQL client first. It will give you a more detailed syntax error usually – Cruncher Oct 23 '17 at 11:55
  • Thank you, found out what the problem was. VARCHAR(MAX) didn't work for some reason. – oml Oct 30 '17 at 10:17

0 Answers0