发表于: 2017-11-18 21:37:34
2 684
今天学习的内容
今天学习了连接池,按照网上示例自己创建了一个连接池
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!--<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >-->
<!--<property name="driverClassName"-->
<!--value="com.mysql.jdbc.Driver"/>-->
<!--<property name="url"-->
<!--value="jdbc:mysql://127.0.0.1:3306/fyc?characterEncoding=utf8&useSSL=false" />-->
<!--<property name="username" value="root" />-->
<!--<property name="password" value="123456" />-->
<!--</bean>-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"/>
<property name="configLocation" value=""/>
<property name="mapperLocations" value="classpath:BmbTestMapper.xml"/>
</bean>
<bean id="bmbTestMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="MapperInterface" value="mapper.BmbTestMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<!-- 数据库基本信息配置 -->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/fyc?characterEncoding=utf8&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="123456" />
<!-- 初始化连接数量 -->
<property name="initialSize" value="1" />
<!-- 最大并发连接数 -->
<property name="maxActive" value="10" />
<!-- 最小空闲连接数 -->
<property name="minIdle" value="1" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="10000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<!-- 用来检测连接是否有效的sql,要求是一个查询语句-->
<property name="validationQuery" value="select 1" />
<!-- 申请连接的时候检测 -->
<property name="testWhileIdle" value="true" />
<!-- 申请连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnBorrow" value="false" />
<!-- 归还连接时执行validationQuery检测连接是否有效,配置为true会降低性能 -->
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 这里配置提交方式,默认就是TRUE,可以不用配置 -->
<property name="defaultAutoCommit" value="true" />
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat" />
<!--属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有:
监控统计用的filter:stat
日志用的filter:log4j
防御SQL注入的filter:wall -->
<!--<property name="filters" value="${druid.filters}" /> -->
<!-- 解密密码必须要配置的项 config, stat启用监控过滤器-->
<!--<property name="filters" value="config,stat" />-->
<!--<property name="connectionProperties" value="config.decrypt=true" />-->
</bean>
</beans>
什么是连接池?举个例子,五一的时候大家都想去旅游,比如北京故宫,一天只能接待2w游客,但是有10w人想要访问,如果没有一个管理机构的话,故宫肯定会崩溃,那么我们在访问数据库的时候也是这样,必须有一个"管理机构"来管理对数据库的访问,这个"管理机构"就是连接池.当一个程序需要访问数据库的时候会向连接池发送请求,我们会给连接池设定一个最大连接数,如果有空闲的连接就会允许访问,建立连接池的目的就是减少资源的浪费,让资源可控,同时节省成本.
今天按照要求向数据库中插入了100w条数据
这是插入1000条数据和所用的时间
这是插入100w条数据和所用的时间
可以看到,想数据库中插入大量数据的时候并没有使用连接池,而是使用的sql语句,在数据库中直接循环插入的,所以效率非常高.这是代码:
package mapper;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Insert2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
final String url = "jdbc:mysql://localhost:3306/fyc?characterEncoding=utf8&useSSL=false";
final String name = "com.mysql.jdbc.Driver";
final String user = "root";
final String password = "123456";
Connection conn = null;
Class.forName(name);
conn = (Connection) DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println("获取连接成功");
insert(conn);
} else {
System.out.println("获取连接失败");
}
}
public static void insert(Connection conn) {
String prefix = "INSERT INTO bmbtest (id,name,sex,age,adress,study) VALUES";
try {
StringBuffer suffix = new StringBuffer();
conn.setAutoCommit(false);
PreparedStatement pst = (PreparedStatement) conn.prepareStatement("");//准备执行语句
long start = System.currentTimeMillis();
for (int i = 1; i <= 1000; i++) {
suffix = new StringBuffer();
System.out.println("插入成功");
for (int j = 1; j <= 1000; j++) {
suffix.append( "("+0+","+"'" +"陆压"+"'"+"," +"'"+"男"+"'"+","+18+","+"'"+"鱼鲮岛"+"'"+","+"'"+"斩仙飞刀"+"'"+"),");
}
String sql = prefix + suffix.substring(0, suffix.length() - 1);
// 添加执行SQL
pst.addBatch(sql);
// 执行操作
pst.executeBatch();
// 提交事务
conn.commit();
// 清空上一次添加的数据
suffix = new StringBuffer();
}
System.out.println("耗时:" + (System.currentTimeMillis()-start)+"毫秒");
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("插入完成");
}
}
遇到的问题
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1陆压男,18,鱼鲮岛,斩仙飞刀),('1陆压男,18,鱼鲮岛,斩仙飞刀),('' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
这个错误是SQL语句错误,在字段和字段之间少了一个逗号引起的,自己没有看出来,最后还是师兄帮忙解决的,可能正是应了那句话,当局者迷吧!
今天在网上查询向数据库批量插入数据的时候发现,网上的资料大部分都是残缺的,让人看了不知所云,所以今天搞的这个时间有点长,浪费了很多精力.
今天的收获
学习了连接池,向数据库批量插入数据,完成了任务一步骤28
明天的安排
明天重构代码,没有问题的话应该可以结束任务一
任务进度:任务1步骤28
任务开始时间:10月28日
任务结束时间:11月19日
评论