咖啡豆(JavaBean)•常用JavaBean

来源:互联网 发布:sql 存储过程 编辑:程序博客网 时间:2024/04/28 01:41
数据库操作封装JavaBean

在使用Hibernate之前常常使用这个JavaBean,类似于Net中的sqlHelper。

[java] view plaincopyprint?
  1. package beans; 
  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.  
  10. public class DBUtil { 
  11.     /**
  12.      * 取得一个数据库连接
  13.      * @return
  14.      * @throws SQLException
  15.      * @throws InstantiationException
  16.      * @throws IllegalAccessException
  17.      * @throws ClassNotFoundException
  18.      */ 
  19.     public Connection getConnection() throws SQLException, 
  20. InstantiationException, IllegalAccessException,
  21.             ClassNotFoundException { 
  22.         Connection conn = null; 
  23.         //加载数据库驱动类 
  24.         Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") 
  25.                 .newInstance(); 
  26.         //数据库连接URL 
  27.         String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
  28.         //数据库用户名 
  29.         String user = "sa"; 
  30.         //数据库密码 
  31.         String password = "1985315"; 
  32.         //根据数据库参数取得一个数据库连接 
  33.         conn = DriverManager.getConnection(url, user, password); 
  34.         return conn; 
  35.     } 
  36.  
  37.     /**
  38.      * 根据传入的SQL语句返回一个结果集
  39.      * @param sql
  40.      * @return
  41.      * @throws Exception
  42.      */ 
  43.     public ResultSet select(String sql) throws Exception { 
  44.         Connection conn = null; 
  45.         Statement stmt = null; 
  46.         ResultSet rs = null; 
  47.         try { 
  48.             conn = getConnection(); 
  49.             stmt = conn.createStatement(); 
  50.             rs = stmt.executeQuery(sql); 
  51.             return rs; 
  52.         } catch (SQLException sqle) { 
  53.             throw new SQLException("select data exception: " 
  54.                     + sqle.getMessage()); 
  55.         } catch (Exception e) { 
  56.             throw new Exception("System e exception: " + e.getMessage()); 
  57.         }  
  58.          
  59.     } 
  60.  
  61.     /**
  62.      * 根据传入的SQL语句向数据库增加一条记录
  63.      * @param sql
  64.      * @throws Exception
  65.      */ 
  66.     public void insert(String sql) throws Exception { 
  67.         Connection conn = null; 
  68.         PreparedStatement ps = null; 
  69.         try { 
  70.             conn = getConnection(); 
  71.             ps = conn.prepareStatement(sql); 
  72.             ps.executeUpdate(); 
  73.         } catch (SQLException sqle) { 
  74.             throw new Exception("insert data exception: " + sqle.getMessage()); 
  75.         } finally { 
  76.             try { 
  77.                 if (ps != null) { 
  78.                     ps.close(); 
  79.                 } 
  80.             } catch (Exception e) { 
  81.                 throw new Exception("ps close exception: " + e.getMessage()); 
  82.             } 
  83.         } 
  84.         try { 
  85.             if (conn != null) { 
  86.                 conn.close(); 
  87.             } 
  88.         } catch (Exception e) { 
  89.             throw new Exception("connection close exception: " + e.getMessage()); 
  90.         } 
  91.     } 
  92.  
  93.     /**
  94.      * 根据传入的SQL语句更新数据库记录
  95.      * @param sql
  96.      * @throws Exception
  97.      */ 
  98.     public void update(String sql) throws Exception { 
  99.         Connection conn = null; 
  100.         PreparedStatement ps = null; 
  101.         try { 
  102.             conn = getConnection(); 
  103.             ps = conn.prepareStatement(sql); 
  104.             ps.executeUpdate(); 
  105.         } catch (SQLException sqle) { 
  106.             throw new Exception("update exception: " + sqle.getMessage()); 
  107.         } finally { 
  108.             try { 
  109.                 if (ps != null) { 
  110.                     ps.close(); 
  111.                 } 
  112.             } catch (Exception e) { 
  113.                 throw new Exception("ps close exception: " + e.getMessage()); 
  114.             } 
  115.         } 
  116.         try { 
  117.             if (conn != null) { 
  118.                 conn.close(); 
  119.             } 
  120.         } catch (Exception e) { 
  121.             throw new Exception("connection close exception: " + e.getMessage()); 
  122.         } 
  123.     } 
  124.  
  125.     /**
  126.      * 根据传入的SQL语句删除一条数据库记录
  127.      * @param sql
  128.      * @throws Exception
  129.      */ 
  130.     public void delete(String sql) throws Exception { 
  131.         Connection conn = null; 
  132.         PreparedStatement ps = null; 
  133.         try { 
  134.             conn = getConnection(); 
  135.             ps = conn.prepareStatement(sql); 
  136.             ps.executeUpdate(); 
  137.         } catch (SQLException sqle) { 
  138.             throw new Exception("delete data exception: " + sqle.getMessage()); 
  139.         } finally { 
  140.             try { 
  141.                 if (ps != null) { 
  142.                     ps.close(); 
  143.                 } 
  144.             } catch (Exception e) { 
  145.                 throw new Exception("ps close exception: " + e.getMessage()); 
  146.             } 
  147.         } 
  148.         try { 
  149.             if (conn != null) { 
  150.                 conn.close(); 
  151.             } 
  152.         } catch (Exception e) { 
  153.             throw new Exception("connection close exception: " + e.getMessage()); 
  154.         } 
  155.     } 
package beans;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class DBUtil {/** * 取得一个数据库连接 * @return * @throws SQLException * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException */public Connection getConnection() throws SQLException,InstantiationException, IllegalAccessException,ClassNotFoundException {Connection conn = null;//加载数据库驱动类Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();//数据库连接URLString url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";//数据库用户名String user = "sa";//数据库密码String password = "1985315";//根据数据库参数取得一个数据库连接    conn = DriverManager.getConnection(url, user, password);return conn;}/** * 根据传入的SQL语句返回一个结果集 * @param sql * @return * @throws Exception */public ResultSet select(String sql) throws Exception {Connection conn = null;Statement stmt = null;ResultSet rs = null;try {conn = getConnection();stmt = conn.createStatement();rs = stmt.executeQuery(sql);return rs;} catch (SQLException sqle) {throw new SQLException("select data exception: "+ sqle.getMessage());} catch (Exception e) {throw new Exception("System e exception: " + e.getMessage());} }/** * 根据传入的SQL语句向数据库增加一条记录 * @param sql * @throws Exception */public void insert(String sql) throws Exception {Connection conn = null;PreparedStatement ps = null;try {conn = getConnection();ps = conn.prepareStatement(sql);ps.executeUpdate();} catch (SQLException sqle) {throw new Exception("insert data exception: " + sqle.getMessage());} finally {try {if (ps != null) {ps.close();}} catch (Exception e) {throw new Exception("ps close exception: " + e.getMessage());}}try {if (conn != null) {conn.close();}} catch (Exception e) {throw new Exception("connection close exception: " + e.getMessage());}}/** * 根据传入的SQL语句更新数据库记录 * @param sql * @throws Exception */public void update(String sql) throws Exception {Connection conn = null;PreparedStatement ps = null;try {conn = getConnection();ps = conn.prepareStatement(sql);ps.executeUpdate();} catch (SQLException sqle) {throw new Exception("update exception: " + sqle.getMessage());} finally {try {if (ps != null) {ps.close();}} catch (Exception e) {throw new Exception("ps close exception: " + e.getMessage());}}try {if (conn != null) {conn.close();}} catch (Exception e) {throw new Exception("connection close exception: " + e.getMessage());}}/** * 根据传入的SQL语句删除一条数据库记录 * @param sql * @throws Exception */public void delete(String sql) throws Exception {Connection conn = null;PreparedStatement ps = null;try {conn = getConnection();ps = conn.prepareStatement(sql);ps.executeUpdate();} catch (SQLException sqle) {throw new Exception("delete data exception: " + sqle.getMessage());} finally {try {if (ps != null) {ps.close();}} catch (Exception e) {throw new Exception("ps close exception: " + e.getMessage());}}try {if (conn != null) {conn.close();}} catch (Exception e) {throw new Exception("connection close exception: " + e.getMessage());}}}


分页操作JavaBean

在操作报表的时候常常用到,方便分页显示。

[java] view plaincopyprint?
  1. package beans; 
  2.  
  3. public class Page { 
  4.     private int totalPage;//总页数 
  5.     private int currentPage;//当前页数 
  6.     private int totalRecord;//总的记录条数 
  7.     private int currentRecord;//当前记录的条数 
  8.     private int pageSize = 6;//每页显示的记录数量,这里默认每页显示6条 
  9.     public int getCurrentPage() { 
  10.         return currentPage; 
  11.     } 
  12.     public void setCurrentPage(int currentRecord,int pageSize ) { 
  13.         //如果当前记录数除以每页显示条数可以整除,商就是当前的页码 
  14.         if(currentRecord%pageSize == 0)  
  15.         { 
  16.             currentPage = currentRecord/pageSize; 
  17.         }else 
  18.         { 
  19.            //如果当前记录数除以每页显示条数不能整除,商加1才是当前的页码 
  20.             currentPage = currentRecord/pageSize+1; 
  21.         } 
  22.     } 
  23.     public int getCurrentRecord() { 
  24.         return currentRecord; 
  25.     } 
  26.     public void setCurrentRecord(int currentRecord) { 
  27.         this.currentRecord = currentRecord; 
  28.     } 
  29.     public int getPageSize() { 
  30.         return pageSize; 
  31.     } 
  32.     public void setPageSize(int pageSize) { 
  33.         this.pageSize = pageSize; 
  34.     } 
  35.     public int getTotalPage() { 
  36.         return totalPage; 
  37.     } 
  38.     public void setTotalPage(int totalRecord,int pageSize) { 
  39.         //如果总记录数除以每页显示条数可以整除,商就是总页码 
  40.         if(totalRecord%pageSize == 0)  
  41.         { 
  42.             totalPage = totalRecord/pageSize; 
  43.         }else 
  44.         { 
  45.            //如果总记录数除以每页显示条数不能整除,商加1才是总页码 
  46.             totalPage = totalRecord/pageSize+1; 
  47.         } 
  48.     } 
  49.     public int getTotalRecord() { 
  50.         return totalRecord; 
  51.     } 
  52.     public void setTotalRecord(int totalRecord) { 
  53.         this.totalRecord = totalRecord; 
  54.     } 
  55.      

0 0
原创粉丝点击