PostgreSQL和JDBC“查询返回了多个结果集”

时间:2020-09-23 06:37:37

标签: java postgresql jdbc

我目前正在研究一个实践项目,以学习PostgreSQL。我想使用以下方法来获取客户编号分配。在客户表中添加新客户时,应连续分配客户编号(1,2,3,4,...)。变量“ anz”包含表中的客户数量。

如果删除了一个客户,则该客户编号将再次变为空闲,并且可以再次分配。例如,客户编号3再次变为空闲,并且getIndex()中的FOR循环仅运行到3。然后分配该编号。 。如果所有号码都被占用,则分配下一个更大的号码。因此,如果分配了1,2,3,则选择4。 函数checkEx()检查表中是否已经存在每个数字(1,2,3,4,...)。

我知道我可以用Java来实现,但是我想对它进行编程以在数据库中进行实践。 不幸的是,我总是收到错误“查询返回了多个结果集”。这些函数包含几个Select查询,我将其替换为PERFORM,但没有成功。

对不起,如果听起来有点复杂,我希望这是可以理解的。 谁能帮我吗?

public static int getKdNr() throws SQLException {
    

            String sql =
            "SELECT * FROM getIndex(); " +
        
            // getIndex()
            "CREATE OR REPLACE FUNCTION getIndex() RETURNS integer AS $BODY$ " +
                                    
                "DECLARE " +
    
                   "inx integer := 0; "                          +
                   "check integer := 0; " +
                   "anz integer := 0; " + 
                                         
                "BEGIN " + 
                
                "PERFORM 'SELECT COUNT(1) FROM customer INTO anz'; " +
                
                            "FOR  i IN 1..anz LOOP "              +
                            //"check := checkVorh(i); "          + 

                              "PERFORM 'SELECT checkEx(i) INTO check'; " + 

                               "IF      check=1 THEN inx := i; "          +
                               "ELSE                 inx := anz+1; "            +
                               "END IF; "                         +          
                            "END LOOP; "                          +
        
                        "RETURN inx; "                            +
                "END; "                                           +
                    
            "$BODY$ LANGUAGE plpgsql; " +
            
            // checkEx(integer)
            "CREATE OR REPLACE FUNCTION checkEx(integer) RETURNS integer AS $BODY$ " +
        
                "DECLARE " +
                
                    "ct integer; " +
                
                "BEGIN " +
                       
                    "PERFORM COUNT(1) INTO ct "      + 
                    "FROM customer "                            +                 
                    "WHERE unique_key = $1; "            +
                   
                    "RETURN ct; "             + 
                   
                "END; " +
            
            "$BODY$ LANGUAGE plpgsql; " ; 

        
    ResultSet rs = stmt.executeQuery(sql);

    return rs.getInt(1);
}

编辑:

我现在已经更改了程序,但是if语句无法正常工作。 但是我不明白为什么。

public static int getKdNr() throws SQLException {
    
String sql = "SELECT * FROM getIndex(); ";
                
ResultSet rs = stmt.executeQuery(sql);
rs.next();
return rs.getInt(1);
}


public static void createFunctions() throws SQLException {

stmt = conn.createStatement();

// checkEx(INTEGER)
    String sqlCheckEx = "CREATE OR REPLACE FUNCTION checkEx(INTEGER) RETURNS INTEGER AS $BODY$ " +

        "DECLARE " +
        
            "ct INTEGER; " +
        
        "BEGIN " +
               
            "PERFORM COUNT(1) INTO ct "      + 
            "FROM customer "                            +                 
            "WHERE unique_key = $1; "            +
           
            "RETURN ct; "             + 
           
        "END; " +
    
    "$BODY$ LANGUAGE plpgsql; " ; 
    
    stmt.executeUpdate(sqlCheckEx);
    }

    


// getIndex()
String sqlIndex = "CREATE OR REPLACE FUNCTION getIndex() RETURNS INTEGER AS $BODY$ " +
    
                        
    "DECLARE " +

       "inx INTEGER   := 1; "                          +
       "check INTEGER := 0; " +
    
                     
    "BEGIN " + 
            
                "FOR  i IN 1.." + getAnzKd() + " LOOP "              +
                   "check := checkEx(i); "                           + 


                               "IF     check>0 THEN     inx := i; "                         +
                               "ELSE                  inx := " + (getAnzKd() +1) + "; "   +
                               "END IF; "                                               +     
                                       
                 "END LOOP; "                                              +
                        
            "RETURN inx; "                            +
            
    "END; " +
                                      
        
    
"$BODY$ LANGUAGE plpgsql; ";

stmt.executeUpdate(sqlIndex);

IO.println("\n\n--> Funktionen erstellt");}

1 个答案:

答案 0 :(得分:0)

您应该将SQL分为3个单独的查询,并为每个查询分别调用executeQuery