《txt文件导入oracle》由会员分享,可在线阅读,更多相关《txt文件导入oracle(6页珍藏版)》请在金锄头文库上搜索。
1、package dataimport;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import ja
2、va.sql.Statement;import java.util.ArrayList;import java.util.HashMap;import log.Log;public class Import_Data String url = jdbc:oracle:thin:;String userName = null;String password = null;Driver driver = null;public static Connection connection = null;public static Log log=new Log();PreparedStatement
3、stmt=null;PreparedStatement stmt1=null;static ArrayList wenjianlujing=new ArrayList();/* param args* filepath-txt 文件路径* 取文件名作为表名,文件第一行作为表头,从第二行起为数据*/public void txtToDb(String filepath)/数据库名称、用户名、密码String driver = oracle.jdbc.OracleDriver; String url=jdbc:oracle:thin:WWW-8E98D3889CA:1521:ORCL; Strin
4、g name = lc; String pwd = laoban;String table_name=liuc_test;try/先链接数据库/1.加载驱动Class.forName(driver);/2.得到连接 Connection ct=DriverManager.getConnection(url,name,pwd);/从下面开始和 sql server 一模一样Statement sm=ct.createStatement();/执行 sql 语句得到数据集ResultSet rs=sm.executeQuery(select * from liuc_test);/循环打印while
5、(rs.next()System.out.println();System.out.print(用户 id:+rs.getInt(1)+ );System.out.print(用户名:+rs.getString(2)+ );System.out.print(密码:+rs.getString(3)+ );System.out.println();rs.close();sm.close();/清空原本数据库/*String sqlClearTable = DELETE FROM + table_name;PreparedStatement stmt=ct.prepareStatement(sqlC
6、learTable); stmt.executeUpdate();stmt.close();*/读入文件File file = new File(filepath); BufferedReader buffer = new BufferedReader(new FileReader(file);String read_data=buffer.readLine();while (read_data!=null)String str = null;str=read_data.split( ); /据 txt 文件内的逗号解析if (str!=null) String sqlInsert = INS
7、ERT INTO +table_name+(xh,servnumber,PASS_WORD,insert_date)+ values +(+str0+,+str1+,+str2+,sysdate);PreparedStatement stmt1=ct.prepareStatement(sqlInsert);stmt1.executeUpdate();stmt1.close(); /注意及时关闭 prepareStatementread_data=buffer.readLine();buffer.close();mit();ct.close();catch(Exception ex)ex.pri
8、ntStackTrace();/* param args* filepath-txt:数据文件路径* confile_path:配置文件路径*/public void txtToDb(String filedir,String confile_path)File filepath=new File(filedir);/先将文件夹下的 txt 文件名称获得try quwenjian(filepath); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();System.out.println(获取文
9、件时报错!);String table_name=liuc_test;int count_row=0;/行数记录try/先链接数据库Connection ct=getConnect(confile_path);if (ct!=null)System.out.println(链接数据库成功!);/*Statement sm=ct.createStatement(); /执行 sql 语句得到数据集ResultSet rs=sm.executeQuery(select * from liuc_test);/循环打印while(rs.next()System.out.println();System
10、.out.print(用户 id:+rs.getInt(1)+ );System.out.print(用户名:+rs.getString(2)+ );System.out.print(密码:+rs.getString(3)+ );System.out.println();rs.close();sm.close();*/ /*/清空原本数据库String sqlClearTable = DELETE FROM + table_name;stmt=ct.prepareStatement(sqlClearTable); stmt.executeUpdate();stmt.close();*/else
11、System.out.println(链接数据库失败!); for (int i=0;iwenjianlujing.size();i+)String file_path=wenjianlujing.get(i);/读入文件File file = new File(file_path); BufferedReader buffer = new BufferedReader(new FileReader(file);String read_data=buffer.readLine();while (read_data!=null)String str = null;str=read_data.sp
12、lit( ); /据 txt 文件内的逗号解析if (str!=null) count_row+;String sqlInsert = INSERT INTO +table_name+(xh,servnumber,PASS_WORD,insert_date)+ values +(+str0+,+str1+,+str2+,sysdate);stmt1=ct.prepareStatement(sqlInsert);stmt1.executeUpdate();read_data=buffer.readLine();buffer.close();mit();Log.saveSqlLog(file_pa
13、th,ct,count_row);stmt1.close(); /注意及时关闭 prepareStatementct.close();System.out.println(插入数据完成!);catch(Exception ex)ex.printStackTrace();public Connection getConnect(String confile_path) int rowNumber = 0;String login = null; try/FileReader reader = new FileReader(D:eclipseworkspacesms_mmswords.txt);
14、FileReader reader = new FileReader(confile_path);BufferedReader br = new BufferedReader(reader); String s = null; br.mark(100);while(br.readLine() != null) rowNumber+;login = new StringrowNumber;br.reset();for (int i = 0; i rowNumber; i+)s = br.readLine();logini = s;br.close();reader.close();url = u
15、rl + login0;userName = login1;password = login2;Class.forName(oracle.jdbc.driver.OracleDriver);connection=DriverManager.getConnection(url,userName,password);connection.setAutoCommit(false);/设置为不自动提交catch(Exception e)e.printStackTrace(); return connection;public void closeconn(Connection connection) try connection.close(); catch (SQLException e) e.printStackTrace();public void quwenjian(File files) throws IOExceptionFile tempList=files.listFiles(); for (in