[关闭]
@wangzhuanyun 2019-08-16T03:14:51.000000Z 字数 2172 阅读 2702

JDBC封装与设计模式(day2)

web


DAO模式的应用

Data Access Object(数据存取对象) 持久层
创建包
com.dao ----放接口文件 UserDao
BaseDao(数据库工具类)
com.dao.impl----放接口实现类 UserDaoImpl
com.entity ----放实体类 User

封装JDBC

  1. /**
  2. * 数据库工具类
  3. */
  4. public class BaseDao {
  5. Connection conn = null;
  6. PreparedStatement ps = null;
  7. //获取Conn对象 打开数据库链接
  8. public boolean getConn() {
  9. boolean bool = false;//默认 false 未打开数据库
  10. try {
  11. //加载驱动 方言
  12. Class.forName("com.mysql.jdbc.Driver");
  13. //准备数据库连接路径
  14. String url = "jdbc:mysql://127.0.0.1:3306/xxshop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull";
  15. //用户名与密码
  16. String username = "root";
  17. String userpwd = "root";
  18. //根据路径,用户名,密码 使用DriverManager获取数据库connection连接
  19. conn = DriverManager.getConnection(
  20. url,username,userpwd);
  21. bool = true;//已经打开
  22. } catch (Exception e) {
  23. e.printStackTrace();
  24. bool = false ;//已经打开
  25. }
  26. return bool;
  27. }
  28. /**
  29. * 添加,修改,删除数据
  30. * @param sql
  31. * @param objs
  32. * @return
  33. */
  34. public int executeUpdate(String sql,Object objs[])
  35. {
  36. int res = 0;//初始化执行结果 失败0
  37. try {
  38. if(getConn())//打开数据库链接
  39. {
  40. ps = conn.prepareStatement(sql);
  41. if(objs!=null){
  42. for (int i = 0; i < objs.length; i++) {
  43. ps.setObject((i+1),objs[i]);
  44. }
  45. }
  46. res = ps.executeUpdate();
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. } finally {
  51. closeResource();//关闭数据源
  52. }
  53. return res;
  54. }
  55. /**
  56. * 查询
  57. * @param sql
  58. * @param objs
  59. * @return
  60. */
  61. public ResultSet executeSQL(String sql,Object objs[]){
  62. ResultSet rs = null;
  63. try {
  64. if(getConn())//打开数据库链接
  65. {
  66. ps = conn.prepareStatement(sql);
  67. //判断是否有参数
  68. if (objs != null) {
  69. //循环封装参数
  70. for (int i = 0; i < objs.length; i++) {
  71. ps.setObject((i + 1), objs[i]);
  72. }
  73. }
  74. rs = ps.executeQuery();
  75. }
  76. } catch (Exception e) {
  77. e.printStackTrace();
  78. } finally {
  79. closeResource();//释放资源
  80. }
  81. return rs;
  82. }
  83. //关闭资源
  84. public void closeResource(){
  85. try {
  86. if(ps!=null)
  87. {
  88. ps.close();
  89. }
  90. if(conn!=null) {
  91. conn.close();
  92. }
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. }
  97. }

调用工具类

实现类 继承 工具类(BaseDao)

查询:ResultSet rs = this.executeSQL(SQL语句,Object数组<参数数组>)

增,删,改: int i = this.executeUpdate(SQL语句,Object数组<参数数组>)

使用配置文件存储连接信息(properties文件)

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/xxshop?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
jdbc.username=root
jdbc.pwd=root

  1. Properties properties = new Properties();
  2. //读取properties文件 BaseDao为当前所在类
  3. InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("jdbc.properties");
  4. //将文件信息转换成properties对象
  5. properties.load(is);
  6. //通过getPropertyKEY)方法获取属性值
  7. String driver = properties.getProperty("jdbc.driver");
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注