发表于: 2017-07-07 20:33:48
1 1103
今天的事情:学习一个简单的for循环语句,用来插入100W条语句,学习连接池的应用。初步学习jdbc批处理数据的用法。
package main;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.DriverManager;
import java.util.Date;
import javax.swing.JFrame;
public class HelloJava extends JFrame {
public final String url = "jdbc:mysql://XXXXXXXXXXXXXXXXXX:3306/db1?characterEncoding=UTF-8&useSSL=false";
public final String username = "root";
public final String password = "**********";
public Connection conn;
public Statement stmt;
public ResultSet rs;
Long begin = new Date( ).getTime();
public HelloJava() {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 对数据库进行批量插入数据操作
* 执行次数100万
*/
public void insertBatch() {
//思路:将100万条数据分成n等份,1等份为1000条数据
//如何实现?
//1、必须将Connection接口的自动提交方式改为手动
//2、利用Statement接口中的如下三个方法:addBatch、clearBath、executeBatch
try {
conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
stmt = conn.createStatement();
for (int i = 0; i < 1000000; i++) {
String sql = "INSERT INTO table1 (`id`, `updata_at`, `create_at`, `name`, `qq`, `jobs`, `join_time`, `school`, `online_id`, `data_url`, `declaration`, `teacher`, `sources`) VALUES (NULL , NULL, NULL, '吴彦祖', '888888', '中大潘石屹', '广州大鼻队', '武汉陈水扁', '海南薛之谦', '江门古天乐', '东莞陈冠希', '湛江吴彦祖', '瞎比写')";
//利用addBatch方法将SQL语句加入到stmt对象中
stmt.addBatch(sql);
if (i % 1000 == 0 && i != 0) {
//利用executeBatch方法执行1000条SQL语句
stmt.executeBatch();
stmt.clearBatch();
conn.commit();
}
}
stmt.executeBatch();
stmt.clearBatch();
conn.commit();
close(); //关闭资源
} catch (SQLException e) {
e.printStackTrace();
}
}
public void close() {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
Long end = new Date().getTime();
System.out.println("1000条数据插入花费时间 : " + (end - begin) / 1000 + " s"+" 插入完成");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HelloJava t = new HelloJava();
//t.createTable();
t.insertBatch();
}
}
效率非常低,用了秒表计算得知才10S才几百行的数据,现在不行的。和单挑数据插入没什么区别的。不知道问题出在哪里
学习了连接池
package mian;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by feyesesbvs on 2017/7/8.
*/
public class ConnectionPool {
List<Connection> cs =new ArrayList<Connection>();
int size;
public ConnectionPool(int size) {
this.size =size ;
init();
}
public void init (){
try{
Class.forName( "com.mysql.jdbc.Driver" );
for (int i =0;i< size;i++){
Connection c = DriverManager
.getConnection( "jdbc:mysql://**************:3306/db1?characterEncoding=UTF-8&useSSL=false","root","***************");
cs.add(c);
}
}catch (ClassNotFoundException e){
e.printStackTrace();
}catch (SQLException e){
e.printStackTrace();
}
}
public synchronized Connection getConnection(){
while (cs.isEmpty()){
try{
this.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
Connection c = cs.remove(0);
return c;
}
public synchronized void returnConnection(Connection c){
cs.add(c);
this.notifyAll();
}
}
连接池的测试类
package mian;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by feyesesbvs on 2017/7/8.
*/
public class TestConnectionPool {
public static void main(String [] args) {
ConnectionPool cp =new ConnectionPool( 250 );
for (int i = 0;i < 1000;i++){
new WorkingThread("working thred" + i,cp).start();
}
}
}
class WorkingThread extends Thread{
private ConnectionPool cp;
public WorkingThread(String name ,ConnectionPool cp){
super(name);
this.cp = cp;
}
public void run(){
Connection c = cp.getConnection();
System.out.println(this.getName()+":\t 获取一条连接,开始工作");
try(Statement st = c.createStatement()){
Thread.sleep( 1000 );
st.execute("select *from table1");
} catch (SQLException | InterruptedException e){
e.printStackTrace();
}
cp.returnConnection( c );
}
}
在run的过程中和run完后。发现了这些异常
明天的计划:重新想构造for循环 加快数据插入速度。复习整理任务1所学的知识。
遇到的问题:不知道为什么for循环那么慢。
收获:学习了使用for循环插入数据。学习了连接池的概念。知道了在jdbc中,有一种事务叫批处理。
评论