日期:2014-05-20 浏览次数:20854 次
import java.util.*; import java.sql.*; public class CallProcedure { public static void main(String args[]) { String url = "jdbc:odbc:stu"; Connection con; String sql; Statement stmt; int num; String name,sex; int age; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e.getMessage()); } try { con = DriverManager.getConnection(url, "system", "system"); stmt = con.createStatement(); //向student表中插入一行录记录 Scanner in=new Scanner(System.in); System.out.println("\n输入学生的自然信息"); System.out.println("-----------------------------"); System.out.print("学号:"); num=in.nextInt(); System.out.print("年龄:"); age=in.nextInt(); CallableStatement proc; proc=con.prepareCall("{call updateage(?,?,?)}"); proc.setInt(1,num); proc.setInt(2,age); proc.registerOutParameter(3, Types.VARCHAR); proc.execute(); String varNumber= proc.getString(3); System.out.println("返回参数是: "+varNumber); if (varNumber.compareTo("0")==0) System.out.println("对学号"+num+"更新成功!"); if (varNumber.compareTo("1")==0) System.out.println("年龄偏大,非神童也,嘿嘿~,更新无效呀!"); if (varNumber.compareTo("2")==0) System.out.println("学号"+num+"不存在,更新失败!"); con.commit(); //关闭连接。 stmt.close(); con.close(); } catch(SQLException ex) { System.err.println("SQLException: " + ex.getMessage()); } } }
proc.registerOutParameter(3, Types.VARCHAR);