毕设做网站类型,网站侧栏软件排行榜怎么做的,数码印花图案设计网站,网页设计制作软件数据源#xff08;连接池#xff09;的作用
数据源(连接池)是提高程序性能如出现的 事先实例化数据源#xff0c;初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 常见的数据源(连接池)#xff1a;DBCP、C3P0、BoneCP、Druid等 开发步…数据源连接池的作用
数据源(连接池)是提高程序性能如出现的 事先实例化数据源初始化部分连接资源 使用连接资源时从数据源中获取 使用完毕后将连接资源归还给数据源 常见的数据源(连接池)DBCP、C3P0、BoneCP、Druid等 开发步骤 ①导入数据源的坐标和数据库驱动坐标 ②创建数据源对象 ③设置数据源的基本连接数据 ④使用数据源获取连接资源和归还连接资源
数据源的手动创建
①导入c3p0
!-- C3P0连接池 --dependency groupIdc3p0/groupId artifactIdc3p0/artifactId version0.9.1.2/version /dependency①导入mysql数据库驱动坐标 dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.11/version/dependency
②创建C3P0连接池 Testpublic void testC3P0() throws Exception{ComboPooledDataSource comboPooledDataSourcenew ComboPooledDataSource();comboPooledDataSource.setDriverClass(com.mysql.cj.jdbc.Driver);comboPooledDataSource.setJdbcUrl(jdbc:mysql://localhost:3306/book?useSSLfalseserverTimezoneUTC);comboPooledDataSource.setUser(root);comboPooledDataSource.setPassword(123456);Connection connectioncomboPooledDataSource.getConnection();System.out.println(connection);}提取jdbc.properties配置文件
jdbc.drivercom.mysql.cj.jdbc.Driver
jdbc.urljdbc:mysql://localhost:3306/book?useSSLfalseserverTimezoneUTC
jdbc.usernamexxx
jdbc.passwordxxxSpring配置数据源
可以将DataSource的创建权交由Spring容器去完成 DataSource有无参构造方法而Spring默认就是通过无参构造方法实例化对象的 DataSource要想使用需要通过set方法设置数据库连接信息而Spring可以通过set方法进行字符串注入 Testpublic void testC3P0() throws Exception{ComboPooledDataSource comboPooledDataSourcenew ComboPooledDataSource();ResourceBundle resourceBundleResourceBundle.getBundle(jdbc);comboPooledDataSource.setDriverClass(resourceBundle.getString(jdbc.driver));comboPooledDataSource.setJdbcUrl(resourceBundle.getString(jdbc.url));comboPooledDataSource.setUser(resourceBundle.getString(jdbc.username));comboPooledDataSource.setPassword(resourceBundle.getString(jdbc.password));Connection connectioncomboPooledDataSource.getConnection();System.out.println(connection);}?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:phttp://www.springframework.org/schema/pxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdcontext:property-placeholder locationjdbc.properties/bean iddatasource classcom.mchange.v2.c3p0.ComboPooledDataSourceproperty namedriverClass value${jdbc.driver}/property namejdbcUrl value${jdbc.url}/property nameuser value${jdbc.username}/property namepassword value${jdbc.password}//bean/beansTestpublic void testC3P02() throws Exception{ApplicationContext applicationContextnew ClassPathXmlApplicationContext(applicationContext.xml);ComboPooledDataSource comboPooledDataSource(ComboPooledDataSource) applicationContext.getBean(datasource);System.out.println(comboPooledDataSource.getConnection());}