JDBC框架

来源:互联网 发布:广联达预算软件盗版 编辑:程序博客网 时间:2024/06/18 06:23
[java] view plaincopy
  1. public class JdbcUtils {  
  2.   
  3.     // 表示定义数据库的用户名  
  4.     private final String USERNAME = "root";  
  5.     // 定义数据库的密码  
  6.     private final String PASSWORD = "admin";  
  7.     // 定义数据库的驱动信息  
  8.     private final String DRIVER = "com.mysql.jdbc.Driver";  
  9.     // 定义访问数据库的地址  
  10.     private final String URL = "jdbc:mysql://localhost:3306/testdb";  
  11.     // 定义数据库的链接  
  12.     private Connection connection;  
  13.     // 定义sql语句的执行对象  
  14.     private PreparedStatement pstmt;  
  15.     // 定义查询返回的结果集合  
  16.     private ResultSet resultSet;  
  17.     // 实现批处理操作的功能  
  18.     private Statement stmt;  
  19.   
  20.     public JdbcUtils() {  
  21.         try {  
  22.             Class.forName(DRIVER);  
  23.             System.out.println("注册驱动成功!!");  
  24.         } catch (Exception e) {  
  25.             // TODO: handle exception  
  26.         }  
  27.     }  
  28.   
  29.     // 定义获得数据库的链接  
  30.     public Connection getConnection() {  
  31.         try {  
  32.             connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);  
  33.         } catch (Exception e) {  
  34.             // TODO: handle exception  
  35.         }  
  36.         return connection;  
  37.     }  
  38.   
  39.     public boolean deleteByBatch(String[] sql) throws SQLException{  
  40.         boolean flag = false;  
  41.         stmt = connection.createStatement();  
  42.         if(sql!=null){  
  43.             for(int i=0;i<sql.length;i++){  
  44.                 stmt.addBatch(sql[i]);  
  45.             }  
  46.         }  
  47.         int[] count = stmt.executeBatch();  
  48.         if(count!=null){  
  49.             flag = true;  
  50.         }  
  51.         return flag;  
  52.     }  
  53.     /** 
  54.      * 完成对数据库的表的添加删除和修改的操作 
  55.      *  
  56.      * @param sql 
  57.      * @param params 
  58.      * @return 
  59.      * @throws SQLException 
  60.      */  
  61.     public boolean updateByPreparedStatement(String sql, List<Object> params)  
  62.             throws SQLException {  
  63.         boolean flag = false;  
  64.         int result = -1;// 表示当用户执行添加删除和修改的时候所影响数据库的行数  
  65.         pstmt = connection.prepareStatement(sql);  
  66.         int index = 1;  
  67.         if (params != null && !params.isEmpty()) {  
  68.             for (int i = 0; i < params.size(); i++) {  
  69.                 pstmt.setObject(index++, params.get(i));  
  70.             }  
  71.         }  
  72.         result = pstmt.executeUpdate();  
  73.         flag = result > 0 ? true : false;  
  74.         return flag;  
  75.     }  
  76.   
  77.     /** 
  78.      * 查询返回单条记录 
  79.      *  
  80.      * @param sql 
  81.      * @param params 
  82.      * @return 
  83.      * @throws SQLException 
  84.      */  
  85.     public Map<String, Object> findSimpleResult(String sql, List<Object> params)  
  86.             throws SQLException {  
  87.         Map<String, Object> map = new HashMap<String, Object>();  
  88.         int index = 1;  
  89.         pstmt = connection.prepareStatement(sql);  
  90.         if (params != null && !params.isEmpty()) {  
  91.             for (int i = 0; i < params.size(); i++) {  
  92.                 pstmt.setObject(index++, params.get(i));  
  93.             }  
  94.         }  
  95.         resultSet = pstmt.executeQuery();// 返回查询结果  
  96.         ResultSetMetaData metaData = resultSet.getMetaData();  
  97.         int col_len = metaData.getColumnCount();// 获得列的名称  
  98.         while (resultSet.next()) {  
  99.             for (int i = 0; i < col_len; i++) {  
  100.                 String cols_name = metaData.getColumnName(i + 1);  
  101.                 Object cols_value = resultSet.getObject(cols_name);  
  102.                 if (cols_value == null) {  
  103.                     cols_value = "";  
  104.                 }  
  105.                 map.put(cols_name, cols_value);  
  106.             }  
  107.         }  
  108.         return map;  
  109.     }  
  110.   
  111.     /** 
  112.      * 查询返回多行记录 
  113.      *  
  114.      * @param sql 
  115.      * @param params 
  116.      * @return 
  117.      * @throws SQLException 
  118.      */  
  119.     public List<Map<String, Object>> findMoreResult(String sql,  
  120.             List<Object> params) throws SQLException {  
  121.         List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();  
  122.         int index = 1;  
  123.         pstmt = connection.prepareStatement(sql);  
  124.         if (params != null && !params.isEmpty()) {  
  125.             for (int i = 0; i < params.size(); i++) {  
  126.                 pstmt.setObject(index++, params.get(i));  
  127.             }  
  128.         }  
  129.         resultSet = pstmt.executeQuery();  
  130.         ResultSetMetaData metaData = resultSet.getMetaData();  
  131.         int cols_len = metaData.getColumnCount();  
  132.         while (resultSet.next()) {  
  133.             Map<String, Object> map = new HashMap<String, Object>();  
  134.             for (int i = 0; i < cols_len; i++) {  
  135.                 String cols_name = metaData.getColumnName(i + 1);  
  136.                 Object cols_value = resultSet.getObject(cols_name);  
  137.                 if (cols_value == null) {  
  138.                     cols_value = "";  
  139.                 }  
  140.                 map.put(cols_name, cols_value);  
  141.             }  
  142.             list.add(map);  
  143.         }  
  144.         return list;  
  145.     }  
  146.   
  147.     // jdbc的封装可以用反射机制来封装:  
  148.     public <T> T findSimpleRefResult(String sql, List<Object> params,  
  149.             Class<T> cls) throws Exception {  
  150.         T resultObject = null;  
  151.         int index = 1;  
  152.         pstmt = connection.prepareStatement(sql);  
  153.         if (params != null && !params.isEmpty()) {  
  154.             for (int i = 0; i < params.size(); i++) {  
  155.                 pstmt.setObject(index++, params.get(i));  
  156.             }  
  157.         }  
  158.         resultSet = pstmt.executeQuery();  
  159.         ResultSetMetaData metaData = resultSet.getMetaData();  
  160.         int cols_len = metaData.getColumnCount();  
  161.         while (resultSet.next()) {  
  162.             // 通过反射机制创建实例  
  163.             resultObject = cls.newInstance();  
  164.             for (int i = 0; i < cols_len; i++) {  
  165.                 String cols_name = metaData.getColumnName(i + 1);  
  166.                 Object cols_value = resultSet.getObject(cols_name);  
  167.                 if (cols_value == null) {  
  168.                     cols_value = "";  
  169.                 }  
  170.                 Field field = cls.getDeclaredField(cols_name);  
  171.                 field.setAccessible(true);// 打开javabean的访问private权限  
  172.                 field.set(resultObject, cols_value);  
  173.             }  
  174.         }  
  175.         return resultObject;  
  176.     }  
  177.   
  178.     /** 
  179.      * 通过反射机制访问数据库 
  180.      *  
  181.      * @param <T> 
  182.      * @param sql 
  183.      * @param params 
  184.      * @param cls 
  185.      * @return 
  186.      * @throws Exception 
  187.      */  
  188.     public <T> List<T> findMoreRefResult(String sql, List<Object> params,  
  189.             Class<T> cls) throws Exception {  
  190.         List<T> list = new ArrayList<T>();  
  191.         int index = 1;  
  192.         pstmt = connection.prepareStatement(sql);  
  193.         if (params != null && !params.isEmpty()) {  
  194.             for (int i = 0; i < params.size(); i++) {  
  195.                 pstmt.setObject(index++, params.get(i));  
  196.             }  
  197.         }  
  198.         resultSet = pstmt.executeQuery();  
  199.         ResultSetMetaData metaData = resultSet.getMetaData();  
  200.         int cols_len = metaData.getColumnCount();  
  201.         while (resultSet.next()) {  
  202.             T resultObject = cls.newInstance();  
  203.             for (int i = 0; i < cols_len; i++) {  
  204.                 String cols_name = metaData.getColumnName(i + 1);  
  205.                 Object cols_value = resultSet.getObject(cols_name);  
  206.                 if (cols_value == null) {  
  207.                     cols_value = "";  
  208.                 }  
  209.                 Field field = cls.getDeclaredField(cols_name);  
  210.                 field.setAccessible(true);  
  211.                 field.set(resultObject, cols_value);  
  212.             }  
  213.             list.add(resultObject);  
  214.         }  
  215.         return list;  
  216.     }  
  217.   
  218.     public void releaseConn() {  
  219.         if (resultSet != null) {  
  220.             try {  
  221.                 resultSet.close();  
  222.             } catch (SQLException e) {  
  223.                 // TODO Auto-generated catch block  
  224.                 e.printStackTrace();  
  225.             }  
  226.         }  
  227.         if (stmt != null) {  
  228.             try {  
  229.                 stmt.close();  
  230.             } catch (SQLException e) {  
  231.                 // TODO Auto-generated catch block  
  232.                 e.printStackTrace();  
  233.             }  
  234.         }  
  235.         if (pstmt != null) {  
  236.             try {  
  237.                 pstmt.close();  
  238.             } catch (SQLException e) {  
  239.                 // TODO Auto-generated catch block  
  240.                 e.printStackTrace();  
  241.             }  
  242.         }  
  243.         if (connection != null) {  
  244.             try {  
  245.                 connection.close();  
  246.             } catch (SQLException e) {  
  247.                 // TODO Auto-generated catch block  
  248.                 e.printStackTrace();  
  249.             }  
  250.         }  
  251.     }  
  252. }  
0 0