• 实现读取文本数据,在将数据导入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
  • 相关阅读:
    软考下午题具体解释---数据流图设计
    透视表提取不反复记录(4)-每一个物品的最大值
    运用python抓取博客园首页的所有数据,而且定时持续抓取新公布的内容存入mongodb中
    [经验总结]material design效果与开发总结
    cocos2d-x 3.0 回调函数
    P1334 瑞瑞的木板
    P2776 [SDOI2007]小组队列
    P1886 滑动窗口
    P1160 队列安排
    P1823 音乐会的等待
  • 原文地址:https://www.cnblogs.com/zhanggl/p/4812202.html
Copyright © 2020-2023  润新知