网站建设的用户名和密码代码,运营培训班有用吗,电子商务网站建设考试试题,六间房直播大厅官网背景
jdbc连接数据库存在着大批量用户进行短时间的SQL连接操作的 需求#xff0c;而普通用户连接后直接断开与数据库的连接#xff0c;下次连接需要重新建立桥梁#xff0c;再频繁访问时。这是很消耗性能的一个操作#xff0c;因此诞生了数据库连接池技术。提前创建 一些连…背景
jdbc连接数据库存在着大批量用户进行短时间的SQL连接操作的 需求而普通用户连接后直接断开与数据库的连接下次连接需要重新建立桥梁再频繁访问时。这是很消耗性能的一个操作因此诞生了数据库连接池技术。提前创建 一些连接避免频繁的创建连接并且可以管理程序和数据库之间的连接动态分配桥梁给申请连接的应用程序。
Druid连接池
Druid德鲁伊连接池是阿里开发的据说是集DBCP、C3P0、Proxool优点于一身的数据库连接池。
一、如何使用Druid连接池
引入德鲁伊连接池jar包和jdbc驱动jar包网上搜索一下很容易找到下载注意是.jar文件代码步骤 第一步建立一个数据库连接池对象第二步设置连接池的参数第三步从连接池中获取连接对象来使用 代码示例
Testpublic void test1() throws SQLException {DruidDataSource ds new DruidDataSource();//设置基本参数ds.setDriverClassName(com.mysql.cj.jdbc.Driver);ds.setUrl(jdbc:mysql://localhost:3306/atguigu);ds.setUsername(root);ds.setPassword(123456);//设置其他连接参数ds.setInitialSize(10);//初始化连接数ds.setMaxActive(11);//最大活动连接数ds.setMaxWait(1000);//最大等待时间ds.setMinIdle(5);//最小连接数for (int i 0; i 30; i) {DruidPooledConnection conn ds.getConnection();System.out.println(conn conn);conn.close();//释放连接到连接池中}}二、封装Druid连接池
1.配置文件参数批量导入
Druid连接池提供了一个createDataSource(Properties prop)的方法来统一设置连接池的参数由于底层是采用硬编码编写的所以配置文件的变量名称必须与代码一致否则无法将配置读取到项目中。
具体步骤
读取配置文件的参数信息使用工厂设计模式批量给参数赋值创建线程池ds调用线程的getConnection方法来获取一个连接
public class JdbcTool2 {private static DataSource ds;static {//1.注册驱动Properties prop new Properties();try {//加载配置文件中的数据到Properties中//配置文件要放到src目录下prop.load(JdbcTools.class.getClassLoader().getResourceAsStream(jdbc.properties));//批量设置参数的大小ds DruidDataSourceFactory.createDataSource(prop);//Class.forName(className);} catch (IOException e) {throw new RuntimeException(e);} catch (Exception e) {throw new RuntimeException(e);}}public static Connection getConnection() throws SQLException {//获取连接return ds.getConnection();}
}2.绑定连接对象
在MVC分层模型中如果我们在DAO层中使用jdbc技术建立连接修改了账号表的数据而在业务层中为了模拟转账的需求又再次获取了一个Connection对象来实现该事务。该写法会导致我们重复创建了不同的连接并且当代码出现异常执行事务回滚时会发现不能恢复到原有的状态。这是由于两层代码中的连接对象并不是同一个。
解决方案如下
在jdbc工具类中定义一个ThreadLocal对象在静态代码块中将其实例化获取连接时使用类似单例模式的写法来将连接与线程绑定在一起
public class JdbcTool3 {private static DataSource ds;private static ThreadLocalConnection threadLocal;static {//1.注册驱动Properties prop new Properties();try {//加载配置文件中的数据到Properties中prop.load(JdbcTools.class.getClassLoader().getResourceAsStream(jdbc.properties));//配置文件要放到src目录下//批量设置参数的大小ds DruidDataSourceFactory.createDataSource(prop);//Class.forName(className);//3.创建线程threadLocal new ThreadLocal();} catch (IOException e) {throw new RuntimeException(e);} catch (Exception e) {throw new RuntimeException(e);}}public static Connection getConnection() throws SQLException {//获取连接Connection conn threadLocal.get();if(connnull){conn ds.getConnection();threadLocal.set(conn);}return conn;}public static void closeAll(ResultSet rs, Statement st, Connection conn){try {if(rs!null){rs.close();}if(st!null){st.close();}if(conn!null){conn.close();//将连接还给连接池threadLocal.remove();//把连接对象从当前线程移除}} catch (SQLException e) {throw new RuntimeException(e);}}public static DataSource getDs(){return ds;}
}示例代码
三、Apche的DBUtils
一个已经封装好了的工具类可以简化我们在编写DAO层代码的复杂度。主要分两个方面来介绍一下该工具类分别是增删改和查询操作。
1.update操作
简化之前的代码
//添加用户public int addUser(User user) throws SQLException {Connection conn JdbcTool3.getConnection();PreparedStatement pst conn.prepareStatement(insert into users(username, password) values(?,?));pst.setObject(1,user.getUsername());pst.setObject(2,user.getPassword());int i pst.executeUpdate();JdbcTool3.closeAll(null,pst,conn);return i;}使用DBUtils后的代码
//添加用户public int addUser(User user) throws SQLException {String sql insert into users(username, password) values(?,?);int update qr.update(sql, user.getUsername(), user.getPassword());return update;}2. query操作
简化之前的代码
//根据id查询用户public User getUserById(int id) throws SQLException {Connection conn JdbcTool3.getConnection();PreparedStatement pst conn.prepareStatement(select id, username, password from users where id ?);pst.setObject(1,id);ResultSet rs pst.executeQuery();User user null;if(rs.next()){int ID rs.getInt(id);String username rs.getString(username);String password rs.getString(password);System.out.print(ID ID);System.out.print(,username username);System.out.println(,password password);user new User(ID, username, password);}JdbcTool3.closeAll(null,pst,conn);return user;}使用DBUtils后的代码
public User getUserById2(int id) throws SQLException {String sql select id, username, password from users where id ?;return qr.query(sql,new BeanHandler(User.class),id);}其中query方法中的BeanHandler对象底层是通过反射的方式来动态的将对象创建出来可以选择相应容器来包装如果是单个字段的话可以使用相应的Handle对象来处理。