- 浏览: 38757 次
- 性别:
- 来自: 北京
文章分类
最新评论
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("==== 数据库连接成功 ====");
}
}
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("==== 数据库连接成功 ====");
}
}
发表评论
-
java/js 时间
2016-03-14 10:54 0/** * 根据传入时间获取上月月份 */ publ ... -
杂七杂八1
2016-03-14 10:51 0public ResponseModel execute(Re ... -
《转》fastjson生成和解析json数据,序列化和反序列化数据
2015-09-29 15:17 1779fastjson生成和解析json数据,序列化和反序列化数据 ... -
fastjson和数组,list,map之间转换
2015-09-29 14:36 4558package fastJson.test; imp ... -
java unicode编码转换才成汉字
2014-11-05 15:40 1684package com.bky.controller; p ... -
遍历Map的四种方法
2014-08-06 11:52 0public static void main(String[ ... -
java中URLEncoder.encode与URLDecoder.docode的使用
2014-08-01 10:09 0在http请求时,传递参数的时候,如果有中文,那么可以先转码再 ... -
URL地址中获取参数和值
2014-07-10 10:09 1201/* * Copyright 2012 The EGF ... -
深入java异常处理机制--深受启发(转)
2014-04-03 15:14 580(转载自:http://www.blogjava.net/fr ... -
java开发中各种异常
2014-04-03 14:33 545作为一名开发者,Ja ... -
java的Timer定时任务调度
2014-02-19 10:25 496//第一个类:监听 import java.util.Time ... -
超链接点击后不跳转
2014-01-27 15:16 1474让超链接点击后不跳转,可以用href = "#&q ... -
dom4j生成和解析xml文件
2014-01-27 15:17 633package com.test; import ... -
jdom生成和解析xml文件
2014-01-27 15:18 546package com.test; import ... -
java生成xml文件
2014-01-15 17:03 442/* * Copyright 2013 The EGF ... -
java实现excel导入功能
2014-01-15 16:53 935方法一 /** *使用Hibernate框架导入 * @thr ... -
String类常用的方法
2013-04-23 12:02 594java中String的常用方法 1、length() 字符 ...
相关推荐
java读取excel数据导入数据库源码 java读取excel数据导入数据库源码
批量读取txt文件导入到数据库中,批量读取txt文件导入到数据库中 批量读取txt文件导入到数据库中,批量读取txt文件导入到数据库中
java 读取txt文本文件中的数据并保存到数据库中源代码,假设txt已有格式,并以","分隔。其中的sql包需要自己去微软官网下载。
shp文件:地理信息系统,也被称作GIS,它主要的扩展类型是SHAPEFILE (.SHP),一个包含了矢量地理空间数据的流行文件格式,描述了几何形态,点,线和多边形...该方法通过java代码实现将shp文件的数据读取以及存入数据库
NULL 博文链接:https://qq-24665727.iteye.com/blog/2339850
在生产企业中,会涉及到许多诸如把外部文件内容导入到数据库。本资源采用java程序开发,文本文件内容里的字段用“,”分开(如果是其他标识符,相应修改一下就行了),方便快捷。
xml文件导入到mysql数据库中,包括文件:ConnectionUtils.java,InsertDT.java,db_mysql.properties,test01.xml,需要的jar包dom4j-1.6.1.jar,mysql-connection-java-3.1.1.3-bin.jar,jaxen-1.1-beta-7.jar
定时读取PDF文件并且批量插入38W条数到数据库,里面包含线程定时执行线程,事务批量插入,读取配置文件工具类,数据库连接工具类,日期工具类,一个小的项目。
2、java读取数据库数据,并导出为excel文件。 3、README.md中有详细的操作步骤示例。 使用说明: 1. 先使用postman导入:other/excel相关.postman_collection.json 2. 导入sql到数据库:other/excel.sql 3. idea导入...
工作中有很多数据以txt方式存储,需要导入oracle数据库处理(方便进行计算、统计),因文件较多,不便于手工处理,所以编写此程序以便进行数据导入。已经测试过,可以将数据导入oracle。
java读取excl文件内容进行mysql自动创建表,实现了从手动创建表到自动一键式创建数据库表,大大节省了开发时间,便于程序猿进行项目开发
从Excel中读取数据导入到数据库中,Excel中有合并单元格的列,也有超过4000字符的数据列。
java excel 读写 java excel 导入数据库 部分来自网络,现整理供同行们使用
这是一个用java编译的将数据库的记录导出为csv文件的程序
用java语言实现对word表格操作,实现自动批量往数据库中插入数据,节省插入数据时间。
主要为大家详细介绍了java读取cvs文件并导入数据库,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java 读取纯真IP数据库 最新数据库 下载后导入到eclipse 可直接运行
Java、SpringBoot实现对Excel内容的读取并将Excel内容导入到数据库中(后端篇)
教会你怎么把excel的数据导入数据库,请按照上面的例子自己做实验。
该代码可以处理100万数据量的excel文件,xlsx文件数据量太大,用普通的读法会报内存溢出错误,所以用官网提供的方法,一条一条的读取大excel文件,本例子从这点出发,组装excel里读取的单条数据为list,在根据需求...