I have this:
public static void createTemporaryTable() {
Statement s = null;
sentence = "CREATE TEMPORARY TABLE Book (ISBN int NOT NULL, " +
"title varchar(45), author varchar(45), price double, PRIMARY KEY (`ISBN`));";
try {
s = Conexion.getInstancia().createStatement();
s.executeUpdate(sentence);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
s.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Then:
public class System {
public static void main(String[] args) {
SqlSentencesList.createTemporaryTable();
}
}
But when I execute select * from Book, MySQL tells me Book table doesn’t exists. I ain’t getting any error messages from java, so the table should be created, but it’s not.
If I execute the same sql sentence for creating the temporary table directly in mysql, it works fine.
This’s my Conexion class:
public class Conexion {
private static Connection conexion = null;
private Conexion() {
}
public static Connection getInstancia() {
if (conexion == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
conexion = DriverManager.getConnection(
"jdbc:mysql://localhost/Esquema_VentaLibros","gustavo", "123581321");
} catch (SQLException sqlex) {
sqlex.printStackTrace();
} catch(ClassNotFoundException cnfex) {
cnfex.printStackTrace();
}
return conexion;
}
else {
return conexion;
}
}
}
Temporary tables are automatically dropped when a connection is closed.
See the MySQL CREATE TABLE documentation for details:
If you want to create a temporary table to do some work in, you should create it when you begin your work, and execute your UPDATE/SELECT statements against it. It will automatically drop when you close your connection, and it won’t conflict with other connections using the same temporary table name.