jdbc调用返回参数的存储过程Demo

来源:互联网 发布:不要网络的混乱大枪战 编辑:程序博客网 时间:2024/06/15 16:29
package demo;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;/**  * @author Administrator  * @date 2015年12月2日 下午5:01:22  * @description TODO */public class JdbcDemo2 {public static void main(String[] args) throws Exception {  System.out.println("---------测试调用存储过程-------");            Connection conn = null;         CallableStatement callStmt = null;     String url = "jdbc:mysql://localhost:3306/";          String db = "testdatabase";         String driver = "com.mysql.jdbc.Driver";         String user = "root";         String pass = "root";                try {             Class.forName(driver);             conn = DriverManager.getConnection(url + db, user, pass);             callStmt = conn.prepareCall("{call aa(?)}");                        //注册返回参数           callStmt.registerOutParameter(1, java.sql.Types.INTEGER);              callStmt.execute();      int num = callStmt.getInt(1);      /**  对应的存储过程    *  delimiter //;    *  create procedure aa(out int num)    *  begin    *  set num =2;    *  end;    *  //    *  delimit ;    */      System.out.println("获取的结果是:" + num);      System.out.println("---------测试调用存储过程结束-------");            } catch(Exception e) {       e.printStackTrace();       } finally {       callStmt.close();       conn.close();       }}}

0 0
原创粉丝点击