从 Java 在 MySQL 中创建临时表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10143649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create temporary table in MySQL from Java
提问by Aikanáro
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.
但是当我执行时select * from Book
,MySQL 告诉我Book
表不存在。我没有从 java 收到任何错误消息,所以应该创建表,但事实并非如此。
If I execute the same sql sentence for creating the temporary table directly in mysql, it works fine.
如果我直接在mysql中执行相同的sql语句来创建临时表,它工作正常。
This's my Conexion
class:
这是我的Conexion
课:
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;
}
}
}
采纳答案by Jonathan
Temporary tables are automatically dropped when a connection is closed.
当连接关闭时,临时表会自动删除。
See the MySQL CREATE TABLEdocumentation for details:
有关详细信息,请参阅MySQL CREATE TABLE文档:
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.)
创建表时可以使用 TEMPORARY 关键字。TEMPORARY 表仅对当前连接可见,并在连接关闭时自动删除。这意味着两个不同的连接可以使用相同的临时表名称,而不会相互冲突或与现有的同名非临时表发生冲突。(现有表是隐藏的,直到临时表被删除。)
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.
如果你想创建一个临时表来做一些工作,你应该在你开始工作时创建它,并针对它执行你的 UPDATE/SELECT 语句。当您关闭连接时它会自动删除,并且不会与使用相同临时表名称的其他连接发生冲突。
回答by Dmytro Shevchenko
Temporary tables are per database connection. So if you're trying to access a temporary table from another connection, you won't see it.
临时表是每个数据库连接。因此,如果您尝试从另一个连接访问临时表,您将看不到它。