如何解决此Java与PostgreSQL错误(表不存在)?

时间:2018-11-26 11:41:10

标签: java postgresql jdbc sql-insert

我使用PostgreSQL 10和NetBeans 8.2 IDE。

我需要将值插入到Postgres表中,但是无法插入。

我的Java代码。

String url = "jdbc:postgresql://localhost:5432/postgres";
String user = "postgres";
String password = "1111"; 
String status=Status,when=When,code=Code,msg=Msg,description=Description,elapsed=Elapsed,mhs_av=MHS_av,mhs_5s=MHS_5s,mhs_1m=MHS_1m,mhs_5m=MHS_5m,mhs_15m=MHS_15m,found_blocks=Found_Blocks,getworks=Getworks,accepted=Accepted,rejected=Rejected;
String hardware_errors=Hardware_Errors,utility=Utility,discarded=Discarded,stale=Stale,get_failures=Get_Failures,local_work=Local_Work,remote_failures=Remote_Failures;
String network_blocks=Network_Blocks,total_mh=Total_MH,work_utility=Work_Utility,difficulty_accepted=Difficulty_Accepted,difficulty_rejected=Difficulty_Rejected,difficulty_stale=Difficulty_Stale;
String best_share=Best_Share,device_hardware=Device_Hardware,device_rejected=Device_Rejected,pool_rejected=Pool_Rejected,pool_stale=Pool_Stale,last_getwork=Last_Getwork;

String query = "INSERT INTO SUMMARY(status,whn,code,msg,description,elapsed,mhs_av,mhs_5s,mhs_1m,mhs_5m,mhs_15m,found_blocks,get_works,accepted,rejected,"
        + "hardware_errors,utility,discarded,stale,get_failures,local_work,remote_failures,network_blocks,total_mh,work_utility,difficulty_accepted,difficulty_rejected,"
        + "difficulty_stale,best_share,device_hardware,device_rejected,pool_rejected,pool_stale,last_getwork) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

try (Connection con = DriverManager.getConnection(url, user, password);
                PreparedStatement pst = con.prepareStatement(query)) {
                pst.setString(1,status);
                pst.setString(2,when); pst.setString(3, code);  pst.setString(4, msg); pst.setString(5,description); pst.setString(6,elapsed); pst.setString(7, mhs_av); pst.setString(8, mhs_5s);
                pst.setString(9,mhs_1m); pst.setString(10, mhs_5m); pst.setString(11, mhs_15m); pst.setString(12, found_blocks); pst.setString(13, getworks); pst.setString(14, accepted);
                pst.setString(15, rejected); pst.setString(16, hardware_errors); pst.setString(17,utility); pst.setString(18, discarded); pst.setString(19, stale); pst.setString(20, get_failures);
                pst.setString(21, local_work); pst.setString(22, remote_failures); pst.setString(23, network_blocks); pst.setString(24, total_mh); pst.setString(25, work_utility); 
                pst.setString(26, difficulty_accepted); pst.setString(27, difficulty_rejected); pst.setString(28, difficulty_stale); pst.setString(29, best_share); 
                pst.setString(30, device_hardware); pst.setString(31, device_rejected); pst.setString(32, pool_rejected); pst.setString(33, pool_stale); pst.setString(34, last_getwork);
                pst.executeUpdate();

                } catch (SQLException ex) {

                    Logger lgr = Logger.getLogger(Parse.class.getName());
                    lgr.log(Level.SEVERE, ex.getMessage(), ex);
                }

我总是会收到此错误

  

Kas 26,2018 2:32:44 PM解析摘要   严重:错误:关系“摘要”不存在     位置:13   org.postgresql.util.PSQLException:错误:关系“摘要”不存在     位置:13       在org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2440)       在org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2183)       在org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:308)       在org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)       在org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)       在org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:143)       在org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:120)       在parse.summary.sum(summary.java:46)       在parse.Parse.display(Parse.java:323)       在parse.Parse.process(Parse.java:960)       在parse.Parse(Parse.java:981)       在parse.Parse.main(Parse.java:1007)

2 个答案:

答案 0 :(得分:0)

该错误消息可能是由以下几种原因引起的:

  1. 您连接到错误的主机,端口或数据库,并且那里没有表summary

  2. 您在正确的数据库中,但是表summary的模式不在您的search_path上。

  3. 您在正确的数据库中,并且search_path是正确的,但是该表未称为summary。请记住,PostgreSQL会将所有未加引号的标识符折叠为小写。因此,如果您的表名为SummarySUMMARY,则将其称为"Summary""SUMMARY"

答案 1 :(得分:0)

我解决了这个问题。我的表名在数据库的“”操作中。

enter image description here

我这样使用\"summary\"

String query = "INSERT INTO  \"Summary\"(status,whn,code,msg,description,elapsed,mhs_av,mhs_5s,mhs_1m,mhs_5m,mhs_15m,found_blocks,get_works,accepted,rejected,"
    + "hardware_errors,utility,discarded,stale,get_failures,local_work,remote_failures,network_blocks,total_mh,work_utility,difficulty_accepted,difficulty_rejected,"
    + "difficulty_stale,best_share,device_hardware,device_rejected,pool_rejected,pool_stale,last_getwork) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

`