1. mysql build library, build table
2. Preparing the data file 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. Collect data logs to MySQL
Description of the class to use:
3.1 file gets a handle to the document that represents a local file.
3.2 FileInputStream is meant for reading streams of raw bytes such as image data.
3.3 An InputStreamReader are a bridge from byte streams to character Streams:it reads bytes and decodes them into charact ERs using a specified charset.
3.4 bufferedreader:reads text from a character-input stream, buffering characters so-provide for the efficient REA Ding of characters, arrays, and lines.
PackageCollect_part;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStreamReader; Public classCollectdata { Public Static voidMain (string[] args) {String path= "D:\\logs\\cpu.txt"; Collectdata Data=NewCollectdata (); Data.readfile (path); } Public voidreadFile (String filePath) {Infobean Infobean=NewInfobean (); Try{String encodeing= "GBK"; File File=NewFile (FilePath); if(File.isfile () &&file.exists ()) {InputStreamReader ISR=NewInputStreamReader (NewFileInputStream (file), encodeing); BufferedReader BufferedReader=NewBufferedReader (ISR); String Linetxt=NULL; Operationdb opdb=Newoperationdb (); 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 (); } }}
Create a connection
PackageCollect_part;Importjava.sql.Connection;ImportJava.sql.DriverManager;Importjava.sql.SQLException;ImportJava.util.logging.Level;ImportJava.util.logging.Logger; Public classmysqlconnection { Public StaticConnection 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); } returnConn; }}
manipulating databases
PackageCollect_part;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;ImportJava.util.logging.Level;ImportJava.util.logging.Logger; Public classoperationdb {PrivateConnection conn =NULL; Public voidAddrcorder (Infobean Infobean)throwsSQLException {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 (); }}
Record information
PackageCollect_part; Public classInfobean {LongTimeStamp; Longelapsed; Longgrpthreads; Longallthreads; Public LongGettimestamp () {returnTimeStamp; } Public voidSettimestamp (String timeStamp) { This. TimeStamp =Long.parselong (TimeStamp); } Public Longgetelapsed () {returnelapsed; } Public voidsetelapsed (String elapsed) { This. elapsed =Long.parselong (elapsed); } Public Longgetgrpthreads () {returngrpthreads; } Public voidsetgrpthreads (String grpthreads) { This. Grpthreads =Long.parselong (grpthreads); } Public Longgetallthreads () {returnallthreads; } Public voidsetallthreads (String allthreads) { This. Allthreads =Long.parselong (allthreads); } }
Java daily exercise (vi) Collect data from files to MySQL