MySQL数据库表格copy

先写个数据库服务类方便调用连接

public class MysqlServer {

    protected Connection conn = null;

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    public MysqlServer(String databaseUri, String username, String passwd) throws SQLException {
        this.conn = DriverManager.getConnection(databaseUri, username, passwd);
    }

    public Connection getConnection() {
        return this.conn;
    }

    public void releaseQuery() {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

封装实体类就不用表述了(三个字段而已)
然后连接MySQL数据库过程
MysqlServer fromMysql = new MysqlServer(“jdbc:mysql://IP地址/库名”, “admin”, “admin”);// 目标数据库 Connection fromConn = (Connection) fromMysql.getConnection();
MysqlServer toMysql = new MysqlServer(“jdbc:mysql://IP地址/库名”, “admin”, “admin”);// 空数据库
Connection toConn = (Connection) toMysql.getConnection();

实例化阻塞队列ArrayBlockingQueue:基于数组实现的一个阻塞队列,在创建ArrayBlockingQueue对象时必须制定容量大小。并且可以指定公平性与非公平性,默认情况下为非公平的,即不保证等待时间最长的队列最优先能够访问队列。

ArrayBlockingQueue arrayListLabelSecondaryCoteGory = new ArrayBlockingQueue(20);
String searchSql = “select id,name,age from 表名”;
String insertSql = “insert into label_copy_secondary_category(id,name,age) values(?,?,?)”;
多线程写完后就是启动了,不管是内部类还是外部类,都要记得.stare()哦;

//读取数据线程
AddThread addThread = new AddThread(fromConn, arrayListLabelSecondaryCoteGory,searchSql);
addThread.start();

for(int i = 0;i<5;i++){
    //插入数据线程
    InsertThread thread = new InsertThread(toConn,arrayListLabelSecondaryCoteGory,insertSql);
            thread.start();
}

接下来写一个读取表格的多线程

class AddThread extends Thread {
    private Connection connection;
    private ArrayBlockingQueue<LabelSecondaryCateGory> arrayListLabelSecondaryCoteGory;
    private String searchSql;

    public AddThread(Connection connection,
            ArrayBlockingQueue<LabelSecondaryCateGory> arrayListLabelSecondaryCoteGory, String searchSql) {
        this.connection = connection;
        this.arrayListLabelSecondaryCoteGory = arrayListLabelSecondaryCoteGory;
        this.searchSql = searchSql;
    }

    @Override
    public void run() {
        try {
            if (connection != null) {
                PreparedStatement ps = connection.prepareStatement(searchSql);
                ResultSet resultSet = ps.executeQuery();
                while (resultSet.next()) {
                    int id = resultSet.getInt("id");
                    int parentid = resultSet.getInt("name");
                    String name = resultSet.getString("age");
                    arrayListLabelSecondaryCoteGory.put(new LabelSecondaryCateGory(id, parentid, name));
                }
                //设置标志位
                    arrayListLabelSecondaryCoteGory.put(new
                    LabelSecondaryCateGory(Integer.MAX_VALUE,1,null));
                System.out.println(arrayListLabelSecondaryCoteGory.size());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

由于阻塞队列一定要要同步进行,满了就会一直卡死,然后就写一个插入线程

class InsertThread extends Thread {
    private String insertSql;
    private Connection connection;
    private ArrayBlockingQueue<LabelSecondaryCateGory> arrayListLabelSecondaryCoteGory;

    public InsertThread(Connection connection,
            ArrayBlockingQueue<LabelSecondaryCateGory> arrayListLabelSecondaryCoteGory,
            String insertSql) {
        this.connection = connection;
        this.arrayListLabelSecondaryCoteGory = arrayListLabelSecondaryCoteGory;
        this.insertSql = insertSql;
    }

    @Override
    public void run() {
        System.out.println(connection);
        while(true){
            if(connection !=null){
                try {
                    PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
                    LabelSecondaryCateGory temp = arrayListLabelSecondaryCoteGory.take();
                    //根据标志位设置插入,如果不是就退出线程
                    if(temp.getId() != Integer.MAX_VALUE){
                        preparedStatement.setInt(1, temp.getId());
                        preparedStatement.setInt(2, temp.getName());
                        preparedStatement.setString(3, temp.getAge());
                        preparedStatement.execute();
                        System.out.println("has done");
                    }else{
                        System.out.println("退出线程");
                        arrayListLabelSecondaryCoteGory.put(new LabelSecondaryCateGory(Integer.MAX_VALUE,1,null));
                        break;
                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } 
            }
        }
    }
}

重点是阻塞队列,一定要同时进行,在多线程里面运行的时候一定要多加一个标志位,不然真卡死不动了,因为队列一直是空的它也会卡死。还有温习一下数据库的连接和语句。第一次写博客,有些不到位的地方可以扣扣加我共同探讨:378631689

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值