码迷,mamicode.com
首页 > 其他好文 > 详细

111222333

时间:2018-05-26 19:40:36      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:ali   util   jdb   for   perm   count   free   cat   unittest   

package com.sf.unitTest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sf.util.JdbcUtils;

public class threadTest {
    
         private static String url="jdbc:mysql://10.202.45.40:3306/sfim?characterEncoding=UTF-8";  
        private static String user="sfim";  
        private static String password="passwd123";  

        private static Logger log = LoggerFactory.getLogger(JdbcUtils.class);
        
        
        private threadTest()  
        {  
        }  
        static  
        {  
            try  
            {  
                Class.forName("com.mysql.jdbc.Driver");  
            }  
            catch(ClassNotFoundException e)  
            {  
                throw new ExceptionInInitializerError(e);  
            }  
        }  
        public static Connection getConnection() throws SQLException  
        {  
            return DriverManager.getConnection(url, user, password);  
        }  
        public static void free(ResultSet resultset,Statement st,Connection conn)  
        {  
            //6.释放资源  
            try{  
            if(resultset!=null)  
                resultset.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
                log.error("getUserPermissionsAES Exception===>", e);
            }  
            finally  
            {  
                try  
                {  
                    if(st!=null)  
                        st.close();  
                } catch (SQLException e) {  
                    e.printStackTrace();  
                    log.error("getUserPermissionsAES Exception===>", e);
                }  
                finally  
                {  
                    if(conn!=null)  
                        try {  
                            conn.close();  
                        } catch (SQLException e) {  
                            e.printStackTrace();  
                            log.error("getUserPermissionsAES Exception===>", e);
                        }  
                }  
            }  
        }
        
        private static final int loopNum = 1*10; 

    @SuppressWarnings("static-access")
    public static void main(String[] args) throws SQLException {
        /*Connection conn = null;
        Statement st = null;
        ResultSet resultset = null;*/
        threadTest threadTest = new threadTest();
        System.out.println("启动时间:"+new Date());
        for(int i=1;i<=100000;i++) {
            threadTest.m1(threadTest,i);
        }
        System.out.println("结束时间:"+new Date());
        
        
        /*try {
            conn = threadTest.getConnection();
            st = conn.createStatement();
            System.out.println("启动时间:"+new Date());
            for(int i=1;i<=100000;i++) {
                st.executeUpdate("INSERT INTO tab_moments_test_0(account,momentsid,create_time,state) VALUES(‘"+i+"‘,‘"+i+"‘,"+System.currentTimeMillis()+",‘"+i+"‘)");
                if(i==100000) {
                    System.out.println("结束时间:"+new Date());
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadTest.free(resultset, st, conn);
        }*/

    }
    
    public void m1(threadTest threadTest,int i) throws SQLException {  
        ExecutorService pool = Executors.newCachedThreadPool();  
        Connection conn = threadTest.getConnection();
        Statement st = conn.createStatement();
        ResultSet resultset = null;
        for (int index = 0; index < loopNum; index++) {    
            Runnable run = new Runnable() {    
                public void run() { 
                    try {
                        st.executeUpdate("INSERT INTO tab_moments_test_0(account,momentsid,create_time,state) VALUES(‘"+i+"‘,‘"+i+"‘,"+System.currentTimeMillis()+",‘"+i+"‘)");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }finally {
                        threadTest.free(resultset, st, conn);
                    }
                    
                }    
            };   
            pool.execute(run);    
        }    
        pool.shutdown();    
    }

}

 

111222333

标签:ali   util   jdb   for   perm   count   free   cat   unittest   

原文地址:https://www.cnblogs.com/yinyanji/p/9093893.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!