在DB中创建表会导致很多异常

时间:2014-11-28 15:11:32

标签: java mysql exception jdbc

我对SQL和Java相当陌生,所以请耐心等待。

我正在尝试访问我的数据库,创建一些表,将其设置为InnoDB并设置一些外键。 一切都编译好看,但我最终得到这些错误(似乎位于JDBC库中):

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'Auditorium'
    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:408)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:978)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2526)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2484)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:848)
    at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:742)
    at CreateDB.createTables(CreateDB.java:35)
    at Main.main(Main.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

CreateDB.java类看起来像这样:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import static java.sql.DriverManager.registerDriver;


public class CreateDB {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String database_name = "<DBname>";
static final String DB_URL = "<server>" + "<DBname>";
static final String USER = "*****";
static final String PASS = "*****";
Statement statement = null;
Connection connection = null;


public CreateDB()
{
    try {
        registerDriver(new com.mysql.jdbc.Driver());
        connection = DriverManager.getConnection(DB_URL, USER, PASS);
        statement = connection.createStatement();
    } catch (Exception e) {
        e.printStackTrace(); // handle errors
    }
}


public void createTables() throws SQLException {
    statement.execute("DROP TABLE Auditorium");
    statement.execute("DROP TABLE Customer");
    statement.execute("DROP TABLE Screening");
    statement.execute("DROP TABLE Ticket");

    String Auditorium = "CREATE TABLE Auditorium "        //Sets name and size of each auditorium
            + "(ID INTEGER NOT NULL AUTO_INCREMENT, "    //Primary key for Auditorium
            + "Name VARCHAR(10), "                        //Name for specific Auditorium (example: 'aud1')
            + "Row INTEGER NOT NULL, "                    //Amount of rows in specific auditorium
            + "Column INTEGER NOT NULL, "                //Amount of columns in specific auditorium
            + "PRIMARY KEY (ID));";                        //Sets primary key (ID)

    String Screening = "CREATE TABLE Screening "        //Keeps track of what screenings we offer
            + "(ID INTEGER NOT NULL AUTO_INCREMENT, "    //Primary key for Screening
            + "Auditorium VARCHAR(10) NOT NULL, "        //Name for specific Auditorium (example: 'aud1')
            + "Movie VARCHAR(50) NOT NULL, "            //Movie title
            + "Date DATE NOT NULL, "                    //Specific date the movie is being shown
            + "Time TIME NOT NULL, "                    //What time the movie runs
            + "PRIMARY KEY (ID));";                        //Sets primary key (ID)

    String Customer = "CREATE TABLE Customer "            //Information on the customer
            + "(ID INTEGER NOT NULL AUTO_INCREMENT, "    //Primary key for Customer
            + "Name VARCHAR(50) NOT NULL, "                //Name on customer
            + "Phone INTEGER NOT NULL, "                //Phone on customer
            + "Email VARCHAR(50), "                        //Optional mail on customer
            + "PRIMARY KEY (ID);";                        //Sets primary key (ID)

    String Ticket = "CREATE TABLE Ticket "                //Ticket that customer can buy
            + "(ID INTEGER NOT NULL AUTO_INCREMENT, "    //Primary key for Ticket
            + "Row INTEGER NOT NULL, "                    //Specific row
            + "Seat INTEGER NOT NULL, "                    //Specific seat
            + "AudID INTEGER NOT NULL, "                //Foreign key references Auditorium(ID)
            + "ScreeningID INTEGER NOT NULL, "            //Foreign key references Screening(ID)
            + "PRIMARY KEY (ID);";                        //Sets primary key (ID)

    statement.executeUpdate(Auditorium);
    statement.executeUpdate(Screening);
    statement.executeUpdate(Customer);
    statement.executeUpdate(Ticket);
}

public void changeEngine() throws SQLException {
    String Auditorium = "ALTER TABLE Auditorium ENGINE=InnoDB;";
    String Screening = "ALTER TABLE Screening ENGINE=InnoDB;";
    String Customer = "ALTER TABLE Customer ENGINE=InnoDB;";
    String Ticket = "ALTER TABLE Ticket ENGINE=InnoDB;";
    statement.execute(Auditorium);
    statement.execute(Screening);
    statement.execute(Customer);
    statement.execute(Ticket);
}

public void applyForeignKeys() throws SQLException {
    String AudID = "ALTER TABLE Ticket ADD FOREIGN KEY (AudID) REFERENCES Auditorium(ID);";
    String ScreeningID = "ALTER TABLE Ticket ADD FOREIGN KEY (ScreeningID) REFERENCES Screening(ID);";
    statement.executeUpdate(AudID);
    statement.executeUpdate(ScreeningID);
}



}

我的主要方法很简单:

import java.sql.SQLException;
public class Main {

public static void main(String[] args) throws SQLException {

    CreateDB c = new CreateDB();
    c.createTables();
    c.changeEngine();
    c.applyForeignKeys();

}
}

5 个答案:

答案 0 :(得分:2)

我认为问题是您在main方法中首先调用c.createTables()并且表格不存在。

如果表不存在,则会返回错误,例如table does not exits,因为您尝试在不存在的表上运行drop语句。

如果表不存在,请将createTables()修改为不运行DROP语句。

由于你使用的是mysql,你可以这样做:

statement.execute("DROP TABLE IF EXISTS Auditorium");

对于Oracle或其他数据库,它会有所不同。

答案 1 :(得分:2)

stacktrace中的行:

 at CreateDB.createTables(CreateDB.java:35)

告诉您代码中出现错误的行。

我的猜测是第35行是:

statement.execute("DROP TABLE Auditorium");

您不能删除不存在的表格。

尝试:

statement.execute("DROP TABLE IF EXISTS Auditorium");

答案 2 :(得分:2)

您还忘记了以下陈述中)末尾的strings

String Customer = "CREATE TABLE Customer "            //Information on the customer
            + "(ID INTEGER NOT NULL AUTO_INCREMENT, "    //Primary key for Customer
            + "Name VARCHAR(50) NOT NULL, "                //Name on customer
            + "Phone INTEGER NOT NULL, "                //Phone on customer
            + "Email VARCHAR(50), "                        //Optional mail on customer
            + "PRIMARY KEY (ID);";                        //Sets primary key (ID)

    String Ticket = "CREATE TABLE Ticket "                //Ticket that customer can buy
            + "(ID INTEGER NOT NULL AUTO_INCREMENT, "    //Primary key for Ticket
            + "Row INTEGER NOT NULL, "                    //Specific row
            + "Seat INTEGER NOT NULL, "                    //Specific seat
            + "AudID INTEGER NOT NULL, "                //Foreign key references Auditorium(ID)
            + "ScreeningID INTEGER NOT NULL, "            //Foreign key references Screening(ID)
            + "PRIMARY KEY (ID);";  

答案 3 :(得分:1)

statement.execute("DROP TABLE IF EXISTS Auditorium");

答案 4 :(得分:0)

在大多数数据库引擎中,存在必须以特定方式标记的保留字以用作对象名(即表或字段名)。在MySql中,rowcolumn是保留字。

将列名括在反引号中将允许将关键字用作列名。