I'm fairly new to SQL and Java so bear with me.
I am trying to reach my DB, create some tables, set it to InnoDB and set some Foreign Keys. Everything compiles and looks okay, but I end up getting these errors (seems to be located in the JDBC lib):
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 class looks like this:
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);
}
}
My main method is very simply doing so:
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();
}
}