Create temporary table in MySQL from Java

此生再无相见时 提交于 2020-06-13 06:16:57

问题


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

}

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/10143649/create-temporary-table-in-mysql-from-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!