java 调用通用存储过程(Sqlserver)

 

  1. package com.wfy.system.dao;   
  2.   
  3. import java.sql.CallableStatement;   
  4. import java.sql.Connection;   
  5. import java.sql.ResultSet;   
  6. import java.sql.SQLException;   
  7. import java.sql.Types;   
  8. import java.text.DateFormat;   
  9. import java.text.SimpleDateFormat;   
  10. import java.util.ArrayList;   
  11. import java.util.HashMap;   
  12. import java.util.List;   
  13. import java.util.Map;   
  14.   
  15. import org.springframework.dao.DataAccessException;   
  16. import org.springframework.jdbc.core.ConnectionCallback;   
  17. import org.springframework.jdbc.core.JdbcTemplate;   
  18.   
  19. import com.wfy.util.JDOM;   
  20.   
  21. /**  
  22.  
  23. * 存储过程通过此类调用  
  24.  
  25. * @author 金鑫  
  26.  
  27. */  
  28. public class DynamicDataProcedureExecuteDAO {   
  29. private Map getSqlServerData(JdbcTemplate jdbcTemplate, final List<?> procedureList){   
  30.    Map returnMap = (Map) jdbcTemplate.execute(new ConnectionCallback(){   
  31.     public Map doInConnection(Connection conn) throws SQLException,   
  32.       DataAccessException {   
  33.      /**  
  34.      * 由于sqlserver跟oracle 的调用方式有所不同,所以需要分开调用,但是我对oracle存储过程不熟悉,所以没有写,以后会补上的。  
  35.      * 我的通用方式是将传入数据进行封装,再将传出数据封装为map返回给用户  
  36.      *   
  37.      * List.get(0) 中存放的是 存储过程的名称  
  38.      *   
  39.      * 从list.get(1)开始存放的是相应参数,参数以map类型存放  
  40.      *   
  41.      * map.put("name","")//传入传出的参数名字  
  42.      * map.put("value","")//传入的参数值,传出为null  
  43.      * map.put("type","input/output/outtable");//传入 传出 输出的结果集  
  44.      * map.put("dataType",type);//传入传出的数据类型。  
  45.      *   
  46.      * 返回的时候将type类型为output的封装为map返回前台。  
  47.      *   
  48.      * */  
  49.        
  50.      //生成存储过程调用字符串   
  51.      String callStr = "{call ";   
  52.      callStr += procedureList.get(0).toString()+"(";   
  53.      for (int i = 1; i < procedureList.size(); i++) {   
  54.       if(!((Map)procedureList.get(i)).get("type").toString().equals("outtable"))   
  55.       callStr += "?,";   
  56.      }   
  57.      if(procedureList.size()>1){   
  58.       callStr = callStr.substring(0,callStr.length()-1);   
  59.      }   
  60.      callStr += ")}";   
  61.        
  62.      System.out.println("callStr:"+callStr);   
  63.        
  64.      CallableStatement cstmt = conn.prepareCall(callStr);   
  65.        
  66.      for (int j = 1; j < procedureList.size(); j++) {   
  67.       Map<String, String> map = (Map<String, String>)procedureList.get(j);   
  68.       if(map.get("type").equals("input")){   
  69.        //说明此函数是传入函数   
  70.        if(map.get("dataType").toUpperCase().equals("STRING")){   
  71.         cstmt.setString(j, map.get("value"));   
  72.        }else if(map.get("dataType").toUpperCase().equals("INT")){   
  73.         try {   
  74.          cstmt.setInt(j, Integer.parseInt(map.get("value").toString()));    
  75.         } catch (Exception e) {   
  76.          throw new SQLException(map.get("value") + " INT 数据类型转换错误");   
  77.         }   
  78.        }else if(map.get("dataType").toUpperCase().equals("DECIMAL")){   
  79.         try {   
  80.          cstmt.setDouble(j, Double.parseDouble(map.get("value").toString()));   
  81.         } catch (Exception e) {   
  82.          throw new SQLException(map.get("value") + " DECIMAL 数据类型转换错误");   
  83.         }   
  84.        }else if(map.get("dataType").toUpperCase().equals("DATE")){   
  85.         try{   
  86.          DateFormat formatter1= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  87.          cstmt.setDate(j, new java.sql.Date(formatter1.parse(map.get("value").toString()).getTime()));   
  88.         }catch(Exception e1){   
  89.          try{   
  90.           DateFormat formatter2= new SimpleDateFormat("yyyy-MM-dd");   
  91.           cstmt.setDate(j, new java.sql.Date(formatter2.parse(map.get("value").toString()).getTime()));   
  92.          }catch(Exception e2){   
  93.           throw new SQLException(map.get("value") + " DATE 数据类型转换错误");   
  94.          }   
  95.         }   
  96.        }   
  97.       }else if(map.get("type").equals("output")){   
  98.        //说明此函数是返回函数   
  99.        if(map.get("dataType").toUpperCase().equals("STRING")){   
  100.         cstmt.registerOutParameter(j, Types.CHAR);   
  101.        }else if(map.get("dataType").toUpperCase().equals("INT")){   
  102.         cstmt.registerOutParameter(j, Types.INTEGER);   
  103.        }else if(map.get("dataType").toUpperCase().equals("DECIMAL")){   
  104.         cstmt.registerOutParameter(j, Types.DECIMAL);   
  105.        }else if(map.get("dataType").toUpperCase().equals("DATE")){   
  106.         cstmt.registerOutParameter(j, Types.DATE);   
  107.        }   
  108.       }   
  109.      }   
  110.      //返回的map   
  111.      Map<String, Object> returnMap = new HashMap<String, Object>();   
  112.      cstmt.execute();   
  113.        
  114.      int k = 1;   
  115.      while(cstmt.getMoreResults()){   
  116.       ResultSet rs = cstmt.getResultSet();   
  117.       List ls = new ArrayList();   
  118.       while(rs.next()){   
  119.        Map vc = new HashMap();   
  120.        for(int i=1;i<rs.getMetaData().getColumnCount()+1;i++){   
  121.         vc.put(rs.getMetaData().getColumnName(i), rs.getObject(i));   
  122.        }   
  123.        ls.add(vc);   
  124.       }   
  125.       for (; k < procedureList.size(); k++) {   
  126.        Map mp = (Map)procedureList.get(k);   
  127.        if(mp.get("type").equals("outtable")){   
  128.         returnMap.put(mp.get("name").toString(), ls);   
  129.         k++;   
  130.         break;   
  131.        }   
  132.       }   
  133.      }   
  134.        
  135.      for (int j = 1; j < procedureList.size(); j++) {   
  136.       Map<String, String> map = (Map<String, String>)procedureList.get(j);   
  137.       if(map.get("type").equals("output")){   
  138.        //说明此函数是返回函数   
  139.        if(map.get("dataType").toUpperCase().equals("STRING")){   
  140.         returnMap.put(map.get("name").toString(), cstmt.getString(j));   
  141.        }else if(map.get("dataType").toUpperCase().equals("INT")){   
  142.         returnMap.put(map.get("name").toString(), cstmt.getInt(j));   
  143.        }else if(map.get("dataType").toUpperCase().equals("DECIMAL")){   
  144.         returnMap.put(map.get("name").toString(), cstmt.getDouble(j));   
  145.        }else if(map.get("dataType").toUpperCase().equals("DATE")){   
  146.         returnMap.put(map.get("name").toString(), cstmt.getDate(j));   
  147.        }   
  148.       }   
  149.      }   
  150.        
  151.      cstmt.close();   
  152.        
  153.      return returnMap;   
  154.     }   
  155.    });   
  156.    return returnMap;   
  157. }   
  158. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
注意,以下使用数据库为sql2000,驱动jtds1.2.2 一、调用存储过程(无结果集返回) Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?,?) }"); callableStatement.setString(1, "xxxxxxxx"); callableStatement.setString(2, "xxxxxxxx"); callableStatement.execute(); //获得sql的消息并输出,这个估计很多人都需要 SQLWarning sqlWarning = callableStatement.getWarnings(); while (sqlWarning != null) { System.out.println("sqlWarning.getErrorCode() = " + sqlWarning.getErrorCode()); System.out.println("sqlWarning.getSQLState() = " + sqlWarning.getSQLState()); System.out.println("sqlWarning.getMessage() = " + sqlWarning.getMessage()); sqlWarning = sqlWarning.getNextWarning(); } //close ConnectionHelper.closeConnection(callableStatement, connection); 二、调用存储过程,返回sql类型数据(非记录集) Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?,?,?) }"); callableStatement.setString(1, "xxxxxxxx"); callableStatement.setString(2, "xxxxxxxx"); //重点是这句1 callableStatement.registerOutParameter(3, Types.INTEGER); callableStatement.execute(); //取返回结果,重点是这句2 //int rsCount = callableStatement.getInt(3); //close ConnectionHelper.closeConnection(callableStatement, connection); 三、重点来了,返回记录集,多记录集 注意,不需要注册返回结果参数,只需要在sql中select出结果即可 例如:select * from tableName 即可得到返回结果 Connection connection = ConnectionHelper.getConnection(); CallableStatement callableStatement = connection.prepareCall("{ call procedureName(?) }"); //此处参数与结果集返回没有关系 callableStatement.setString(1, "xxxxxxxx"); callableStatement.execute(); ResultSet resultSet = callableStatement.getResultSet(); //以上两个语句,可以使用ResultSet resultSet = callableStatement.executeQuery();替代 //多结果返回 ResultSet resultSet2; if (callableStatement.getMoreResults()) { resultSet2 = callableStatement.getResultSet(); while (resultSet2.next()) { } } //close ConnectionHelper.closeConnection(callableStatement, connection); 提示:多结果返回可以使用如下代码(以上主要让
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值