Some print.
Some print.
~SingleForum~无废话记录~ + ~19~记录点有用的好吗?少说废话少装b~

~SingleForum~无废话记录~ » java

Spring调用存储过程一个小记录

Started 2 years ago by admin. | Tags: . .

Spring调用存储过程一个小记录

JAVA:
  1. public void testSpringPackage() {
  2.  
  3.     SpringStoredProce springStoredProce = new SpringStoredProce(this
  4.             .getJdbcTemplate());
  5.     List< Map > resultList = springStoredProce.execute(0, 10,
  6.             " and t.seed_name_cn like '%竹%' ");
  7.     for (int i = 0; i < resultList.size(); i++) {
  8.         Map rowMap = resultList.get(i);
  9.         final String userId = rowMap.get("seed_name_cn").toString();
  10.         final String name = rowMap.get("g_name_cn").toString();
  11.         final String password = rowMap.get("tagsids").toString();
  12.         System.out.println("USER_ID=" + userId + "\t name=" + name
  13.                 + "\t password=" + password);
  14.     }
  15.  
  16. }

JAVA:
  1. import java.sql.CallableStatement;
  2. import java.sql.Connection;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9.  
  10. import org.springframework.dao.DataAccessException;
  11. import org.springframework.jdbc.core.CallableStatementCallback;
  12. import org.springframework.jdbc.core.CallableStatementCreator;
  13. import org.springframework.jdbc.core.JdbcTemplate;
  14.  
  15. import com.rizon.eiss.util.StringUtils;
  16.  
  17. public class SpringStoredProce {
  18.  
  19.     private JdbcTemplate jdbcTemplate;
  20.  
  21.     public SpringStoredProce(JdbcTemplate jdbcTemplate) {
  22.         this.jdbcTemplate = jdbcTemplate;
  23.     }
  24.  
  25.     public List< Map > execute(long start_no, long end_no, String filter) {
  26.         List< Map > resultList = null;
  27.         try {
  28.             resultList = (List< Map >) this.jdbcTemplate.execute(
  29.                     new ProcCallableStatementCreator(start_no, end_no, filter),
  30.                     new ProcCallableStatementCallback());
  31.         } catch (DataAccessException e) {
  32.             throw new RuntimeException(
  33.                     "execute method error : DataAccessException "
  34.                             + e.getMessage());
  35.         }
  36.         return resultList;
  37.     }
  38.  
  39.     /**
  40.      * Create a callable statement in this connection.
  41.      */
  42.     private class ProcCallableStatementCreator implements
  43.             CallableStatementCreator {
  44.  
  45.         private long start_no;
  46.         private long end_no;
  47.         private String filter;
  48.  
  49.         public ProcCallableStatementCreator(long start_no, long end_no,
  50.                 String filter) {
  51.             this.start_no = start_no;
  52.             this.end_no = end_no;
  53.             this.filter = filter;
  54.         }
  55.  
  56.         public CallableStatement createCallableStatement(Connection conn) {
  57.  
  58.             CallableStatement cs = null;
  59.             try {
  60.                 cs = conn.prepareCall("{call TREE_PACKAGE.TREE_FIND(?,?,?,?)}");
  61.                 cs.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
  62.                 cs.setLong(2, start_no);
  63.                 cs.setLong(3, end_no);
  64.                 cs.setString(4, filter);
  65.             } catch (SQLException e) {
  66.                 throw new RuntimeException(
  67.                         "createCallableStatement method Error : SQLException "
  68.                                 + e.getMessage());
  69.             }
  70.             return cs;
  71.         }
  72.  
  73.     }
  74.  
  75.     private class ProcCallableStatementCallback implements
  76.             CallableStatementCallback {
  77.  
  78.         public ProcCallableStatementCallback() {
  79.         }
  80.  
  81.         public Object doInCallableStatement(CallableStatement cs) {
  82.             List< Map > resultsMap = new ArrayList< Map >();
  83.             try {
  84.                 cs.execute();
  85.                 ResultSet rs = (ResultSet) cs.getObject(1);
  86.                 while (rs.next()) {
  87.                     Map< String, Object > rowMap = new HashMap< String, Object >();
  88.                     rowMap.put("t_id", rs.getLong("t_id"));
  89.                     rowMap.put("status", StringUtils.Trim(rs.getString("status")));
  90.                     rowMap.put("tree_name", StringUtils.Trim(rs.getString("tree_name")));
  91.  
  92.                     rowMap.put("seed_name_cn", StringUtils.Trim(rs.getString("seed_name_cn")));
  93.                     rowMap.put("seed_name_en", StringUtils.Trim(rs.getString("seed_name_en")));
  94.                     rowMap.put("seed_comment", StringUtils.Trim(rs.getString("seed_comment")));
  95.  
  96.                     rowMap.put("gid", StringUtils.Trim(rs.getString("gid")));
  97.                     rowMap.put("g_name_cn", StringUtils.Trim(rs.getString("g_name_cn")));
  98.                     rowMap.put("g_name_en", StringUtils.Trim(rs.getString("g_name_en")));
  99.                     rowMap.put("g_class", StringUtils.Trim(rs.getString("g_class")));
  100.  
  101.                     rowMap.put("tagsname", StringUtils.Trim(rs.getString("tagsname")));
  102.                     rowMap.put("tagsids", StringUtils.Trim(rs.getString("tagsids")));
  103.                     rowMap.put("tags_des", StringUtils.Trim(rs.getString("tags_des")));
  104.  
  105.                     resultsMap.add(rowMap);
  106.                 }
  107.                 rs.close();
  108.             } catch (SQLException e) {
  109.                 throw new RuntimeException(
  110.                         "doInCallableStatement method error : SQLException "
  111.                                 + e.getMessage());
  112.             }
  113.             return resultsMap;
  114.         }
  115.     }
  116. }

RSS feed for this topic