package www.zjptcc.wxw.jdbctest.psql; /* * CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT prod int) AS $$ BEGIN sum := x + y; prod := x * y; END; $$ LANGUAGE plpgsql; 或 CREATE OR REPLACE FUNCTION sum_n_product (x int, y int, OUT sum int, OUT prod int) AS $$ SELECT $1 + $2, $1 * $2 $$ LANGUAGE SQL; * * */ import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types; public class TestPsqlSp1 { /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 try { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgresql://127.0.0.1:5432/you_db";// 数据库名 Connection conn = DriverManager.getConnection(url, "you_user","you_passwd"); /* sum_n_product(x int, y int, OUT sum int, OUT prod int) 是将输入参数x,y * 的和放入第三个参数sum(输出参数),x,y的积放入第四个参数prod(输出参数) * */ String procedure = "{ call sum_n_product(?, ?, ?, ?)}"; CallableStatement statement = conn.prepareCall(procedure); //通过 setXXX 方法将值传给IN参数 statement.setInt(1, 3); statement.setInt(2, 8); //registerOutParameter注册输出参数sum,prod,输出参数的索引值是3,4 //参数的索引值是根据占位符?出现的次序从左到右由1开始计,不管其是输入还是输出参数 statement.registerOutParameter(3,Types.INTEGER); statement.registerOutParameter(4,Types.INTEGER); statement.execute(); //打印输出参数sum的值 System.out.println("sum="+statement.getInt(3)); System.out.println("product="+statement.getInt(4)); conn.close(); } catch (SQLException sqe) { System.out.println("Unexpected exception : " + sqe.toString() + ", sqlstate = " + sqe.getSQLState()); System.exit(1); } catch (Exception e) { System.out.println(e.getMessage()); } } }
运行结果:
sum=11
product=24
例子二
package www.zjptcc.wxw.jdbctest.psql; /* create table t123(id int primary key, col2 varchar(32) not null); insert into t123 values(1, 'iihero'); insert into t123 values(2, 'Sybase'); insert into t123 values(3, 'ASE'); CREATE OR REPLACE FUNCTION test_proc (int, OUT num_t123 int) AS $$ DECLARE id_min ALIAS FOR $1; BEGIN select count( a.id ) into num_t123 from t123 a where a.id >= id_min; END $$ LANGUAGE plpgsql; mymotif=> select test_proc(1); mymotif=> drop FUNCTION test_proc(int, OUT num_t123 int); * * */ import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types; public class TestPsqlSp2 { /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 try { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgresql://127.0.0.1:5432/you_db";// 数据库名 Connection conn = DriverManager.getConnection(url, "you_user","you_passwd"); /* test_proc(@id_min int, @num_t123 int output))是将表t123中的id字段中大于等于第一个参数@id_min 的数目统计好,放到第二个参数@num_t123(输出参数) */ String procedure = "{ call test_proc(?, ?)}"; CallableStatement statement = conn.prepareCall(procedure); //通过 setXXX 方法将IN参数传给@id_min,其中第一个1是参数的索引值,第二个1是传给@id_min的值 statement.setInt(1, 1); //registerOutParameter注册输出参数@num_t123,输出参数的索引值是2 //参数的索引值是根据占位符?出现的次序从左到右由1开始计,不管其是输入还是输出参数 statement.registerOutParameter(2,Types.INTEGER); statement.execute(); //打印输出参数@num_t123的值 System.out.print(statement.getInt(2)); conn.close(); } catch (SQLException sqe) { System.out.println("Unexpected exception : " + sqe.toString() + ", sqlstate = " + sqe.getSQLState()); System.exit(1); } catch (Exception e) { System.out.println(e.getMessage()); } } }
例子三
package www.zjptcc.wxw.jdbctest.psql; /* * CREATE TABLE STUDENT (sno int, sname varchar(32)); insert into STUDENT values(1, '王海滨'); insert into STUDENT values(2, '李寻欢 '); insert into STUDENT values(3, '张铁头'); CREATE OR REPLACE FUNCTION sp_stu_sel() RETURNS refcursor AS $$ declare ref refcursor; BEGIN open ref for select * from STUDENT; return ref; END; $$ LANGUAGE plpgsql; * * */ import java.sql.CallableStatement; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; public class TestPsqlSpStu { /** * @param args */ public static void main(String[] args) { // TODO 自动生成的方法存根 try { Class.forName("org.postgresql.Driver").newInstance(); String url = "jdbc:postgresql://127.0.0.1:5432/you_db";// 数据库名 Connection conn = DriverManager.getConnection(url, "you_name", "you_passwd"); conn.setAutoCommit(false); // return refcursor must within a transaction String procedure = "{ ? = call sp_stu_sel()}"; CallableStatement statement = conn.prepareCall(procedure); // 参数的索引值是根据占位符?出现的次序从左到右由1开始计,不管其是输入还是输出参数 statement.registerOutParameter(1, Types.OTHER); statement.execute(); ResultSet result = (ResultSet) statement.getObject(1); System.out.println(result); while (result.next()) { System.err.println("Name : " + result.getString("sname")); //getString(2) } conn.commit(); // 打印输出返回值 conn.close(); } catch (SQLException sqe) { System.out.println("Unexpected exception : " + sqe.toString() + ", sqlstate = " + sqe.getSQLState()); System.exit(1); } catch (Exception e) { System.out.println(e.getMessage()); } } }
运行结果:
org.postgresql.jdbc4.Jdbc4ResultSet@c31d6f2
Name : 王海滨
Name : 李寻欢
Name : 张铁头