发表于: 2017-04-06 22:32:39

7 1288


今天完成的任务:

    还是不甘心,终于参考网上嵌套方法写了一个插入数据的类,只有原来时间的十分之一,插入了一百万条数据。为稳妥起见,只是用了math.random()方法,任意组合了数字。


package com.ptteng.DataInsert;


import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.util.Date;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.Types;

import java.util.ArrayList;

import java.util.List;


public class AppTest

{   

    private static final String DRIVER = "com.mysql.jdbc.Driver";

    private static final String URL = "jdbc:mysql://localhost:3306/student";

    private static final String USER = "root";

    private static final String PASSWORD = "wobushi369";

    

    private static Connection conn = null;

   

    static 

    {

        // load driver

        try 

        {

            Class.forName(DRIVER);

            conn = DriverManager.getConnection(URL, USER, PASSWORD);

         } 

        catch (ClassNotFoundException e)

        {

            e.printStackTrace();

         } 

        catch (SQLException e) 

        {

            e.printStackTrace();

         }

    }


    public static void showDbs()

    {

        String sql = "show databases";

        try

        {

            PreparedStatement pst = conn.prepareStatement(sql);

            ResultSet rst = pst.executeQuery();

            while (rst.next()) 

            {

                System.out.println(rst.getString(1));

            }

            rst.close();

            pst.close();

            conn.close();

        

        catch (SQLException e)

        {

            e.printStackTrace();

         }

    }


    public static void insert()

    {

        // 开时时间  

        Long begin = new Date().getTime();  

        // sql前缀  

        String prefix = "INSERT INTO insert_datatest (count, create_time, random) VALUES ";  

    

        try 

        {  

            // 保存sql后缀  

            StringBuffer suffix = new StringBuffer();  

            // 设置事务为非自动提交  

            conn.setAutoCommit(false);  

            // Statement st = conn.createStatement();  

            // 比起st,pst会更好些  

            PreparedStatement pst = conn.prepareStatement("");  

            // 外层循环,总提交事务次数  

            for (int i = 1; i <= 100; i++)

            {  

                for (int j = 1; j <= 10000; j++) 

                {  

                    // 构建sql后缀  

                    suffix.append("(" + j*i + ", SYSDATE(), " + i*j*Math.random() + "),");  

                } 

                

                // 构建完整sql  

                String sql = prefix + suffix.substring(0, suffix.length() - 1);  

                // 添加执行sql  

                pst.addBatch(sql);  

                // 执行操作  

                pst.executeBatch();  

                // 提交事务  

                conn.commit();  

                // 清空上一次添加的数据  

                suffix = new StringBuffer();  

            }

            // 头等连接  

            pst.close();  

            conn.close();  

        } 

        catch (SQLException e)

        {  

            e.printStackTrace();  

        }  

    

        // 结束时间  

        Long end = new Date().getTime();  

        // 耗时  

        System.out.println("cast : " + (end - begin) + " ms");  

    } 

  

    public static void main(String [] args)

    {

        insert();

        showDbs();

    }

}

效果如下:

明天的计划:

    今天下午复习了springMVC,比较了自己开始写的原始链接数据的的JDBC类,发现Java和spring的一些原理,尤其spring中配置的xml文件作用域,public修饰的类的作用域。莎莎师兄的讲解让人理清不少头绪,明天继续学习springMVC,新建一个springMVC项目。

遇到的问题:

     任务越往后,对前面的基础要求越多,不是简单记住就能做下来。需要消化前面接触到的知识,照猫画虎,换个项目也许就不知道东南西北了。零基础转行,面对很多基础知识要补习。

收获:

     从一个简陋的嵌套方法,扩展成一个可以实际运行插入100万条数据的工具类。进步虽小,却是坚持下去的动力,积跬步以至千里,方向重于速度,加油!

PS(1):

开始方法只执行了main里写在前面的insert();后面的showDbs();没有被执行,报错

 No operations allowed after connection closed.

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)

at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)

at com.mysql.jdbc.Util.getInstance(Util.java:386)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1015)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)

at com.mysql.jdbc.ConnectionImpl.throwConnectionClosedException(ConnectionImpl.java:1320)

at com.mysql.jdbc.ConnectionImpl.checkClosed(ConnectionImpl.java:1312)

at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4547)

at com.mysql.jdbc.ConnectionImpl.prepareStatement(ConnectionImpl.java:4512)

at com.ptteng.DataInsert.DataInsertTest.showDbs(DataInsertTest.java:58)

at com.ptteng.DataInsert.DataInsertTest.main(DataInsertTest.java:135)

解决:原因是在insert()方法里最后有con.close() 方法。将连接关闭了。删掉改方法,可用。


PS(2):附加题,可以试验用sql语句直接插入1000万条数据的运行效果如何?(暂存)


返回列表 返回列表
评论

    分享到