Insert QQ chat record into the database

Source: Internet
Author: User
Tags time and date

Recently in the completion of the establishment, one of the links is to analyze the QQ chat record, before the analysis needs to first QQ chat record export, and then into the database, QQ chat record after export is a text document, export way:
1. After logging in to QQ, click on any friend to view the chat history with him and click on the small horn icon on the chat record screen.
2. After clicking on the small horn icon, you can select the chat record that you want to export, right click on the name of the person or group you want to export, select Export Chat record, export format as text document.
The exported text document format is very strong, see:

We roughly analyze this format, the top lines of descriptive text directly deleted, mainly to see the chat record part, first date, then a space, then the time, then a space, then the user name, after the line is specific content, the normal document is such a format, no problem, However, sometimes we send the QQ message has a newline character, so that our export records add some complexity, so we have to do a simple processing of this document.

First of all, let's talk about one of my overall ideas, the first line of each record, there is a space between the different information, so I want to put the body of the record in the first line, that is, the time of the line, the record body and the name of a space between, so that each row is a record, after reading a line of information You can use the string's own function split (), which is a space to split the string into an array of length 4. The array is then traversed into the database.

Based on this idea, I'm going to do the following with the document:
1. Delete all the spaces in the body of the record to avoid unnecessary hassles when using the Split function. There is no need to delete the space on the time and date line, which means you can't do it with find and replace.
2. A newline character may be present in the body, and this should be removed.

 Public  class r2db {     Public Static void Main(string[] args) {//Process The original document, the processed document is stored in 999.txt        //Before processing, remove the descriptive text from the front of the documentProcesstxt ();//Read the original document and insert the databaseReadandinsert (); }Private Static void Readandinsert() {String sql ="";Try{File File =NewFile ("F:\\test\\999.txt"); String str =NULL; BufferedReader br =NewBufferedReader (NewFileReader (file)); Qqchat Qqchat =NewQqchat (); list<qqchat> list =NewArraylist<qqchat> (); while(str = br.readline ())! =NULL) {string[] STRs = Str.split (" "); Qqchat.setqqdate (strs[0]); Qqchat.setqqtime (strs[1]); Qqchat.setqquser (strs[2]);/** * Some message content is empty, the length of the array after splitting is 3, for this message, * set it as unknown message */                if(strs.length==4) {Qqchat.setqqcontent (strs[3]); }Else{Qqchat.setqqcontent ("Unknown Message");                } list.add (Qqchat); Qqchat =NewQqchat (); } Connection con =NULL; PreparedStatement PS =NULL; con = dbutil.getconnection ();Try{ for(Qqchat q:list) {sql =INSERT INTO Qq_record values (null, ' "+ q.getqqdate () +"', '"+ q.getqqtime () +"', '"+ q.getqquser () +"', '"+ q.getqqcontent () +"');";                    PS = con.preparestatement (SQL);                Ps.executeupdate (); } System.out.println ("Insert success!" "); }Catch(SQLException e) {//If an error is encountered in the INSERT statement, then output, see where the problem is, directly resolve theSYSTEM.OUT.PRINTLN (SQL);            E.printstacktrace (); }        }Catch(IOException e)        {E.printstacktrace (); }    }Private Static void Processtxt() {Try{//The original document is named Hdjg.txtFile File =NewFile ("F:\\test\\hdjg.txt"); String str =NULL; BufferedReader br =NewBufferedReader (NewFileReader (file)); PrintWriter out =NewPrintWriter (NewFile ("F:\\test\\999.txt")); while(str = br.readline ())! =NULL) {//This regular expression is used to match 2015-02-10 16:02:50 Zhang San                //If it is exported with a person's chat record will not have to use the regular, but I want to export is a group chat, so to use regularPattern pattern = pattern. Compile ("\\d{4}\\-\\d{2}\\-\\d{2}\\s\\d{2}\\:\\d{2}\\:\\d{2}\\s.+"); Matcher Matcher = Pattern.matcher (str);if(Matcher.matches ()) {//each time the name of the output line is first output a newlineOut.println ();//Replace two spaces in the text with aOut.print (Str.replace ("  "," ")+" "); }Else{/** * Remove the space in the body of the chat, and there is a single quotation mark in the body of the text, which will cause problems when the data is inserted, so replace all the single quotes in the body with double quotes */Out.print (Str.replace (" ",""). Replace ("'","\""));            }} out.close (); System.out.println ("ok!"); }Catch(IOException e)        {E.printstacktrace (); }    }}

Dbutil.java

 Public classDbutil { Public StaticConnectiongetconnection() {String username ="Root"; String Password ="Admin"; String URL ="Jdbc:mysql://localhost:3306/qqchat"; Connection con =NULL;Try{con = drivermanager.getconnection (URL, username, password); }Catch(SQLException e)        {E.printstacktrace (); }returnCon } Public Static void Close(Connection con) {Try{if(con!=NULL) Con.close (); }Catch(SQLException e)        {E.printstacktrace (); }    } Public Static void Close(Java.sql.PreparedStatement PS) {Try{if(ps!=NULL) Ps.close (); }Catch(SQLException e)        {E.printstacktrace (); }    } Public Static void Close(ResultSet RS) {Try{if(rs!=NULL) Rs.close (); }Catch(SQLException e)        {E.printstacktrace (); }    }}

Qqchat.java

 Public classQqchat {PrivateString qqdate;PrivateString Qqtime;PrivateString Qquser;PrivateString qqcontent; PublicStringgetqqdate() {returnQqdate; } Public void setqqdate(String qqdate) { This. qqdate = qqdate; } PublicStringGetqqtime() {returnQqtime; } Public void Setqqtime(String qqtime) { This. qqtime = Qqtime; } PublicStringGetqquser() {returnQquser; } Public void Setqquser(String Qquser) { This. Qquser = Qquser; } PublicStringgetqqcontent() {returnQqcontent; } Public void setqqcontent(String qqcontent) { This. qqcontent = qqcontent; }}

There is a use of a regular expression above, if only to export the chat record with a person there is no need to use regular, if the export is a group chat for several years, the regular is very necessary.

Insert the QQ chat record into the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.