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

最新文章

  1. Office 365 如何使用powershell查询邮件追踪
  2. page、pageContext、servletContext的区别
  3. ios跑酷游戏源码完整版
  4. Tomcat6查看数据库的数据源信息
  5. 一头扎进EasyUI3
  6. @perproty and @synthesize
  7. 使用Python获取Linux系统的各种信息
  8. WordPress的神器
  9. JVM垃圾回收机制总结(4) :新一代的垃圾回收算法
  10. Linux下的TUN/TAP编程
  11. 常用Raspberry Pi周边传感器的使用教程
  12. 浅谈层次化的AI架构
  13. Android 开发技术流程
  14. Spark SQL源代码分析之核心流程
  15. Idea+maven+tomcat部署第一个tomcat项目
  16. nginx在代理转发地图瓦片数据中的应用
  17. Fib的奇怪定理 : gcd(F[n],F[m])=F[gcd(n,m)]
  18. Android短轮询解决方案——CountDownTimer+Handler
  19. Python_selenium二次封装selenium的几个方法
  20. 64位版本的Windows不兼容,masm无法运行解决方法

热门文章

  1. js总结体会
  2. 比较器CompareTo的使用
  3. option click事件
  4. poj 1905 图形推算+二分
  5. HTML5 Canvas知识点学习笔记
  6. pytest----fixture(1)--使用fixture执行配置及销毁逻辑
  7. 轻量级的惰性控件——ViewStub
  8. 实现简单的PHP接口,以及使用js/jquery ajax技术调用此接口
  9. CSS兼容问题资料汇集
  10. UserCF算法和ItemCF算法的对比