简单的java解析文本文件批量导入数据库.docx

简单的java解析文本文件批量导入数据库.docx

ID:50845077

大小:31.92 KB

页数:3页

时间:2020-03-15

简单的java解析文本文件批量导入数据库.docx_第1页
简单的java解析文本文件批量导入数据库.docx_第2页
简单的java解析文本文件批量导入数据库.docx_第3页
资源描述:

《简单的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;}}

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。