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