1.抽象类DaoSupport.java

  1. package com.dao;  
  2.  
  3. import java.util.List;  
  4.  
  5. public abstract class DaoSupport  
  6. {  
  7.     /**  
  8.      *   
  9.      * {查询抽象方法}  
  10.      *   
  11.      * @param sql  
  12.      * @param rowMapper  
  13.      * @return  
  14.      * @author:LJ  
  15.      */ 
  16.     @SuppressWarnings("unchecked")  
  17.     protected abstract List query(String sql, RowMapper rowMapper);  
  18.  
  19.     /**  
  20.      *   
  21.      * {带参数查询抽象方法}  
  22.      *   
  23.      * @param sql  
  24.      * @param objs  
  25.      * @param rowMapper  
  26.      * @return  
  27.      * @author:LJ  
  28.      */ 
  29.     @SuppressWarnings("unchecked")  
  30.     protected abstract List query(String sql, Object[] objs, RowMapper rowMapper);  
  31.  
  32.     /**  
  33.      *   
  34.      * {更新抽象方法}  
  35.      *   
  36.      * @param sql  
  37.      * @param objs  
  38.      * @return  
  39.      * @author:LJ  
  40.      */ 
  41.     protected abstract int update(final String sql, Object[] objs);  
  42.  
  43.     /**  
  44.      *   
  45.      * {带参数更新抽象方法}  
  46.      *   
  47.      * @param sql  
  48.      * @return  
  49.      * @author:LJ 
  50.      */ 
  51.     protected abstract int update(final String sql);  
  52.  
  53.     /**  
  54.      *   
  55.      * {批处理抽象方法}  
  56.      *   
  57.      * @param sql  
  58.      * @return  
  59.      * @author:LJ  
  60.      */ 
  61.     protected abstract int[] batchUpdate(String[] sql);  
  62.     /**  
  63.      *   
  64.      * {带参数批量处理方法}  
  65.      *   
  66.      * @param sql  
  67.      * @param pss  
  68.      * @return  
  69.      * @author:LJ  
  70.      */ 
  71.     protected abstract int[] batchUpdate(final String sql, final BatchPreparedStatementSetter pss);  
  72. }  

