简体   繁体   中英

Create temporary table in MySQL from Java

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:

You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.)

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.

Temporary tables are per database connection. So if you're trying to access a temporary table from another connection, you won't see it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM