解析txt文件并查询写txt文件并保存数据

*********解析txt文件并且将txt文件中的字段进行查询操作并且写入txt文件并且保存查询字段*********


public class RouseBD {
public String encoding = "GBK";
public InputStreamReader read;
public BufferedReader bufferedReader;
ResultSet rsset = null;
PreparedStatement pstmt = null; 
Connection conn = null;
static List<String> zylist = new ArrayList<String>();
public static String flag = "FALSE";
static{
//资源名称
// zylist.add("bjtxzxx");
// zylist.add("crjryxx");
// zylist.add("ycmxx");
// zylist.add("bmxx");
// zylist.add("exjczkkxx");
// zylist.add("swryxx");
zylist.add("T_YH");
}
public void rouseCom(){
//解析txt文档
String encoding = "GBK";
File file = new File("d://lzq_bf_new/bianfang-portal/src/main/java/com/egf/ws/zybz.txt");
if (file.isFile() && file.exists())
{
try {
read = new InputStreamReader( new FileInputStream(file), encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bufferedReader = new BufferedReader(read);
String lineTXT = null;
int num = 0;
Map<String, String> data = new HashMap<String, String>();
try {
while ((lineTXT = bufferedReader.readLine()) != null){
//解析的txt文档中,包含的有限身份证限制为1000个
if(data.size()>1000){
return;
}
if(num==0){
num++;
continue;
}else{
String[] linedata = lineTXT.split(";");
//暂存身份证和姓名
if(linedata[0].length()!=18 && linedata[0].length()!=15){
return;
}
data.put(linedata[0], linedata[1]);
}
num++;
}
} catch (IOException e) {
e.printStackTrace();
}
execute(data);
}else{
System.out.println("找不到指定的文件!");
}
}
/**
* 执行数据库操作
* @param map
*/
public void execute(Map<String, String> map){
Iterator zylt = zylist.iterator();
while(zylt.hasNext()){
String tablename = (String) zylt.next();
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
conn = DriverManager.getConnection("jdbc:db2://10.1.7.74:50000/bf", "db2inst1", "db2");
String sql = "select * from QBYWK."+tablename+" where sfzh=?";
pstmt = conn.prepareStatement(sql);
Set<Map.Entry<String, String>> set = map.entrySet();
Iterator<Map.Entry<String, String>> it = set.iterator();
//创建文件
String path = "d://"+tablename+".txt";
File filename = new File(path);
if (!filename.exists()) {
filename.createNewFile();
}
BufferedWriter output = new BufferedWriter(new FileWriter(filename));
while(it.hasNext()){
Map.Entry<String, String> me = it.next();
String sfzh = me.getKey();
pstmt.setString(1, sfzh);
rsset=pstmt.executeQuery();
//处理查询结果
resultHandle(tablename, rsset,output); 
}
} catch (Exception e) {
e.printStackTrace();
}
finally { 
   try {
if(rsset != null) { 
     rsset.close(); 
rsset = null; 
}
    if(pstmt != null) { 
     pstmt.close(); 
     pstmt = null; 
    } 
    if(conn != null) { 
     conn.close(); 
     conn = null; 
    } 
   } catch (SQLException e) { 
    e.printStackTrace(); 
   } 
}
}
//保存有效查询信息
saveInfo(map);
}

/**
* 处理查询结果集
* @param rs
*/
public void resultHandle(String tablename,ResultSet rsset,BufferedWriter output){

ResultSetMetaData rsmd;
try {
rsmd = rsset.getMetaData();
int columnCount = rsmd.getColumnCount();
String liename="";
String liedata="";
if(flag.equals("FALSE")){
for (int i=1; i<=columnCount; i++){  
String lie = rsmd.getColumnName(i);
liename+=(lie+";");
}  
output.write(liename+"\n");
liename="";
flag="TRUE";
}
while (rsset.next()){ 
for (int i=1; i<=columnCount; i++){  
String lie = rsset.getString(i);
liedata+=(lie+";");
}
output.write(liedata+"\n");
liedata="";
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
}  
}
/**
* 保存有效查询信息
* @param map
*/
public void saveInfo(Map<String, String> map){
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyy-MM-dd");
Date da = new Date();
try {
Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
conn = DriverManager.getConnection("jdbc:db2://10.1.7.74:50000/bf", "db2inst1", "db2");
String sql2 = "insert into QBYWK.T_QQBFZYINFO VALUES(QBYWK.SEQ_T_QQBFZY.nextval,?,?,?)";
pstmt = conn.prepareStatement(sql2);
Set<Map.Entry<String, String>> set2 = map.entrySet();
Iterator<Map.Entry<String, String>> it2 = set2.iterator();
while(it2.hasNext()){
Map.Entry<String, String> me = it2.next();
String sfzh = me.getKey();
pstmt.setString(1, sfzh);
pstmt.setString(2, me.getValue());
pstmt.setString(3, dateformat1.format(da));
            //在oracle中执行插入操作可以使用pstmt.executeQuery()
            //在db2中执行插入操作不可以,只可以使用pstmt.execute()
pstmt.execute();
}
} catch (Exception e) {
e.printStackTrace();
}
finally { 
   try {
if(rsset != null) { 
     rsset.close(); 
rsset = null; 
}
    if(pstmt != null) { 
     pstmt.close(); 
     pstmt = null; 
    } 
    if(conn != null) { 
     conn.close(); 
     conn = null; 
    } 
   } catch (SQLException e) { 
    e.printStackTrace(); 
   } 
}
}

你可能感兴趣的:(txt)