欢迎来到天天文库
浏览记录
ID:50845077
大小:31.92 KB
页数:3页
时间:2020-03-15
《简单的java解析文本文件批量导入数据库.docx》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、packageutil;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.UnsupportedEncodingException;importjava.sql.Connection;importjava.sql.DriverManager;impor
2、tjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;/***批量导入数据库*@authorAdministrator**/publicclassInsertDB{publicConnectiongetConnection(){Connectionconn=null;try{Class.forName("oracle.jdbc.driver.OracleDriver");conn=DriverManager.getConnection("jdbc:oracle:
3、thin:@****:**:**","**","**");}catch(Exceptione){e.printStackTrace();}returnconn;}publicstaticvoidmain(String[]args){InsertDBrt=newInsertDB();rt.AhPhoneInsert("C:\DocumentsandSettings\Administrator\桌面\C.txt");//参数为你的txt文件路径}//数据记录导入publicbooleanAhPhoneInsert(Stringfile){try{Connectio
4、ncon=getConnection();PreparedStatementps=null,ps1=null,ps2=null;ResultSetrs=null;BufferedReaderbr=newBufferedReader((newInputStreamReader(newFileInputStream(newFile(file)),"utf-8")));//编码方式为utf-8,txt保存时编码方式也要选择为utf-8Stringline;try{Stringsql="insertintoCvalues(?)";try{con.setAutoCommit(f
5、alse);ps=con.prepareStatement(sql);intn=0;while((line=br.readLine())!=null){Stringphone=line.trim();ps.setString(1,phone);ps.addBatch();n++;if(n>1000){ps.executeBatch();n=0;}}ps.executeBatch();con.commit();}catch(SQLExceptione){e.printStackTrace();}finally{if(null!=rs){try{rs.close();}c
6、atch(SQLExceptione){e.printStackTrace();}}if(null!=ps){try{ps.close();}catch(SQLExceptione){e.printStackTrace();}}if(null!=ps2){try{ps2.close();}catch(SQLExceptione){e.printStackTrace();}}if(null!=ps1){try{ps1.close();}catch(SQLExceptione){e.printStackTrace();}}if(null!=con){try{con.clo
7、se();}catch(SQLExceptione){e.printStackTrace();}}}}catch(IOExceptione){e.printStackTrace();}}catch(UnsupportedEncodingExceptione){e.printStackTrace();}catch(FileNotFoundExceptione){e.printStackTrace();}returntrue;}}
此文档下载收益归作者所有