Jdbc调用存储过程

jdbc调用存储过程:

经常使用的4种:

                     1. 返回结果集的proc

                     2. 输出参数

                     3.使用带有返回状态的存储过程 

                     4.受影响行数

 

以下为mysql的存储过程 sqlServer同理

案例1: 返回结果集的proc

存储过程:

drop procedure if exists proc_selectEmployee;

create procedure proc_selectEmployee(in carid varchar(20))

begin

    select * from employee where cardID = carid;

end

 

call proc_selectEmployee('SZ65380');

 

 

[java] view plain copy
 
  1. import java.sql.*;  
  2.   
  3. import java.sql.CallableStatement;  
  4.   
  5. /** 
  6.  *  
  7.  * 简单的jdbc调用存储过程 只有输入参数 返回单个结果集 
  8.  *  
  9.  */  
  10. public class GeTest1 {  
  11.     public static void main(String[] args) {  
  12.   
  13.         Connection connection = null;  
  14.         //用于执行 SQL 存储过程的接口  
  15.         CallableStatement statement = null;  
  16.         ResultSet resultSet = null;  
  17.   
  18.         try {  
  19.             Class.forName("com.mysql.jdbc.Driver");  
  20.             String url = "jdbc:mysql://localhost:3306/test";  
  21.   
  22.             String user = "root";  
  23.   
  24.             String password = "123";  
  25.   
  26.             connection = DriverManager.getConnection(url, user, password);  
  27.   
  28.             String sql = "call proc_selectEmployee(?)";  
  29.   
  30.             //调用存储过程  
  31.             statement = connection.prepareCall(sql);  
  32.   
  33.             statement.setString(1, "SZ65380");  
  34.   
  35.             resultSet = statement.executeQuery();  
  36.   
  37.             if (resultSet.next()) {  
  38.                 System.out.println(resultSet.getString("address"));  
  39.             }  
  40.   
  41.         } catch (ClassNotFoundException e) {  
  42.             e.printStackTrace();  
  43.         } catch (SQLException e) {  
  44.             e.printStackTrace();  
  45.         } catch (Exception e) {  
  46.             e.printStackTrace();  
  47.         } finally {  
  48.             try {  
  49.                 if (resultSet != null) {  
  50.                     resultSet.close();  
  51.                 }  
  52.                 if (statement != null) {  
  53.                     statement.close();  
  54.                 }  
  55.                 if (connection != null) {  
  56.                     connection.close();  
  57.                 }  
  58.             } catch (SQLException e) {  
  59.                 e.printStackTrace();  
  60.             }  
  61.         }  
  62.     }  
  63. }  

 

 

 

案例2: 输出参数

 

存储过程:

drop procedure if exists proc_outtwo;

create procedure proc_outtwo(in idint int,out cardIdstring varchar(44),out addressstringvarchar(88))

begin

    select cardID,address into cardIdstring,addressstring from employee where id =idint;

end

 

call proc_outtwo(1,@one,@two);

select @one;

select @two;

 

 

[java] view plain copy
 
  1. import java.sql.*;  
  2.   
  3. import java.sql.CallableStatement;  
  4.   
  5. /** 
  6.  *  
  7.  * 执行存储过程 得到输出参数 
  8.  *  
  9.  */  
  10. public class GeTest2 {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.   
  17.         Connection connection = null;  
  18.         //用于执行 SQL 存储过程的接口  
  19.         CallableStatement statement = null;  
  20.   
  21.         ResultSet resultSet = null;  
  22.   
  23.         try {  
  24.             Class.forName("com.mysql.jdbc.Driver");  
  25.             String url = "jdbc:mysql://localhost:3306/test";  
  26.   
  27.             String user = "root";  
  28.   
  29.             String password = "123";  
  30.   
  31.             connection = DriverManager.getConnection(url, user, password);  
  32.   
  33.               
  34.             //第一个为输入参数后面2个为输出参数  
  35.             String sql = "call proc_outtwo(?,?,?);";  
  36.   
  37.             //调用存储过程  
  38.             statement = connection.prepareCall(sql);  
  39.   
  40.             //设置输入参数  
  41.             statement.setInt(1, 1);  
  42.   
  43.             //设置输出参数 以及类型  
  44.             statement.registerOutParameter(2, Types.VARCHAR);  
  45.   
  46.             statement.registerOutParameter(3, Types.VARCHAR);  
  47.   
  48.             statement.execute();  
  49.   
  50.             //得到输出参数  
  51.             System.out.println(statement.getString(2));  
  52.   
  53.             System.out.println(statement.getString(3));  
  54.   
  55.         } catch (ClassNotFoundException e) {  
  56.             e.printStackTrace();  
  57.         } catch (SQLException e) {  
  58.             e.printStackTrace();  
  59.         } catch (Exception e) {  
  60.             e.printStackTrace();  
  61.         } finally {  
  62.             try {  
  63.                 if (resultSet != null) {  
  64.                     resultSet.close();  
  65.                 }  
  66.                 if (statement != null) {  
  67.                     statement.close();  
  68.                 }  
  69.                 if (connection != null) {  
  70.                     connection.close();  
  71.                 }  
  72.             } catch (SQLException e) {  
  73.                 e.printStackTrace();  
  74.             }  
  75.         }  
  76.     }  
  77. }  

 

 

 

案例3:

使用带有返回状态的存储过程  return 1;  mysql的proc不支持 返回值 sqlserver支持

如果要获得返回值的话为:

存储过程:

create proc checkit

(@addressString varchar(50))

as

begin

if ((select count(*) from employee where address =@addressString))

    return 1

else

    return 0

go

 

 

[java] view plain copy
 
  1. CallableStatement cstmt = con.prepareCall("{? = call checkit(?)}");  
  2.       cstmt.registerOutParameter(1, java.sql.Types.INTEGER);  
  3.       cstmt.setString(2, "深圳");  
  4.       cstmt.execute();  
  5.       System.out.println("return的值" + cstmt.getInt(1));  

 

 

 

案例4:

获得更新行数:

 

drop procedure if exists proc_updateEmployee;

create procedure proc_updateEmployee()

begin

    update Employee set job=1;

end

 

call proc_selectEmployee();

 

[java] view plain copy
 
  1. CallableStatement cstmt = con.prepareCall("{call proc_updateEmployee()}");  
  2.       cstmt.execute();  
  3.       int count = cstmt.getUpdateCount();  
  4.       cstmt.close();  
  5.       System.out.println("受影响行数:" + count);   

 

posted on 2017-11-26 23:15  waao·····  阅读(126)  评论(0编辑  收藏  举报

导航