数据库访问JavaBean的设计——工具类JDBCUtil创建
上一节
下一节
1. 案例需求
【案例描述】
数据库操作在一个Web应用程序中的后台处理中占有很大的比重,本例设计一组JavaBean封装数据库的基本操作,供上层模块调用,提高程序的复用性和可移植性。
【案例分析】
假设操作的数据库名是jdbc,表格是user (id, name, password, birthday, salary),封装的基本操作是记录的添加、修改、查询全部、按userid查找用户、按userid删除用户。
2. 案例设计
关键技术点:1) Java web应用中访问资源文件的方式
2) JDBCUtil工具类封装数据库资料的开关释放
3) User这个VO对象与数据库表的对应关系
该案例需要设计以下组件:
(1)数据库jdbc及其数据库表user。
(2)在资源包中建立属性文件mysql.properties,存放数据库的基本信息,这样做的好处是数据库信息发生变化时只需要修改该文件,不用重新编译代码。
(3)建立一个获取连接和释放资源的工具类JDBCUtil.java。
(4)建立VO的定义类User.java实现记录信息对象化,体现面向对象程序设计思想。
3. Code Example
User.java示例代码
| package cn.edu.wfu.cs.web.domain; import java.util.Date; public class User { //define Bean properties private String id; private String name; private String password; private Date birthday; private float salary; //define default constructors public User() { super(); } //define setters and getters public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public float getSalary() { return salary; } public void setSalary(float salary) { this.salary = salary; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((birthday == null) ? 0 : birthday.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + Float.floatToIntBits(salary); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; User other = (User) obj; if (birthday == null) { if (other.birthday != null) return false; } else if (!birthday.equals(other.birthday)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (password == null) { if (other.password != null) return false; } else if (!password.equals(other.password)) return false; if (Float.floatToIntBits(salary) != Float.floatToIntBits(other.salary)) return false; return true; } } |
JDBCUti.java示例代码
| package cn.edu.wfu.cs.web.util; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; /** * * @author Huihui Zhang * * JDBC工具类设计: 1) 工具类通常禁止继承, i.e., final + private constructor() * 2) 常用的设计模式: 单例模式, static类模式 * */ public final class JDBCUtil { // define dbAccess properties private static String dbDriver; private static String dbURL; private static String dbUser; private static String password; private static Properties prop = new Properties(); static {// 数据库驱动只需注册一次 register driver only once,故而 只需在类加载时对数据库驱动信息进行一次性初始化 try { //通过ClassLoader访问资源文件, 注意资源目录的格式不带根"/" prop.load(JDBCUtil.class.getClassLoader().getResourceAsStream( "cn/edu/wfu/cs/resource/mysql.properties"));//数据库的驱动信息通过配置文件的形式提供, 解耦对特定DB引擎的硬编码 //initialize db properties dbDriver = prop.getProperty("dbDriver"); dbURL= prop.getProperty("dbURL"); dbUser = prop.getProperty("dbUser"); password = prop.getProperty("password"); //register dbDriver Class.forName(dbDriver); } catch (IOException e) { throw new ExceptionInInitializerError(e); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //define private default constructor private JDBCUtil() {//禁止外部创建实例 super(); } //get db connection public static Connection getConnection() { try { return DriverManager.getConnection(dbURL, dbUser, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * * @param rs * @param prest * @param conn * * Note: 1)数据库资源释放的顺序: RresultSet ---> PreparedStatement ---> Connection * 2)数据库资源释放过程中的异常处理一定要确保不会影响后继资源的释放 * * */ public static void freeDBResources(ResultSet rs, PreparedStatement prest, Connection conn) { try { if (null != rs) {//数据库资源释放的顺序: RresultSet ---> PreparedStatement ---> Connection rs.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally {// 数据库资源释放过程中的异常捕获一定要确保不会影响后继资源的释放 try { if (null != prest) { prest.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (null != conn) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } } |

