拥有Java I can';t插入数据
我试图用Java插入我的postgres数据库。我有本地数据库的默认配置 我想把一些数据放在一个表中,我有一些问题 代码如下:拥有Java I can';t插入数据,java,postgresql,sql-insert,quoted-identifier,Java,Postgresql,Sql Insert,Quoted Identifier,我试图用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.Logge
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);
}
}
}
这里是例外情况
SEVERE: ERROR: relation "test2" does not exist
Position: 13
org.postgresql.util.PSQLException: ERROR: relation "test2" does not exist
Position: 13
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2101)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1834)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:510)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:386)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:332)
at test.Test.main(Test.java:30)
严重:错误:关系“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不存在。尝试登录PostgreSQL并检查此表 可以使用命令行实用程序列出数据库中的所有现有表
psql -d postgres
\dt
您的表称为
TEST2
而不是TEST2
。显然,您是使用双引号创建的,这使得Postgres(和其他符合标准的DBMS)区分大小写
因此,您现在必须在每次引用该表时将其名称用双引号括起来
String stm = "INSERT INTO \"TEST2\"(ID) VALUES(?)";
很可能这不是您想要的,因此只需在标识符周围使用双引号重新创建表,而不使用双引号:
CREATE TABLE test2
(
...
)
创建不同于以下内容的表:
CREATE TABLE "test2"
(
...
)
如果不想重新创建表,可以重命名它们:
alter table "TEST2" rename to test2;
检查创建表test2的模式。 如果它是不在搜索路径中的模式之一,则有两个选项:-
- 将模式添加到搜索路径(postgres.conf文件中以逗号分隔的列表)
- 通过在表名前面加上架构名和点来引用表。
例如,如果相关表test2位于my_模式中,那么您的查询应该是插入my_模式中的
。test2(ID)值(?)
test2
。非常感谢!你知道如何在不重新制作所有表格的情况下解决这个问题吗?我也用pgadmin做了所有表格。我没有放任何东西quotes@thanassis:您肯定在某个地方使用了引号,否则该表就不会以这种方式创建。有关如何重命名它们,请参见我的编辑。