推广网站的方法有哪些,北京免费网站建设,wordpress健康资讯模板,网页传奇手游排行榜前十名43.1. 回顾 1. 把数据库表中查询的结果封装到一个实体类中。 命名规则:类名和表名一致 类中属性和表的字段对应。 表中的一条记录对应实体的一个对象 多条记录→集合 43.2. 正文
目录
43.1. 回顾
43.2. 正文
43.3. 抽取dao公共父类。
43.4. 引入数据源 43.3. 抽取dao公共…43.1. 回顾 1. 把数据库表中查询的结果封装到一个实体类中。 命名规则:类名和表名一致 类中属性和表的字段对应。 表中的一条记录对应实体的一个对象 多条记录→集合 43.2. 正文
目录
43.1. 回顾
43.2. 正文
43.3. 抽取dao公共父类。
43.4. 引入数据源 43.3. 抽取dao公共父类。 通过昨天我们的操作可以发现dao类中很多方法都有一些相同的内容。那么我们就可以把这些相同的代码抽取到一个公共父类中。只需要这些dao子类继承该父类子类就无需再写这些公共代码。 public class BaseDao {//1.相同属性抽取过来protected Connection connnull;protected PreparedStatement psnull;protected ResultSet rsnull;private String urljdbc:mysql://localhost:3306/myinfo;private String userroot;private String passwordroot;private static String driverNamecom.mysql.cj.jdbc.Driver;//加载驱动放入静态代码块中--随着类的加载而被加载而且只会加载一次。 静态方法或静态代码只能调用静态成员static{try {Class.forName(driverName);} catch (ClassNotFoundException e) {e.printStackTrace();}}//2.获取连接对象public void getConnection() throws SQLException {conn DriverManager.getConnection(url, user, password);}//3.关闭资源public void closeAll(){//关闭连接数据库的资源if (rs ! null) {try {rs.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (ps ! null) {try {ps.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (conn ! null) {try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}} 抽象一个公共的增删改方法 //4. 公共的增删改操作 参数类型不同Object 参数个数不确定 ...public int update(String sql,Object... params){try {//2. 获取连接数据库的对象getConnection();//3. 获取执行sql语句的对象 ?表示占位符ps conn.prepareStatement(sql);//4. 为占位符赋值for(int i0;iparams.length;i) {ps.setObject(i1, params[i]);}//5. 执行sql语句 把sql执行的结果封装到ResultSet中int row ps.executeUpdate();return row;} catch (SQLException throwables) {throwables.printStackTrace();} finally {closeAll();}return 0;} 43.4. 引入数据源 数据源就是用来存放连接数据库的对象。也叫数据库连接池。 : C3P0、DBCP 、BoneCP、Proxool、 DDConnectionBroker、DBPool、 XAPool、Primrose、SmartPool、 MiniConnectionPoolManager 及Druid等。 1引入jar包
2修改basedao代码
↓ 上面选择的内容我们发现写死再代码中如果未来交付时需要改为对应客户的信息。需要修改源码--一旦代码写完不允许修改源码。--我们可以把上面这些信息写在属性文件中。 XXX.properties db.properties driverClassNamecom.mysql.cj.jdbc.Driver
urljdbc:mysql://localhost:3306/myinfo
usernameroot
passwordroot
# 初始化连接数量
initialSize5
# 最大连接数
maxActive10
# 最大等待时间
maxWait3000注意:名称必须为上面的名称→→ 值可以是你自己 修改baseDao代码
public class BaseDao {//1.相同属性抽取过来protected Connection connnull;protected PreparedStatement psnull;protected ResultSet rsnull;//硬编码private static DataSource dataSourcenull;//加载驱动放入静态代码块中--随着类的加载而被加载而且只会加载一次。 静态方法或静态代码只能调用静态成员static{try {//创建一个属性类Properties propnew Properties();//加载属性文件prop.load(BaseDao.class.getClassLoader().getResourceAsStream(db.properties));dataSource DruidDataSourceFactory.createDataSource(prop);} catch (Exception e) {e.printStackTrace();}}//2.获取连接对象public void getConnection() throws SQLException {conn dataSource.getConnection();}//4. 公共的增删改操作 参数类型不同Object 参数个数不确定 ...public int update(String sql,Object... params){try {//2. 获取连接数据库的对象getConnection();//3. 获取执行sql语句的对象 ?表示占位符ps conn.prepareStatement(sql);//4. 为占位符赋值for(int i0;iparams.length;i) {ps.setObject(i1, params[i]);}//5. 执行sql语句 把sql执行的结果封装到ResultSet中int row ps.executeUpdate();return row;} catch (SQLException throwables) {throwables.printStackTrace();} finally {closeAll();}return 0;}//3.关闭资源public void closeAll(){//关闭连接数据库的资源if (rs ! null) {try {rs.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (ps ! null) {try {ps.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (conn ! null) {try {conn.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}}