当前位置:Gxlcms > 数据库问题 > java 每日习题(六)从文件收集数据记录到mysql

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

时间:2021-07-01 10:21:17 帮助过:1人阅读

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

标签:

人气教程排行