咖啡豆(JavaBean)•常用JavaBean

数据库操作封装JavaBean

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

  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();		//数据库连接URL		String 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

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

  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值