实现读取文本数据,在将数据导入mysql
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; /**
* ClassName:DBlUtils <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2015年9月14日 下午3:19:51 <br/>
*
* @author
* @version
* @since JDK 1.7.79
*/
public class DBlUtils { public static String url = null;
public static String username = null;
public static String password = null;
public static Connection conn;
public static Statement stmt;
public static ResultSet rs;
public static String fileName = null; public static String PATH = "/dbconfig.properties";
private static Properties properties;
static {
try {
InputStream is = DBlUtils.class.getResourceAsStream(PATH);
properties = new Properties();
properties.load(is);
url = properties.getProperty("jdbc.url");
username = properties.getProperty("jdbc.username");
password = properties.getProperty("jdbc.password");
fileName = properties.getProperty("fileName");
System.out.println("fileName:" + fileName);
if (is != null)
is.close();
} catch (IOException e) { e.printStackTrace();
} } public void closeConnection(Connection conn) { if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void insertData(String sql) {
try { conn = DriverManager.getConnection(url, username, password);
conn.setAutoCommit(false);
stmt = conn.prepareStatement("load data local infile '' " + "into table loadtest fields terminated by ','");
StringBuilder sb = new StringBuilder();
InputStream is = new ByteArrayInputStream(sb.toString().getBytes());
((com.mysql.jdbc.Statement) stmt).setLocalInfileInputStream(is);
stmt.executeUpdate(sql);
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
} public static String readFileByLines(String fileName) {
File file = new File(fileName);
String tempString = null;
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
String sql = ("insert into tablename(title, feel, state, num, anum, updatetime, createtime) values ('" + tempString + "',0, 0, 0, 0, NOW(), NOW())");
insertData(sql);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return tempString;
} /**
* 实现数据增量插入
*
* @param fileName
* 文件名称
* @param str
* 比较关键字
* @return
*/
public static String increaseInsertData(String fileName, String str) {
File file = new File(fileName);
String tempString = null;
BufferedReader reader = null;
List lists = new ArrayList();
try {
reader = new BufferedReader(new FileReader(file));
int line = 1;
while ((tempString = reader.readLine()) != null) {
lists.add(tempString);
}
// 取出mysql里面的数据
String sqlstr = null;
String sql = ("");
// 判断是否包含mysql 数据库
if (!lists.contains(sqlstr)) {
// TODO 不包含将数据添加到mysql 中 } else {
// 包含 不添加 } } catch (Exception e) {
e.printStackTrace();
}
return null;
} public static void main(String[] args) { // String filePath = "E://zongyi.txt";
String readFileByLines = readFileByLines(fileName);
String str = ("insert into table(title, feel, state, snum, anum, updatetime, createtime) values (readFileByLines,0, 0, 0, 0, NOW(), NOW())"); insertData(readFileByLines);
System.out.println(readFileByLines);
System.out.println("添加数据成功。。。。"); }
}
配置文件信息:在resources 目录下: dbconfig.properties
jdbc.url=jdbc\:mysql\://ip\:3306/databases?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=username
jdbc.password=password
jdbc.dbType=mysql
# filePath
fileName =E://xx.txt
fileName=E\://words.txt
实现读取文本数据,在将数据导入mysql的更多相关文章
- 使用python读取文本中结构化数据
需求 read some .txt file in dir and find min and max num in file. solution: echo *.txt > file.name ...
- .net core利用MySqlBulkLoader大数据批量导入MySQL
最近用core写了一个数据迁移小工具,从SQLServer读取数据,加工后导入MySQL,由于数据量太过庞大,数据表都过百万,常用的dapper已经无法满足.三大数据库都有自己的大数据批量导入数据的方 ...
- 将access数据库导入mysql
一般地,直接在mysql端,导入时选择access文件就行:但是若access数据库版本太老,导入mysql时会出错: 这时,就需要借助access 2003,对原始数据进行转换为2003版本数据,即 ...
- MySQL中游标使用以及读取文本数据
原文:MySQL中游标使用以及读取文本数据 前言 之前一直没有接触数据库的学习,只是本科时候修了一本数据库基本知识的课.当时只对C++感兴趣,天真的认为其它的课都没有用,数据库也是半懂不懂,胡乱就考试 ...
- (转)php读取文件使用redis的pipeline导入大批量数据
第一次写博客,哈哈,纯属用来记录一下自己工作中遇到的问题及解决办法. 昨天因为工作的需求,需要做一个后台上传TXT文件,读取其中的内容,然后导入redis库中.要求速度快,并且支持至少10W以上的数据 ...
- Python实现随机读取文本N行数据
工作中需要判断某个文本中的URL是否能正常访问,并且随机获取其中N行能正常访问的URL数据,我的思路是:读取文本每一行数据,用urlopen访问,将返回状态码为200的URL保存到一个列表,获得列表长 ...
- 第一课 导入库 - 创建数据集 - CSV读取 - 导出 - 查找最大值 - 绘制数据
第1课 创建数据 - 我们从创建自己的数据集开始分析.这可以防止阅读本教程的最终用户为得到下面的结果而不得不下载许多文件.我们将把这个数据集导出到一个文本文件中,这样您就可以获得从文本文件中一些拉取数 ...
- .Net读取Excel文件时丢失数据的问题 (转载)
相信很多人都试过通过OleDB读取Excel文件,这种方法效率十分高,只是有一点会让人十分头痛,就是当一列中既有混合型数据,又有纯数据时,往往容易丢失数据. 百度过后,改连接字符串 “HDR=YES; ...
- 600万用户数据导入MYSQL、MSSQL、Oracle数据库方法【转】
1.导入MySql数据库 参考文献:http://zhuaxia.org/blog/post/145 1.1.LOAD DATA INFILE语法 因为获得的数据库文件是一个文本文件www.csd ...
- Excel连接到MySQL,将Excel数据导入MySql,MySQL for Excel,,
Excel连接到MySQL 即使当今时代我们拥有了类似微软水晶报表之类的强大报表工具和其他一些灵活的客户管 理应用工具,众多企业在分析诸如销售统计和收入信息的时候,微软的Excel依然是最常用的工具. ...
随机推荐
- fiddler对浏览器、app抓包及证书安装
1.fiddler对浏览器抓包 1.1 对浏览器的http的抓包 Capturing开启,进行抓包: Capturing关闭,停止抓包: 如下图: 1.2 对浏览器的https抓包 1.2.1 开启 ...
- Quartz特点
运行环境 Quartz 可以运行嵌入在另一个独立式应用程序 Quartz 可以在应用程序服务器(或servlet容器)内被实例化,并且参与XA事务 Quartz 可以作为一个独立的程序运行(其自己的J ...
- 总分 Score Inflation
题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类& ...
- docker容器和宿主机时间不一致的问题
第1种:复制宿主机的localtime文件,到容器里docker cp /etc/localtime threg:/etc/ 注:这里 threg为容器名称,复制完后需重启容器 第2种在构建docke ...
- 基于用户的协同过滤(UserCF)
- Linux 父子进程实现复制文件内容到另一个文件内
1. 子进程先拷贝前一半 ,父进程一直阻塞到子进程拷贝完再拷贝后一半 /* 子进程先拷贝前一半文件,父进程先阻塞等待子进程拷贝完前一半内容, * 然后父进程在拷贝,后一半内容 * */ #includ ...
- loop find column
declare l_sql varchar2(500); -- variable that contains a query l_c sys_refcursor; -- cursor variable ...
- 创建maven项目的时候:Could not resolve archetype org.apache.maven.archetypes:maven-archetype-webapp:1.0 from any of the configured repositories. 解决办法
问题: https://yq.aliyun.com/ziliao/364921 尝试没成功. https://www.aliyun.com/jiaocheng/296712.html 尝 ...
- 论文阅读笔记:《Generating Question-Answer Hierarchies》
题目: <Generating Question-Answer Hierarchies> 作者: Kalpesh Krishna & Mohit Iyyer What: 1.SQU ...
- 改变this 指向的3种方法
1.在函数内部声明一个that,然后将this赋值给that, var that=this; 最后用that 代替this使用 <!DOCTYPE html> <html lang= ...