`

java读取txt文件,导入数据库

    博客分类:
  • java
阅读更多
package com.egf.qingbao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* @author xh
* @version $Revision: 1.1 $
* @since 0.1
*/
public class CopyOfReadTxtutil {
private static Connection conn = null;

public static void readTxtFile(String filePath) {
conn = JdbcUtil.getConnection("oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@xxx.x.x.xxx:1521:tjkf", "ypxx", "ypxx");
PreparedStatement pre = null;
String sql = "insert into yp_tldp values(SEQ_YP_TLDP.nextval,?,?,?,?,?,?,?,?,?,?,?)";
String encoding = "UTF-8";
long startTime = System.currentTimeMillis();
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(file),
encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
// List<String> datas = new ArrayList<String>();
int i = 0;
try {
conn.setAutoCommit(false);
// 开始事务
pre = conn.prepareStatement(sql);
try {
while ((lineTxt = bufferedReader.readLine()) != null) {
String[] tldp = lineTxt.split(" ");
i++;
pre.setString(1, tldp[2]);
pre.setString(2, tldp[1]);
pre.setString(3, tldp[6]);
pre.setString(4, tldp[5]);
pre.setString(5, tldp[3]);
pre.setString(6, tldp[9]);
pre.setString(7, tldp[8]);
pre.setString(8, tldp[4]);
pre.setInt(9, 0);
pre.setString(10, tldp[7]);
pre.setString(11, tldp[0]);
pre.addBatch();
// 分段提交    下面的i要等于txt的行数
if ((i % 500 == 0 && i != 0) || i == 4808) {
pre.executeBatch();
conn.commit();
conn.setAutoCommit(false);// 开始事务
JdbcUtil.release(null, pre, conn);
conn = JdbcUtil.getConnection(
"oracle.jdbc.driver.OracleDriver",
"jdbc:oracle:thin:@xx.xx.xx.xx:1521:tjkf",
"ypxx", "ypxx");
pre = conn.prepareStatement(sql);
System.out.println("------------>" + i);
}
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis(); // 获取结束时间
System.out.println("程序运行时间: " + (endTime - startTime) / 1000 + "秒");
}
}

public static void main(String[] args) {
String filePath = "D:\\tl3.txt";
readTxtFile(filePath);
}

}

JDBC连接数据库
package com.egf.qingbao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
* jdbc连接数据库工具类
*
* @author xh
*/
public class JdbcUtil {
private static Properties info = new Properties();

// private static InputStream is = null;
/**
* 静态代码块初始化db.properties中的数据库配置信息。
*/
// static {
// try {
// //设置系统配置资源类路径到qingbao.cfg.dir全局常量
// // ConfigurationUtils.QINGBAO_CFG_DIR =
// ConfigurationUtils.getConfigResourcePath();
// //设置db.properties文件所在类路径
// //
// if(!StringUtils.isBlank(ConfigurationUtils.QINGBAO_CFG_DIR)){//QINGBAO_CFG_DIR
// 不为空
// // is = new FileInputStream(new File(ConfigurationUtils.QINGBAO_CFG_DIR +
// ConfigurationUtils.FILE_SEPARATOR +
// ConfigurationUtils.APP_CFG_FILE_NAME_SEARCHER +
// ConfigurationUtils.FILE_SEPARATOR + "db.properties"));//
// JdbcUtil.class.getResourceAsStream();
// // }else{//默认
// is = JdbcUtil.class.getResourceAsStream("/db.properties");
// // }
// info.load(is);
// is.close();
// } catch (Exception e) {
// throw new ExceptionInInitializerError(e);
// }finally{
// try {
// is.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }

/**
* 获取数据库连接
*
* @return
* @throws Exception
*/
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(info.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("加载oracle驱动,初始化db.properties配置文件出错。");
}
try {
conn = DriverManager.getConnection(info.getProperty("url"), info
.getProperty("username"), info.getProperty("password"));
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(
"创建数据库连接失败,请检查db.properties配置文件,并查看数据库用户名密码是否配置正确。");
}
return conn;
}

/**
* 根据参数获取数据库连接
*
* @param driver
* @param url
* @param username
* @param password
* @return
*/
public static Connection getConnection(String driver, String url,
String username, String password) {
Connection conn = null;
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("加载oracle驱动出错。");
}
try {
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("创建数据库连接失败,请检查参数是否配置正确。");
}
return conn;
}

/**
* 释放资源,释放数据库连接
*
* @param rs
* @param stm
* @param conn
*/
public static void release(ResultSet rs, Statement stm, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("关闭ResultSet出错");
}
}
if (stm != null) {
try {
stm.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("关闭Statement出错");
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("关闭Connection出错");
}
}
}

/**
* main方法测试
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
System.out.println(getConnection());
System.out.println("==== 数据库连接成功 ====");
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics