Java 数据库导出txt

package com.dtbridge.loan.util;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.dtbridge.core.util.Connect;
import com.dtbridge.loan.dayend.sqlmapdao.DayLoanBaseSqlMapDaoNew;

public class 信贷导文本 {
	private static final Logger log = Logger.getLogger(DayLoanBaseSqlMapDaoNew.class.getName());
	public Connect connect = new Connect("jdbc.properties");
	private static 信贷导文本 instance = null;
	private FileWriter fileWriter = null;
	private boolean isWriteFirstLine;
	private long startTime = 0;
	private long endTime = 0;
	private int resultFieldsLength = 0;
	private int errFieldIndex=0;
	private int loopStep = 0;
	private StringBuffer lineStr = null;
	
	public static synchronized 信贷导文本 getInstance() {
		if (instance == null)
			instance = new 信贷导文本();
		return instance;
	}
	
	private Connection conn = connect.getConnection();
	public Statement stmt = null;
	
	public 信贷导文本(){}
	
	
	public void 导出信贷公司客户信息表(){
		log.info("(2): 导出信贷公司客户信息表开始: 导出文件名称为 D://信贷公司客户信息表.txt");
		startTime = System.currentTimeMillis();
		isWriteFirstLine = true;
		String[] resultFields = {"cus_id","loan_id","cn_com_name","com_kind","cus_field","cus_kind","org_id","rep_name","rep_person_id","com_reg_id","Nat_Tax_Id","Loc_Tax_Id","Reg_Capital","Acc_Permit","current_address","Com_Phone","class_1","class_2","class_gb","opt_capital","in_credit_class","reg_org_id","reg_by_id","reg_date"};
		String[] resultFieldsName = {"客户号","贷款卡号","客户名称","企业性质","客户所属领域","客户种类","组织机构代码","法定代表人","法定代表人身份证号码","营业执照号码","国税登记证号","地税登记证号","注册资本","开户许可证","办公地址","联系电话","行业大类","行业中类","行业分(小)类","企业规模","内部信用等级","登记机构","登记人","登记日期"};
		String sql = "select cus_id,loan_id,cn_com_name,com_kind,cus_field,cus_kind,org_id,rep_name,rep_person_id,com_reg_id,Nat_Tax_Id,Loc_Tax_Id,Reg_Capital,Acc_Permit,current_address,Com_Phone,class_1,class_2,class_gb,opt_capital,in_credit_class,reg_org_id,reg_by_id,reg_date from cus_com";
		ResultSet rs = null;
		int totalRows = 0;
		try {
			stmt = conn.createStatement();
			fileWriter = new FileWriter(new File("D://信贷公司客户信息表.txt"));
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				fileWriter.write(getLinedString(rs,resultFields,resultFieldsName,","));
			}
			if(fileWriter !=null)
				fileWriter.close();
			totalRows = rs.getRow();
			if(rs != null)
				rs.close();
		} catch (SQLException e) {
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		} catch (Exception e) {
		} 
		endTime = System.currentTimeMillis();
		log.info("导出信贷公司客户信息表结束: 共导出 '" + totalRows + "' 条! 共耗时: " + Math.round((endTime - startTime)/60000.0) + "分钟!");
	}

	
	
	private String getLinedString(ResultSet rs,String[] resultFields,String[] resultFieldsName,String decifer) throws Exception{
		if(resultFields.length!=resultFieldsName.length)
			throw new Exception("要导出的字段名称与字段名称说明的长度不一致!");
		if(lineStr == null)
			lineStr = new StringBuffer(800);
		else
			lineStr.delete(0,lineStr.length());
		resultFieldsLength = resultFields.length;
		try {
			if(isWriteFirstLine){
				for(loopStep=0; loopStep<resultFieldsName.length; loopStep++){
					lineStr.append(resultFieldsName[loopStep])
					.append(decifer);
				}
				lineStr.append("\r\n");
			}
			isWriteFirstLine = false;
			for(loopStep=0; loopStep<resultFieldsLength; loopStep++){
				errFieldIndex = loopStep;
				if(rs.getString(resultFields[loopStep]) != null)
					lineStr.append(rs.getString(resultFields[loopStep]).trim())
						.append(decifer);
				else
					lineStr.append("")
						.append(decifer);
			}
			lineStr.deleteCharAt(lineStr.lastIndexOf(decifer)).append("\r\n");
		} catch (SQLException e) {
			log.info("要导出的表数据中没有这个字段: '" + resultFields[errFieldIndex] + "'");
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lineStr.toString();
	}
	
	public void dataExport(){
		log.info("信贷导数程序开始:");
		long start = System.currentTimeMillis();
		导出信贷公司客户信息表();

		try {
			if(stmt != null)
				stmt.close();
			if(conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		log.info("信贷导数程序结束: 共耗时: " + Math.round((end - start)/60000.0) + "分钟!");
	}
	
	public static void main(String []a){
		信贷导文本.getInstance().dataExport();
	}
}

你可能感兴趣的:(java,apache,sql,log4j,jdbc)