工具 mysqlToSqlite

package util;
import java.sql.*;

public class MysqlToSqlite {
    public void importData(String tableName, String sqlitePath) throws SQLException {
        Connection mysqlconn = null;
        Statement statement = null;
        ResultSet rs = null;
        Connection sqliteconn = null;
        PreparedStatement preStatement = null;
        try {
            //MySQL数据库、
            mysqlconn = getMysqlconn();
            statement =mysqlconn.createStatement();
            rs = statement.executeQuery("select * from "+tableName);

            //结果集获取到的长度,即表有多少字段
            int size = rs.getMetaData().getColumnCount();
            //拼接插入语句
            StringBuffer sbf =new StringBuffer();
            sbf.append("insert into "+tableName+" values (");
            String link ="";
            for (int i = 0; i <size ; i++) {
                sbf.append(link).append("?");
                link=",";
            }
            sbf.append(")");
            //连接Sqlite数据库,清空表数据
            sqliteconn = getIteconn(sqlitePath);
            preStatement = sqliteconn.prepareStatement("delete from "+tableName);
            preStatement.executeUpdate();

            preStatement = sqliteconn.prepareStatement(sbf.toString());

            //取出结果集并向SQLite数据库插入数据 (使用批处理 )
            //完成条数
            int count =0;
            int num=0;
            //取消事务
            sqliteconn.setAutoCommit(false);
            long start = System.currentTimeMillis();
            while (rs.next()) {
                ++count;
                for (int i=1;i<= size;i++) {
                    preStatement.setObject(i, rs.getObject(i));
                }

                //将预先语句存储起来,这里还没有向数据库插入
                preStatement.addBatch();
                //当count 到达 200条时 向数据库提交
                if (count % 200 ==0 ){
                    ++num;
                    preStatement.executeBatch();
                }
            }
            //防止有数据未提交
            preStatement.executeBatch();
            //提交事务
            sqliteconn.commit();
            System.out.println("完成 "+count+" 条数据,耗时:"+(System.currentTimeMillis()-start)/1000.0+"s");
        } catch (SQLException e) {
            e.printStackTrace();
            sqliteconn.rollback();
        } finally {
            //关闭资源
            close(sqliteconn,preStatement,null);
            close(mysqlconn,statement,rs);
        }

    }

    private Connection getIteconn(String sqlitePath){
        try {
            Class.forName("org.sqlite.JDBC");
            return DriverManager.getConnection("jdbc:sqlite:" + sqlitePath);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    private Connection getMysqlconn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            return DriverManager.getConnection("url","userName","password");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public  void  close(Connection conn,Statement stmt,ResultSet rs){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ranx3

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值