发布网友 发布时间:2022-04-20 16:07
共1个回答
热心网友 时间:2023-07-17 10:50
//存储过程create or replace Procere countBySal(\x0d\x0a p_sal emp.sal%type,\x0d\x0a p_count OUT number\x0d\x0a)as \x0d\x0abegin\x0d\x0a select count(*) into p_count from emp where sal >= p_sql;\x0d\x0aend countBySal; //调用步奏import java.sql.CallableStatement; //带哦用存储过程所必须的语句借口\x0d\x0aimport java.sql.Connection;\x0d\x0aimport java.sql.DriverManager;\x0d\x0aimport java.sql.Types;public class EmpUtil {\x0d\x0a \x0d\x0a public static int countBySal(double sal) throws Exception{\x0d\x0a Class.forName("oracle.jdbc.driver.OracleDriver");\x0d\x0a String url="jdbc:oracle:thin:@localhost:1521:test";\x0d\x0a Connection cn=DriverManager.getConnection(url, "scott", "tiger");\x0d\x0a String sql="{call countBySal(?,?)}";//调用存储过程的语句,call后面的就是存储过程名和需要传入的参数\x0d\x0a CallableStatement cst=cn.prepareCall(sql);\x0d\x0a cst.setDouble(1, sal);//设置in参数的值\x0d\x0a cst.registerOutParameter(2, Types.INTEGER);//注册out参数的类型\x0d\x0a cst.execute();\x0d\x0a int result = cst.getInt(2);\x0d\x0a cst.close();\x0d\x0a cn.close();\x0d\x0a return result;\x0d\x0a }\x0d\x0a \x0d\x0a public static void main(String[] args) {\x0d\x0a int count;\x0d\x0a try {\x0d\x0a count = EmpUtil.countBySal(3000);\x0d\x0a System.out.println("工资在3000元以上的人数为:"+count);\x0d\x0a } catch (Exception e) {\x0d\x0a e.printStackTrace();\x0d\x0a }\x0d\x0a \x0d\x0a }\x0d\x0a \x0d\x0a}