2013-07-17 67 views
0

我同時也讓輸入了錯誤,因此避免錯誤和存儲postgre數據庫table_sp table需要插入數據庫表

 package mypkg; 

     import java.sql.CallableStatement; 
     import java.sql.Connection; 
     import java.sql.DriverManager; 
     import java.sql.ResultSet; 
     import java.sql.SQLException; 
     import java.sql.Statement; 
     import java.util.Scanner; 

     public class PostgreSp { 

      public static void main(String args[]) throws SQLException, ClassNotFoundException { 
       Connection cn; 
       Statement st; 
       ResultSet rs; 

       try { 
        Scanner in = new Scanner(System.in); 

        System.out.println("Enter a PK"); 
        String pk = in.nextLine(); 
        System.out.println("Enter a user"); 
        String u = in.nextLine(); 
        System.out.println("Enter a pass"); 
        String ps = in.nextLine(); 
        Class.forName("org.postgresql.Driver"); 
        cn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/samples", "postgres", "analuo"); 

        CallableStatement calstat = cn.prepareCall("{call new(?,?,?)}"); 
        calstat.setString(1, pk); 
        calstat.setString(2, u); 
        calstat.setString(3, ps); 
        int i = calstat.executeUpdate(); 
        cn.close(); 
        calstat.close(); 
        System.out.println("Your data has been inserted into table."); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

存儲過程:

CREATE OR REPLACE FUNCTION new(pk character varying, u character varying,ps character varying) 
     RETURNS SETOF record AS 
    $BODY$ 
    BEGIN 

    RAISE NOTICE 'PK is %','--'pk; 
    EXECUTE 'INSERT INTO table_sp("pk_id","username","password")VALUES ('''||pk||''' ,'''||u||''','''||ps||''')' ; 
    END; 
    $BODY$ 
     LANGUAGE plpgsql VOLATILE 
     COST 100 
     ROWS 1000; 
    ALTER FUNCTION new(character varying, character varying,character varying) 
     OWNER TO postgres; 

輸出示例:

run: 
Enter a PK 
1 
Enter a user 
admin 
Enter a pass 
admin 
org.postgresql.util.PSQLException: ERROR: a column definition list is required for functions returning "record" 
    Position: 15 
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2103) 
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1836) 
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:512) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388) 
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:330) 
    at mypkg.PostgreSp.main(PostgreSp.java:37) 
BUILD SUCCESSFUL (total time: 36 seconds) 

我也附加存儲過程。如何避免錯誤並存儲表格。

+3

當我在谷歌搜索'錯誤:函數返回需要列定義列表'它作爲第一個答案提出這個:http://stackoverflow.com/questions/8605174/postgresql-error-42601- a-column-definition-list-is-required-for-functions-ret - Google是一個開始尋找答案的好地方 – DaveRlz

回答

1

您已經使用RETURNS SETOF record定義了函數,但函數體不返回任何內容。看着你的java代碼,你不希望它返回任何東西。 (使用executeUpdate)

將函數更改爲RETURNS VOID,以便Postgres知道什麼都不會返回。