【发布时间】:2012-10-03 16:44:30
【问题描述】:
我尝试使用 Java 插入我的 postgres 数据库。我的本地数据库有默认配置。
我想将一些数据放在一个表中,但我遇到了一些问题。
代码如下:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
public static void main(String[] args) {
Connection con = null;
PreparedStatement pst = null;
String url = "jdbc:postgresql://localhost/postgres";
String user = "postgres";
String password = "thanassis";
try {
con = DriverManager.getConnection(url, user, password);
String stm = "INSERT INTO TEST2(ID) VALUES(?)";
pst = con.prepareStatement(stm);
pst.setInt(1, 1);
pst.executeUpdate();
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(PreparedStatement.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (pst != null) {
pst.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(PreparedStatement.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
这里有例外
严重:错误:关系“test2”不存在 职位:13 org.postgresql.util.PSQLException:错误:关系“test2”不存在 职位:13 在 org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101) 在 org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834) 在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255) 在 org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510) 在 org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386) 在 org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:332) 在 test.Test.main(Test.java:30)【问题讨论】:
-
插入数据前必须先创建表
test2。
标签: java postgresql sql-insert quoted-identifier