1. 程式人生 > >Java檔案讀取,寫入資料庫

Java檔案讀取,寫入資料庫

/**
 * 檔案讀寫及資料入庫工具包
 */
package com.xxx.file;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


/**
 * @author QZ_ba
 * 檔案讀取類
 */
public class ReadFile3 {
//建立資料庫連線
static Connection con = null;
//建立預編譯語句物件
static Statement pstmt = null;
//建立一個結果集
static ResultSet result = null;
/**
* 初始化資料庫連線驅動
*/
private void initJdbc(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//載入驅動程式
String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";              //資料庫連線資訊
String user = "admin1001";//資料庫使用者名稱
String pass = "admin1001";//資料庫密碼
con = DriverManager.getConnection(url, user, pass);//獲取連線
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 檔案讀取操作
* @param fileName 檔名(全路徑名稱)
* @throws IOException 
*/
private void readFile(String fileName) throws IOException{
BufferedReader in = new BufferedReader(new FileReader(fileName));//閱讀器
   String line;//行資料
   String tbName = null;//標籤名--資料庫表名
   String[] cols = null;//標籤列名--資料庫列名
   String[] vals = null;//標籤列值--資料庫列值


   List<String> inserList = new ArrayList<String>();//插入語句集合
   List<String> tbList = new ArrayList<String>();                   //表名集合
   List<String[]> colSz = new ArrayList<String[]>();//列名陣列集合
   PrintWriter out = new PrintWriter(new FileWriter("F:/test/insert.txt"));//
   try {
   while(null!=(line = in.readLine())){
   if(line.startsWith("<") && !line.startsWith("<!") && !line.startsWith("</")){//解析表名--以"<"開頭,且不以"<!"和"</"開頭
   tbName = line.substring(line.indexOf("<") + 1, line.indexOf(":"));//從"<"開始擷取到第一個":"
   tbList.add(tbName);
   }else if(line.startsWith("@")){//解析列名
   cols = line.replaceAll("@", "").split("\\s{2,}|\t");//替換掉開頭"@",按多空格或Tab鍵分組擷取,獲得列名陣列
   colSz.add(cols);
   }else if(line.startsWith("#")){//解析資料
   vals = line.split("\\s{2,}|\t");//按多空格或Tab鍵分組擷取,獲得資料陣列
   String ists = null;         //插入資料SQL
ists = "INSERT INTO " + tbName + "_TEST(";
for (int i = 0; i < cols.length; i++) {
ists += cols[i] + ",";
}
ists = ists.substring(0, ists.length() - 1);
ists += ") VALUES(";
String val = "";
for (int i = 1; i < vals.length; i++) {
if(!"NULL".equals(vals[i])){
val = vals[i];
}
ists += "'" + val + "',";
val = "";
}
ists = ists.substring(0, ists.length() - 1);
ists += ")";
inserList.add(ists);

   }else{//此處出現列值中有換行的現象,需要做特殊處理
   //若無插入語句,跳出當前迴圈,繼續執行下次迴圈
   if(null == inserList || inserList.size() == 0) continue;
   if(line.startsWith("//")) continue;
   /* 將出現換行的行按多空格或製表符進行分割 並將其陣列值插入上一行插入語句的valus後 */
   String[] hxStr = line.split("\\s{2,}|\t");
   String insertStr = inserList.get(inserList.size() - 1).replace(")", "");//獲取上一行插入語句,並將結尾處的)替換為空
   for (int i = 0; i < hxStr.length; i++) {
   insertStr += ",'" + hxStr[i] + "'";//追加換行資料切割後的每一個值
}
   insertStr += ")";//將values最後追加),插入語句完整 
   inserList.remove(inserList.size() - 1);//刪除上一行插入語句
   inserList.add(insertStr);              //將新的插入語句插入到list中
   }
   }
   for (String str : inserList) {
   out.write(str + ";");
out.println();
}
   out.flush();
   System.out.println("資料插入語句寫完畢!");
   
    createTables(tbList,colSz);
insertData(inserList);
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(null != out) out.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
/**
* 建表操作
* @param createList
*/
private void createTables(List<String> tbName,List<String[]> col) {
try {
pstmt = con.createStatement();
int tlen = tbName.size();   //表個數
int clen = 0;               //列個數
String[] cols = {};         //列名陣列
String createSql = "";
String tname = "";
List<String> clist = new ArrayList<String>();
PrintWriter out = new PrintWriter(new FileWriter("F:/test/create.txt"));
for (int i = 0; i < tlen; i++) {
tname = tbName.get(i);
cols = col.get(i);
clen = cols.length;
createSql = "CREATE TABLE "+tname+"_TEST(";
for (int j = 0; j < clen; j++) {
createSql +=  cols[j] + " VARCHAR2(1000),";
}
createSql = createSql.substring(0, createSql.length() - 1) + ")";
out.write(createSql);
out.println();
clist.add(createSql);
pstmt.addBatch(createSql);
}
out.flush();
pstmt.executeBatch();
try {
if (null != out) {
out.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
System.out.println("建表完畢!");
} catch (Exception e) {
e.printStackTrace();
}finally{

}

}
/**
* 資料入庫操作
* @throws ClassNotFoundException 
* @throws SQLException 
*/
private void insertData(List<String> inserList) throws ClassNotFoundException{
try{
pstmt = con.createStatement();
System.out.println("資料條數:" + inserList.size());
for (String string : inserList) {
pstmt.addBatch(string);
}
pstmt.executeBatch();
}catch (Exception e) {
e.printStackTrace();
}finally{

}
}

/**
* @param args
*/
public static void main(String[] args) {
ReadFile3 rd = new ReadFile3();
try {
rd.initJdbc();
long begin = System.currentTimeMillis();
System.out.println("資料插入準備!開始時間為:" + begin);
rd.readFile("F:/123.CIME");
long end = System.currentTimeMillis();
System.out.println("資料插入完畢!結束時間為:" + end);
try {
if(null != pstmt){
pstmt.close();
}
if(null != con){
con.close();
}
} catch (Exception e2) {
}
long mills = (end - begin) / 1000 / 60 ;
System.out.println("耗時約:" + mills + "分鐘!");
} catch (Exception e) {
e.printStackTrace();
}


}


}