JDBC批量处理dome


import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Random;



public class TestJDBC {

	public static void main(String[] args) {
		Date date = new Date();
		Long time = date.getTime();
		boolean[] gender = {true,false};
		String[] name = {"会计","业务","java","c","前端"};
		String[] name1 = {"会计","业务员","程序员","ui设计"};
		String[] work = {"百度","腾讯","阿里巴巴","淘宝","第一财经"};
		String sql = "INSERT INTO `t_specialist_management2` VALUES (null,?,?,?,'1547273548215.jpg',?,?,?,?,?,?);";
		Connection con = null;
		PreparedStatement ps = null;
		try {
//			Class.forName("com.mysql.jdbc.Driver");
//			 con = DriverManager.getConnection(
//					"jdbc:mysql://127.0.0.1:3306/forestpestcontrolsystem?userecode=true&charectar=utf8",
//					"root",
//					"1234");
			MyDataSource dataSource = new MyDataSource();
			con = dataSource.getConnection();

			con.setAutoCommit(false);
			
			ps = con.prepareStatement(sql);
			//5、插入数据

			for(int i = 0; i < 1; i++){
				
				ps.clearBatch(); 

	                        //一次性提交1W条

				for(int j = 0; j < 4900; j++){

					ps.setString(1,RandomValue.getChineseName());
					ps.setBoolean(2, gender[(int)(Math.random()*2)]);
					ps.setDate(3, new java.sql.Date(randomDate("2000-07-01","2019-01-01").getTime()));
					ps.setString(4, name[(int)(Math.random()*5)]);
					ps.setString(5, name1[(int)(Math.random()*4)]);
					ps.setString(6, RandomValue.getTel());
					ps.setString(7, work[(int)(Math.random()*5)]);
					ps.setString(8, RandomValue.getRoad());
					ps.setString(9, RandomValue.getEmail(20, 30));
					System.out.println(i);

												

					ps.addBatch();

				}

				//批处理

				ps.executeBatch();

	                        //提交

				con.commit();				

			}
			

            con.commit();
            ps.close();	

    		dataSource.releaseConnection(con);


		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	private static char getRandomChar() {
        String str = "";
        int hightPos; //
        int lowPos;

        Random random = new Random();

        hightPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(hightPos)).byteValue();
        b[1] = (Integer.valueOf(lowPos)).byteValue();

        try {
            str = new String(b, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            System.out.println("错误");
        }

        return str.charAt(0);
    }
	
	 private static Date randomDate(String beginDate,String endDate){
         try {
             SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
             Date start = format.parse(beginDate);  // 构造开始日期
             Date end = format.parse(endDate);  // 构造结束日期
             // getTime()表示返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数。
           if(start.getTime() >= end.getTime()){
                 return null; 
            }
                           long date = random(start.getTime(),end.getTime());
                           return new Date(date);          } catch (Exception e) { 
            e.printStackTrace();
         }          return null; 
	    } 
          private static long random(long begin,long end){
         long rtn = begin + (long)(Math.random() * (end - begin)); 
         // 如果返回的是开始时间和结束时间,则递归调用本函数查找随机值 
        if(rtn == begin || rtn == end){
             return random(begin,end);
         }
         return rtn;
     }  
          public static class MyDataSource{

      	    //链表 --- 实现栈结构

      	    private LinkedList<Connection> dataSources = new LinkedList<Connection>();

      	    //初始化连接数量

      	    MyDataSource() {

      			//一次性创建10个连接

      			for(int i = 0; i < 10; i++) {

      				try {

      					//1、装载驱动对象

      					 Class.forName("com.mysql.jdbc.Driver");

      					 //2、通过JDBC建立数据库连接

      					 Connection con =DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/forestpestcontrolsystem?userecode=true&charectar=utf8","root","1234");

      					 //3、将连接加入连接池中

      					dataSources.add(con);

      				} catch (Exception e) {

      					e.printStackTrace();

      				}

      			}

      		}

       

      	    public Connection getConnection() throws SQLException {

      	           //取出连接池中一个连接

      	           final Connection conn = dataSources.removeFirst(); // 删除第一个连接返回

      	           return conn;

      	    }

       

      	    //将连接放回连接池

      	    public void releaseConnection(Connection conn) {

      	           dataSources.add(conn);

      	    }

      	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值