我想用 JDBC 输入一个命令来创建一个表,但是在第一次编译之后,当表已经生成时,每一个下一个都会抛出异常。我不明白这怎么可能,因为我在那里放了 [IF NOT EXISTS] 术语,所以应该没有 SQL 错误。
public class Test
{
public static void main(String[] args)
{
try
{
Connection conn = BazaDanych.Polacz();
Statement stat = conn.createStatement();
String command = "CREATE TABLE [IF NOT EXISTS] testowatabela2 (id INTEGER, wartosc DOUBLE PRECISION);";
stat.execute(command);
}
catch(SQLException e)
{
System.out.println("SQL Exception in Test");
}
}
}
最佳答案
IF NOT EXISTS
子句中的方括号可能逐字地来自文档:
http://www.postgresql.org/docs/9.1/static/sql-createtable.html
但它们不能出现在实际的 CREATE TABLE
语句中,因为括号表示该子句是可选的。
另一件需要考虑的事情是 IF NOT EXISTS
是 PostgreSQL 9.1 的新特性,所以它会在旧版本中失败。如果您不确定所使用的版本,请在 SQL 中运行:select version()
关于java - 表存在时出现SQLException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10652341/