需要插入数据库表

时间:2013-07-17 07:26:50

标签: java postgresql

我在输入时收到错误,因此请避免错误并存储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)

我也附加了商店程序。如何避免错误并存储表。

1 个答案:

答案 0 :(得分:1)

您已使用RETURNS SETOF record定义了该函数,但函数体不返回任何内容。看看你的java代码,你也不希望它返回任何东西。 (使用executeUpdate)

将函数更改为RETURNS VOID,以便Postgres知道不会返回任何内容。