码迷,mamicode.com
首页 > 数据库 > 详细

java 每日习题(六)从文件收集数据记录到mysql

时间:2015-11-19 20:47:16      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

1. mysql建库,建表

2. 准备数据文件cpu.txt

1447836374319,169,0,0
1447836375346,498,0,0
1447836376346,250,0,0
1447836377347,0,0,0
1447836378347,497,0,0
1447836379347,0,0,0
1447836380347,498,0,0
1447836381348,0,0,0
1447836382348,498,0,0
1447836383348,0,0,0
1447836384348,248,0,0
1447836385348,0,0,0

3. 收集数据记录到mysql

  使用到的类说明:

  3.1  File 获取文件句柄,File实例代表一个本地文件。

  3.2  FileInputStream is meant for reading streams of raw bytes such as image data. 

  3.3  An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset.

  3.4  BufferedReader: Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

package collect_part;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class CollectData {

    public static void main(String[] args) {

        String path = "d:\\logs\\cpu.txt";
        CollectData data = new CollectData();
        data.readFile(path);
        
    }

    public void readFile(String filePath) {
        InfoBean infoBean = new InfoBean();
        try {
            String encodeing = "GBK";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) {
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encodeing);
                BufferedReader bufferedReader = new BufferedReader(isr);
                String lineTxt = null;
                OperationDB opdb = new OperationDB();
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    String[] s = lineTxt.split(",");
                    //System.out.println(s[0].toString() + s[1].toString() + s[2].toString() + s[3].toString() );
                    infoBean.setTimeStamp(s[0].toString());
                    infoBean.setElapsed(s[1].toString());
                    infoBean.setGrpThreads(s[2].toString());
                    infoBean.setAllThreads(s[3].toString());
                    opdb.addRcorder(infoBean);
                    System.out.println(lineTxt);
                }

                isr.close();

            } else {
                System.out.println("not found specific file");
            }
        } catch (Exception e) {
            System.out.println("read file content error");
            e.printStackTrace();
        }

    }
}

 

创建连接

package collect_part;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class MysqlConnection {
    public static Connection getConnection() {

        Connection conn = null;
        String url = "jdbc:mysql://10.1.1.148:3306/testresults";
        String user = "user";
        String password = "passwd";

        try {

            conn = DriverManager.getConnection(url, user, password);
        } catch (SQLException ex) {
            Logger.getLogger(MysqlConnection.class.getName()).log(Level.SEVERE,null, ex);
        }
        return conn;

    }
}

 

操作数据库

package collect_part;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OperationDB {
    private Connection conn = null;

    public void addRcorder(InfoBean infoBean) throws SQLException {
        if (conn == null) {
            conn = MysqlConnection.getConnection();
        }

        String sql = "INSERT INTO cpuInfo (`timeStamp`, `elapsed`, `grpThreads`, `allThreads`) VALUES (?, ?, ?, ?)";

        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setLong(1, infoBean.getTimeStamp());
        pstmt.setLong(2, infoBean.getElapsed());
        pstmt.setLong(3, infoBean.getGrpThreads());
        pstmt.setLong(4, infoBean.getAllThreads());

        pstmt.executeUpdate();
    }
}

 

记录信息

package collect_part;

public class InfoBean {

    long timeStamp;
    long elapsed;
    long grpThreads;
    long allThreads;
    
    public long getTimeStamp() {
        return timeStamp;
    }
    public void setTimeStamp(String timeStamp) {
        this.timeStamp = Long.parseLong(timeStamp);
    }
    public long getElapsed() {
        return elapsed;
    }
    public void setElapsed(String elapsed) {
        this.elapsed = Long.parseLong(elapsed);
    }
    public long getGrpThreads() {
        return grpThreads;
    }
    public void setGrpThreads(String grpThreads) {
        this.grpThreads = Long.parseLong(grpThreads);
    }
    public long getAllThreads() {
        return allThreads;
    }
    public void setAllThreads(String allThreads) {
        this.allThreads = Long.parseLong(allThreads);
    }

    
    
}

 

java 每日习题(六)从文件收集数据记录到mysql

标签:

原文地址:http://www.cnblogs.com/mtlogs/p/4978575.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!