jdbc简单封装

来源:互联网 发布:vmware nat 端口 编辑:程序博客网 时间:2024/06/05 00:44
  1. package frame.dao;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.PreparedStatement;   
  5. import java.sql.ResultSet;   
  6. import java.sql.SQLException;   
  7. import java.util.ArrayList;   
  8. import java.util.List;   
  9.   
  10. public abstract class BaseDao {   
  11.     private Connection getConnection() throws SQLException {   
  12.         return ConnectionFactory.getConnection();   
  13.     }   
  14.     /**  
  15.      * 获得PreapredStatement的方法  
  16.      *   
  17.      * @param sqlString  
  18.      * @return  
  19.      * @throws SQLException  
  20.      */  
  21.     protected PreparedStatement getPreparedStatement(String sqlString) throws SQLException {   
  22.         return getConnection().prepareStatement(sqlString);   
  23.     }   
  24.     /**  
  25.      * 根据条件取出前n条的方法  
  26.      *   
  27.      * @param sqlString  
  28.      * @param size  
  29.      * @return  
  30.      * @throws SQLException  
  31.      */  
  32.     protected PreparedStatement getPreparedStatement(String sqlString, int size) throws SQLException {   
  33.         return getConnection().prepareStatement(sqlString + " LIMIT " + size);   
  34.     }   
  35.     /**  
  36.      * 根据条件分页的方法  
  37.      *   
  38.      * @param sqlString  
  39.      * @param begin  
  40.      * @param size  
  41.      * @return  
  42.      * @throws SQLException  
  43.      */  
  44.     protected PreparedStatement getPreparedStatement(String sqlString, int begin, int size) throws SQLException {   
  45.         return getConnection().prepareStatement(sqlString + " LIMIT " + begin + "," + size);   
  46.     }   
  47.     /**  
  48.      * 释放PreparedStatement 资源,insert delete update 操作调用  
  49.      *   
  50.      * @param pstm  
  51.      */  
  52.     private void destoryResource(PreparedStatement pstm) {   
  53.         if (pstm != null) {   
  54.             try {   
  55.                 pstm.close();   
  56.             }   
  57.             catch (SQLException e) {   
  58.                 System.out.println(e.toString());   
  59.             }   
  60.         }   
  61.     }   
  62.     /**  
  63.      * 释放PreparedStatement 资源,select 调用  
  64.      *   
  65.      * @param pstm  
  66.      * @param rs  
  67.      */  
  68.     private void destoryResource(PreparedStatement pstm, ResultSet rs) {   
  69.         if (pstm != null) {   
  70.             try {   
  71.                 pstm.close();   
  72.             }   
  73.             catch (SQLException e) {   
  74.                 System.out.println(e.toString());   
  75.             }   
  76.         }   
  77.         if (rs != null) {   
  78.             try {   
  79.                 rs.close();   
  80.             }   
  81.             catch (SQLException e) {   
  82.                 System.out.println(e.toString());   
  83.             }   
  84.         }   
  85.     }   
  86.     /**  
  87.      * 执行sql insert delete update调用  
  88.      *   
  89.      * @param pstm  
  90.      * @throws SQLException  
  91.      */  
  92.     protected void execute(PreparedStatement pstm) throws SQLException {   
  93.         try {   
  94.             pstm.execute();   
  95.         }   
  96.         catch (SQLException e) {   
  97.             e.printStackTrace();   
  98.             throw new SQLException();   
  99.         }   
  100.         finally {   
  101.             this.destoryResource(pstm);   
  102.         }   
  103.     }   
  104.     /**  
  105.      * 批处理操作  
  106.      *   
  107.      * @param pstm  
  108.      * @throws SQLException  
  109.      */  
  110.     protected void executeBatch(PreparedStatement pstm) throws SQLException {   
  111.         try {   
  112.             pstm.executeBatch();   
  113.         }   
  114.         catch (SQLException e) {   
  115.             e.printStackTrace();   
  116.             throw new SQLException();   
  117.         }   
  118.         finally {   
  119.             this.destoryResource(pstm);   
  120.         }   
  121.     }   
  122.     /**  
  123.      * update 操作  
  124.      *   
  125.      * @param pstm  
  126.      * @return  
  127.      * @throws SQLException  
  128.      */  
  129.     protected int update(PreparedStatement pstm) throws SQLException {   
  130.         int flag = 0;   
  131.         try {   
  132.             flag = pstm.executeUpdate();   
  133.         }   
  134.         catch (SQLException e) {   
  135.             e.printStackTrace();   
  136.             throw new SQLException();   
  137.         }   
  138.         finally {   
  139.             this.destoryResource(pstm);   
  140.         }   
  141.         return flag;   
  142.     }   
  143.     /**  
  144.      * 获得一个对象的操作  
  145.      *   
  146.      * @param pstm  
  147.      * @param objCall  
  148.      * @return  
  149.      * @throws SQLException  
  150.      */  
  151.     protected T getObject(PreparedStatement pstm, ResultCall objCall) throws SQLException {   
  152.         T obj = null;   
  153.         ResultSet rs = null;   
  154.         try {   
  155.             rs = pstm.executeQuery();   
  156.             if (rs.next()) {   
  157.                 obj = objCall.getResultObject(rs);   
  158.             }   
  159.         }   
  160.         catch (SQLException e) {   
  161.             e.printStackTrace();   
  162.             throw new SQLException();   
  163.         }   
  164.         finally {   
  165.             this.destoryResource(pstm, rs);   
  166.         }   
  167.         return obj;   
  168.     }   
  169.     /**  
  170.      * 返回整形数据的操作  
  171.      *   
  172.      * @param pstm  
  173.      * @return  
  174.      * @throws SQLException  
  175.      */  
  176.     protected int getInt(PreparedStatement pstm) throws SQLException {   
  177.         int num = 0;   
  178.         ResultSet rs = null;   
  179.         try {   
  180.             rs = pstm.executeQuery();   
  181.             if (rs.next()) {   
  182.                 num = rs.getInt(1);   
  183.             }   
  184.         }   
  185.         catch (SQLException e) {   
  186.             e.printStackTrace();   
  187.             throw new SQLException();   
  188.         }   
  189.         finally {   
  190.             this.destoryResource(pstm, rs);   
  191.         }   
  192.         return num;   
  193.     }   
  194.     /**  
  195.      * 返回长整形数据的操作  
  196.      *   
  197.      * @param pstm  
  198.      * @return  
  199.      * @throws SQLException  
  200.      */  
  201.     protected long getLong(PreparedStatement pstm) throws SQLException {   
  202.         long num = 0;   
  203.         ResultSet rs = null;   
  204.         try {   
  205.             rs = pstm.executeQuery();   
  206.             if (rs.next()) {   
  207.                 num = rs.getLong(1);   
  208.             }   
  209.         }   
  210.         catch (SQLException e) {   
  211.             e.printStackTrace();   
  212.             throw new SQLException();   
  213.         }   
  214.         finally {   
  215.             this.destoryResource(pstm, rs);   
  216.         }   
  217.         return num;   
  218.     }   
  219.     protected boolean hasObject(PreparedStatement pstm) throws SQLException {   
  220.         boolean flag = false;   
  221.         ResultSet rs = null;   
  222.         try {   
  223.             rs = pstm.executeQuery();   
  224.             if (rs.next()) {   
  225.                 flag = true;   
  226.             }   
  227.         }   
  228.         catch (SQLException e) {   
  229.             e.printStackTrace();   
  230.             throw new SQLException();   
  231.         }   
  232.         finally {   
  233.             this.destoryResource(pstm, rs);   
  234.         }   
  235.         return flag;   
  236.     }   
  237.     /**  
  238.      * 返回一个集合的操作  
  239.      *   
  240.      * @param pstm  
  241.      * @param rsObjCall  
  242.      * @return  
  243.      * @throws SQLException  
  244.      */  
  245.     protected List executeQuery(PreparedStatement pstm, ResultCall rsObjCall) throws SQLException {   
  246.         List list = new ArrayList();   
  247.         ResultSet rs = null;   
  248.         try {   
  249.             rs = pstm.executeQuery();   
  250.             while (rs.next()) {   
  251.                 list.add(rsObjCall.getResultObject(rs));   
  252.             }   
  253.         }   
  254.         catch (SQLException e) {   
  255.             e.printStackTrace();   
  256.             throw new SQLException();   
  257.         }   
  258.         finally {   
  259.             this.destoryResource(pstm, rs);   
  260.         }   
  261.         return list;   
  262.     }   
  263.     /**  
  264.      * 获得新插入数据的id  
  265.      *   
  266.      * @param pstm  
  267.      * @return  
  268.      * @throws SQLException  
  269.      */  
  270.     protected int getCurrentId(PreparedStatement pstm) throws SQLException {   
  271.         ResultSet rs = null;   
  272.         try {   
  273.             pstm.execute();   
  274.             rs = pstm.getGeneratedKeys();   
  275.             if (rs.next()) {   
  276.                 return rs.getInt(1);   
  277.             }   
  278.         }   
  279.         catch (Exception e) {   
  280.             System.out.println(e.toString());   
  281.             throw new SQLException();   
  282.         }   
  283.         finally {   
  284.             this.destoryResource(pstm, rs);   
  285.         }   
  286.         return 0;   
  287.     }   
  288. }  

 

原创粉丝点击