实现sql语句插入功能

今天碰到数据库迁移的问题,从mysql数据库迁移到db2数据库,首先尝试了 ibm 的ibm_mtk_V2_win进行数据迁移,但是狗血的出现了很多问题(这个只支持java6,版本太高记得换回来) 后来想到了写脚本(主要是一个sql有一两百兆的数据,复制粘贴执行不可行)

java 编写

package com.hsm.db;

import java.io.*;
import java.sql.*;

/**
 * 读取指定文件下sql脚本,执行到数据库
 * 朱行读取分批处理批量插入数据库
 */
public class TestReadFile {
    public static void main(String[] args) {
        System.err.println("begin");
        long start = System.currentTimeMillis();
        String path = "C:\\Users\\huangsm@allinfinance.com\\Desktop\\tm_account.sql";
        getData(path);
        System.err.print((System.currentTimeMillis() - start) / 1000);

    }

    private static void getData(String path) {
        //读取文件
        BufferedReader reader;
        Connection conn = null;
        Statement pst = null;
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            conn = DriverManager.getConnection(
                    "jdbc:db2://10.250.1.211:50000/zyjr:currentSchema=C;","zyjr","zyjr");
            System.out.println("数据库连接成功!!!");
            pst = conn.createStatement();
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
            String line;
            int i = 0;
            
            while ((line = reader.readLine()) != null) {
            	//主要防止空行问题,个人觉得这样比简单
            	if(line.length()<=10){
            		continue;
            	}
            	if(i==0){
            		//这个执行的时候发现的问题,不知道为什么,用这个测试
            		System.out.println(line);
                	pst.execute(line.substring(1,line.length()-1));
            	}else{
            		System.out.println(line);
            		pst.execute(line.substring(0,line.length()-1));
            	}
                if (i % 100 == 0) {
                    System.out.println("执行了:" + i);
                }
                i += 1;
            }
            reader.close();
            // 执行批量更新
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

写完之后被一个家伙说,这样写数据库插入太慢了,我就呵呵了,毕竟花了这么长时间,最好告诉我可以写sh脚本,而且还提供了sh脚本案例,我就开心的换了一个方式.(工作就是好,可以互相的交流)

sh 脚本实现

#!/bin/sh

echo "---------------------------------	cts数据初始化	START----------------------------"

db2 connect to zyjrdb user zyjrusr using zyjrusr
db2 set schema=CTS
#插入数据
#db2 -tvf tm_account.sql
db2 -tvf tm_address.sql
db2 -tvf tm_card.sql
db2 -tvf tm_contact_tel.sql

db2 -tvf tm_contract.sql
db2 -tvf tm_customer.sql
db2 -tvf tm_repayment.sql
#db2 -tvf tm_period.sql
db2 connect reset
echo "---------------------------------	cts 数据初始化	END ----------------------------"

这个脚本很简单,主要就是把数据执行写在了一起,还有执行脚本的时候一定要让脚本具有可执行权限,语句为

chmod +x fielname #具体细节可以看书

这个时候想起了一句话:入了行,什么都简单了

转载于:https://my.oschina.net/u/2615530/blog/865769

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值