2.实现类BaseDao.java

  1. package com.dao;  
  2.  
  3. import java.sql.Connection;  
  4. import java.sql.DriverManager;  
  5. import java.sql.PreparedStatement;  
  6. import java.sql.ResultSet;  
  7. import java.sql.SQLException;  
  8. import java.sql.Statement;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11.  
  12. public class BaseDao  
  13. {  
  14.     private String driverClassName = "oracle.jdbc.driver.OracleDriver";  
  15.  
  16.     private String username = "scott";  
  17.  
  18.     private String password = "tiger";  
  19.  
  20.     private String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";  
  21.  
  22.     /**  
  23.      *   
  24.      * {查询}  
  25.      *   
  26.      * @param sql  
  27.      * @return  
  28.      * @author:LJ  
  29.      */ 
  30.     @SuppressWarnings("unchecked")  
  31.     protected List query(String sql, RowMapper rowMapper)  
  32.     {  
  33.         Connection con = null;  
  34.         Statement stmt = null;  
  35.         ResultSet rs = null;  
  36.         List list = new ArrayList();  
  37.         try 
  38.         {  
  39.             Class.forName(driverClassName);  
  40.             con = DriverManager.getConnection(url, username, password);  
  41.             stmt = con.createStatement();  
  42.             rs = stmt.executeQuery(sql);  
  43.             while (rs.next())  
  44.             {  
  45.                 list.add(rowMapper.map(rs));  
  46.             }  
  47.         }  
  48.         catch (ClassNotFoundException e)  
  49.         {  
  50.             e.printStackTrace();  
  51.         }  
  52.         catch (SQLException e)  
  53.         {  
  54.             e.printStackTrace();  
  55.         }  
  56.         finally 
  57.         {  
  58.             DBUtil.closeQuietly(rs);  
  59.             DBUtil.closeQuietly(stmt);  
  60.             DBUtil.closeQuietly(con);  
  61.         }  
  62.         return list;  
  63.     }  
  64.  
  65.     /**  
  66.      *   
  67.      * {带参数查询}  
  68.      *   
  69.      * @param sql  
  70.      * @param objs  
  71.      * @return  
  72.      * @author:LJ 
  73.      */ 
  74.     @SuppressWarnings("unchecked")  
  75.     protected List query(String sql, final Object[] objs, RowMapper rowMapper)  
  76.     {  
  77.         Connection con = null;  
  78.         PreparedStatement preparedStatement = null;  
  79.         ResultSet rs = null;  
  80.         List list = new ArrayList();  
  81.         try 
  82.         {  
  83.             Class.forName(driverClassName);  
  84.             con = DriverManager.getConnection(url, username, password);  
  85.             preparedStatement = con.prepareStatement(sql);  
  86.             if (objs != null && objs.length > 0)  
  87.             {  
  88.                 for (int i = 0, n = objs.length; i < n; i++)  
  89.                 {  
  90.                     preparedStatement.setObject(i + 1, objs[i]);  
  91.                 }  
  92.             }  
  93.             rs = preparedStatement.executeQuery();  
  94.             while (rs.next())  
  95.             {  
  96.                 list.add(rowMapper.map(rs));  
  97.             }  
  98.         }  
  99.         catch (SQLException e)  
  100.         {  
  101.             throw new RuntimeException(e);  
  102.         }  
  103.         catch (ClassNotFoundException e)  
  104.         {  
  105.             e.printStackTrace();  
  106.         }  
  107.         finally 
  108.         {  
  109.             DBUtil.closeQuietly(rs);  
  110.             DBUtil.closeQuietly(preparedStatement);  
  111.             DBUtil.closeQuietly(con);  
  112.         }  
  113.         return list;  
  114.     }  
  115.  
  116.     /**  
  117.      *   
  118.      * {修改}  
  119.      *   
  120.      * @param sql  
  121.      * @param objs  
  122.      * @return  
  123.      * @author:LJ  
  124.      */ 
  125.     protected int update(final String sql, final Object[] objs)  
  126.     {  
  127.         Connection con = null;  
  128.         PreparedStatement preparedStatement = null;  
  129.         int retCode = 0;  
  130.         try 
  131.         {  
  132.             Class.forName(driverClassName);  
  133.             con = DriverManager.getConnection(url, username, password);  
  134.             preparedStatement = con.prepareStatement(sql);  
  135.             if (objs != null && objs.length > 0)  
  136.             {  
  137.                 for (int i = 0, n = objs.length; i < n; i++)  
  138.                 {  
  139.                     preparedStatement.setObject(i + 1, objs[i]);  
  140.                 }  
  141.             }  
  142.             retCode = preparedStatement.executeUpdate();  
  143.         }  
  144.         catch (SQLException e)  
  145.         {  
  146.             throw new RuntimeException(e);  
  147.         }  
  148.         catch (ClassNotFoundException e)  
  149.         {  
  150.             e.printStackTrace();  
  151.         }  
  152.         finally 
  153.         {  
  154.             DBUtil.closeQuietly(preparedStatement);  
  155.             DBUtil.closeQuietly(con);  
  156.         }  
  157.         return retCode;  
  158.     }  
  159.  
  160.     /**  
  161.      *   
  162.      * {带参数修改}  
  163.      *   
  164.      * @param sql  
  165.      * @return  
  166.      * @author:LJ  
  167.      */ 
  168.     protected int update(final String sql)  
  169.     {  
  170.         Connection con = null;  
  171.         Statement stmt = null;  
  172.         int retCode = 0;  
  173.         try 
  174.         {  
  175.             Class.forName(driverClassName);  
  176.             con = DriverManager.getConnection(url, username, password);  
  177.             stmt = con.createStatement();  
  178.             retCode = stmt.executeUpdate(sql);  
  179.         }  
  180.         catch (SQLException e)  
  181.         {  
  182.             throw new RuntimeException(e);  
  183.         }  
  184.         catch (ClassNotFoundException e)  
  185.         {  
  186.             e.printStackTrace();  
  187.         }  
  188.         finally 
  189.         {  
  190.             DBUtil.closeQuietly(stmt);  
  191.             DBUtil.closeQuietly(con);  
  192.         }  
  193.         return retCode;  
  194.     }  
  195.  
  196.     /**  
  197.      *   
  198.      * {批量处理}  
  199.      *   
  200.      * @param sql  
  201.      * @return  
  202.      * @author:LJ 
  203.      */ 
  204.     protected int[] batchUpdate(final String[] sql)  
  205.     {  
  206.         Connection con = null;  
  207.         Statement stmt = null;  
  208.         int[] rowsAffected = new int[sql.length];  
  209.         try 
  210.         {  
  211.             Class.forName(driverClassName);  
  212.             con = DriverManager.getConnection(url, username, password);  
  213.             stmt = con.createStatement();  
  214.             for (int i = 0, n = sql.length; i < n; i++)  
  215.             {  
  216.                 stmt.addBatch(sql[i]);  
  217.             }  
  218.             rowsAffected = stmt.executeBatch();  
  219.         }  
  220.         catch (SQLException e)  
  221.         {  
  222.             throw new RuntimeException(e);  
  223.         }  
  224.         catch (ClassNotFoundException e)  
  225.         {  
  226.             e.printStackTrace();  
  227.         }  
  228.         finally 
  229.         {  
  230.             DBUtil.closeQuietly(stmt);  
  231.             DBUtil.closeQuietly(con);  
  232.         }  
  233.         return rowsAffected;  
  234.     }  
  235.  
  236.     /**  
  237.      *   
  238.      * {带参数批量处理}  
  239.      *   
  240.      * @param sql  
  241.      * @return  
  242.      * @author:LJ  
  243.      */ 
  244.     public int[] batchUpdate(final String sql, final BatchPreparedStatementSetter pss)  
  245.     {  
  246.  
  247.         Connection con = null;  
  248.         PreparedStatement preparedStatement = null;  
  249.         int[] rowsAffected = null;  
  250.         try 
  251.         {  
  252.             Class.forName(driverClassName);  
  253.             con = DriverManager.getConnection(url, username, password);  
  254.             preparedStatement = con.prepareStatement(sql);  
  255.             for (int i = 0, n = pss.getBatchSize(); i < n; i++)  
  256.             {  
  257.                 pss.setValues(preparedStatement, i);  
  258.                 preparedStatement.addBatch();  
  259.             }  
  260.             rowsAffected = preparedStatement.executeBatch();  
  261.         }  
  262.         catch (SQLException e)  
  263.         {  
  264.             throw new RuntimeException(e);  
  265.         }  
  266.         catch (ClassNotFoundException e)  
  267.         {  
  268.             e.printStackTrace();  
  269.         }  
  270.         finally 
  271.         {  
  272.             DBUtil.closeQuietly(preparedStatement);  
  273.             DBUtil.closeQuietly(con);  
  274.         }  
  275.         return rowsAffected;  
  276.     }  

3.对外接口RowMapper.java

  1. package com.dao;  
  2.  
  3. import java.sql.ResultSet;  
  4. import java.sql.SQLException;  
  5.  
  6. public interface RowMapper<E>  
  7. {  
  8.     /**  
  9.      *   
  10.      * {将结果集中的一行转化为JAVA对象}  
  11.      *   
  12.      * @param rs  
  13.      * @return  
  14.      * @author:LJ  
  15.      */ 
  16.     public E map(ResultSet rs) throws SQLException;  

4.批处理接口BatchPreparedStatementSetter.java

  1. package com.dao;  
  2.  
  3. import java.sql.PreparedStatement;  
  4. import java.sql.SQLException;  
  5.  
  6. public interface BatchPreparedStatementSetter  
  7. {  
  8.     /**  
  9.      *   
  10.      * {设置参数值}  
  11.      *   
  12.      * @param preparedstatement  
  13.      * @param i  
  14.      * @throws SQLException  
  15.      * @author:LJ  
  16.      */ 
  17.     public void setValues(PreparedStatement preparedstatement, int i) throws SQLException;  
  18.  
  19.     /**  
  20.      *   
  21.      * {获取批处理个数}  
  22.      *   
  23.      * @return  
  24.      * @author:LJ  
  25.      */ 
  26.     public int getBatchSize();  

5.关闭资源DBUtil.java

  1. package com.dao;  
  2.  
  3. import java.sql.Connection;  
  4. import java.sql.ResultSet;  
  5. import java.sql.SQLException;  
  6. import java.sql.Statement;  
  7.  
  8. public class DBUtil  
  9. {  
  10.     /**  
  11.      *   
  12.      * {关闭连接}  
  13.      *   
  14.      * @param con  
  15.      * @author:LJ  
  16.      */ 
  17.     public static void closeQuietly(Connection con)  
  18.     {  
  19.         try 
  20.         {  
  21.             if (con != null)  
  22.             {  
  23.                 con.close();  
  24.             }  
  25.         }  
  26.         catch (SQLException e)  
  27.         {  
  28.             e.printStackTrace();  
  29.         }  
  30.     }  
  31.  
  32.     /**  
  33.      *   
  34.      * {关闭Statement声明}  
  35.      *   
  36.      * @param stmt  
  37.      * @author:LJ 
  38.      */ 
  39.     public static void closeQuietly(Statement stmt)  
  40.     {  
  41.         try 
  42.         {  
  43.             if (stmt != null)  
  44.             {  
  45.                 stmt.close();  
  46.             }  
  47.         }  
  48.         catch (SQLException e)  
  49.         {  
  50.             e.printStackTrace();  
  51.         }  
  52.     }  
  53.  
  54.     /**  
  55.      *   
  56.      * {关闭结果集}  
  57.      *   
  58.      * @param rs
  59.      * @author:LJ 
  60.      */ 
  61.     public static void closeQuietly(ResultSet rs)  
  62.     {  
  63.         try 
  64.         {  
  65.             if (rs != null)  
  66.             {  
  67.                 rs.close();  
  68.             }  
  69.         }  
  70.         catch (SQLException e)  
  71.         {  
  72.             e.printStackTrace();  
  73.         }  
  74.     }