A self-compiled program reads log files in XML format and writes data to the database.

Source: Internet
Author: User
Tags informix sybase

[1] program description: The following describes a Java class used.

Northking. java. This is the entry class of the program.

Xmltodb. Java is a class for processing data written from XML to the database.

Analysisxml. Java: the class for analyzing XML files

Worklog. Java: the JavaBean class of XML log files.

Dbconnection. Java this is the data connection code class

[2] detailed program description: provides specific code for each class for your reference.

Northking. Java

Package src.net. NK. TC;

Public class northking {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub

Xmltodb = new xmltodb ();

Xmltodb. doxmltodb_worklog ();

Xmltodb. doxmltodb_ipaddress ();
}
 
}

Xmltodb. Java

Package src.net. NK. TC;

Import java. SQL. connection;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. util. stringtokenizer;
Import java. util. RegEx .*;

Public class xmltodb {
 
Private dbconnection dBc;
Private analysisxml ax;
Private worklog Wl;
Private connection xmlcon;
Private statement st;




 
Public xmltodb ()
{

DBC = new dbconnection ();
DBC. getproperties ();
Xmlcon = DBC. getconnection ();
Ax = new analysisxml ();
Ax. xmldata ("/worklog. xml ");
WL = new worklog ();
}
 
 
 
Public void doxmltodb_worklog () // write data to the worklog table
{

Try {

St = xmlcon. createstatement ();

For (INT I = 0; I <ax. xmlvector. Size (); I ++)
{
WL = (worklog) ax. xmlvector. Get (I );

Xmlcon. setautocommit (false );



St.exe cuteupdate ("insert into worklog values ('" + WL. getcode () + "','" + WL. getname () + "','" + WL. getlog () + "')");

Xmlcon. Commit ();

/* String temp = WL. getcode () + "|" + WL. getname () + "|" + WL. getlog ();
System. Out. println (temp );*/
}


} Catch (exception e ){
// Todo auto-generated Catch Block
Try {
Xmlcon. rollback ();
If (st! = NULL)
St. Close ();
} Catch (sqlexception E1 ){
// Todo auto-generated Catch Block
E1.printstacktrace ();
}
E. printstacktrace ();
}


}
 
Public void doxmltodb_ipaddress () // write data to the IPaddress table
{

Try {

St = xmlcon. createstatement ();

For (INT I = 0; I <ax. xmlvector. Size (); I ++)
{
WL = (worklog) ax. xmlvector. Get (I );

String [] ipstring = findallip (WL. getlog (). Split ("");

// Stringtokenizer STN = new stringtokenizer (findallip (WL. getlog ()),"");

Xmlcon. setautocommit (false );

For (Int J = 0; j <ipstring. length; j ++)
{
St.exe cuteupdate ("insert into IPaddress values ('" + WL. getcode () + "', '" + ipstring [J] + "')");
}


/* While (STN. hasmoretokens ())
{
System. Out. println (STN. nexttoken ());
St.exe cuteupdate ("insert into IPaddress values ('" + WL. getcode () + "', '" + STN. nexttoken () + "')");
}*/

Xmlcon. Commit ();


}


} Catch (exception e ){

Try {
Xmlcon. rollback ();
If (st! = NULL)
St. Close ();

} Catch (sqlexception E1 ){

E1.printstacktrace ();
}
E. printstacktrace ();
}


}
 
Public String findallip (string SRC) {// read all IP addresses from a row and return strings separated by spaces.
String ippattern = "[// D] {0, 3 }//. [// D] {0, 3 }//. [// D] {0, 3 }//. [// D] {0, 3 }";
String timepattern = "[// D] {4 }//. [// D] {2 }//. [// D] {2 }//. [// D] {2 }//. [// D] {2 }";
String temp = "";

Stringbuffer sb = new stringbuffer ();
SB. append (SRC );

Matcher timematcher = pattern. Compile (timepattern). matcher (SRC );
While (timematcher. Find ()){
Temp = timematcher. group (0 );
Int J = sb. indexof (temp );

If (j> = 0 ){
For (INT x = 0; x <16; X ++ ){
SB. deletecharat (j );
}
}
}

Src = sb. tostring ();
Matcher ipmather = pattern. Compile (ippattern). matcher (SRC );
Stringbuffer result = new stringbuffer ();

While (ipmather. Find ()){
Result. append (ipmather. group (0) + "");
}

// System. Out. println (result );

Return result. tostring ();
}
 

}

Analysisxml. Java

Package src.net. NK. TC;

Import java. Io .*;
Import java. util .*;
Import org. JDOM .*;
Import org. JDOM. Output .*;
Import org. JDOM. Input .*;

Public class analysisxml {
 
 
Public vector xmlvector = NULL;

 
 
Public list xmldata (string path)
{
Xmlvector = new vector ();

Inputstream Fi = NULL;
Try {

FI = getclass (). getresourceasstream (PATH );

Xmlvector = new vector ();
Saxbuilder sb = new saxbuilder ();
Document Doc = sb. Build (FI );

Element root = Doc. getrootelement (); // obtain the root element.
List logs = root. getchildren (); // obtain the set of all child elements of the root element.
Element log = NULL;
Worklog xml = NULL;
For (INT Index = 0; index <logs. Size (); index ++)
{
Xml = new worklog ();
Log = (element) logs. Get (INDEX );

XML. setcode (log. getattributevalue ("Code "));
XML. setname (log. getattributevalue ("name "));
XML. setlog (log. getvalue ());

Xmlvector. Add (XML );
}
}
Catch (exception X)
{
System. Out. println (X. tostring ());
}
Finally {
Try {
Fi. Close ();
}
Catch (exception e ){
E. printstacktrace ();
}
}
Return xmlvector;


}
 

}

Worklog. Java

Package src.net. NK. TC;

Public class worklog {
 
Private string code;
Private string name;
Private string log;
 
Public String getcode ()
{
Return this. Code;
}
 
Public String getname ()
{
Return this. Name;
}
 
Public String getlog ()
{
Return this. log;
}
 
Public void setcode (string hPa)
{
This. Code = hPa;
}
 
Public void setname (string hPa)
{
This. Name = hPa;
}
 
Public void setlog (string hPa)
{
This. log = hPa;
}
 
 

}

Dbconnection. Java

Package src.net. NK. TC;

Import java. SQL .*;
Import java. util .*;
Import java. Io .*;

Public class dbconnection {
 
Private Properties pS;
 
Private string driver = NULL;
Private string url = NULL;
Private string username = NULL;
Private string Password = NULL;
 
Private connection con = NULL;
 
 
Public void getproperties ()
{
PS = new properties ();
Inputstream is = getclass (). getresourceasstream ("/dbconnect. properties ");
Try {
PS. Load (is );

}
Catch (exception E)
{
System. Out. println (E. tostring ());
}

Driver = ps. getproperty ("dbdriver ");
Url = ps. getproperty ("dburl ");
Username = ps. getproperty ("dbusername ");
Password = ps. getproperty ("dbuserpwd ");


}
 
Public connection getconnection ()
{
Try {
Class. forname (this. Driver );
Con = drivermanager. getconnection (this. url, this. username, this. Password );

Return con;

} Catch (exception e ){
// Todo auto-generated Catch Block

E. printstacktrace ();
Return con;
}

}
 

}

[3] related (configuration) files

Dbconnect. Properties Database Connection configuration file

Worklog. xml Log File

The details are as follows:

[Dbconnec. properties]

# MySQL #
# Dbdriver = com. MySQL. JDBC. Driver
# Dburl = JDBC: mysql: // 192.168.0.66/test
# Dbusername = root
# Dbuserpwd = Password

# Com. Borland. datastore. JDBC. datastoredriver #
# Dbdriver = com. Borland. datastore. JDBC. datastoredriver
# Dburl = JDBC: BORLAND: dslocal: northking. JDS
# Dbusername = northking
# Dbuserpwd = northking

# Oracle #
# Dbdriver = oracle. JDBC. Driver. oracledriver
# Dburl = JDBC: oracle: thin: @ 192.168.0.66: 1521: Test
# Dbusername = sys
# Dbuserpwd = Password
#

# SQL strongswan server #
# Dbdriver = com. Microsoft. JDBC. sqlserver. sqlserverdriver
# Dburl = JDBC: Microsoft: sqlserver: // 192.168.0.66: 1433; databasename = test
# Dbusername = sa
# Dbuserpwd = Password
#

# Sybase #
# Dbdriver = com. Sybase. jdbc2.jdbc. sybdriver
# Dburl = JDBC: Sybase: TDS: 192.168.0.66: 2638? Servicename = test
# Dbusername = sa
# Dbuserpwd = Password
#

# DB2 #
# Dbdriver = com.ibm.db2.jdbc.net. db2driver
# Dburl = JDBC: DB2: // 192.168.0.66: 6789/test
# Dbusername = db2inst1
# Dbuserpwd = Password
#

# Informix #
# Dbdriver = com. Informix. JDBC. ifxdriver
# Dburl = JDBC: Informix-sqli: // 123.45.67.89: 1533/mydb: informixserver = myserver
# Dbusername = db2inst1
# Dbuserpwd = Password
#

# Access #
Dbdriver = sun. JDBC. ODBC. jdbcodbcdriver
Dburl = JDBC: ODBC: Driver = {Microsoft Access Driver (*. mdb)}; DBQ = C: // data. MDB
Dbusername = Admin
Dbuserpwd = Password
#

----------------------------------------

[Worklog. xml]

<? XML version = "1.0" encoding = "UTF-8"?>
<Work_logs>
<Log Code = "100000101" name = ""> 190020.1.01.01 at 148.74.77.198. disable web application 4.194.208.181. fix database 12.106.220.202. backup database 95.245.222.14. debug log 242.93.134.196. modify log14.151.120.76. modify the interface </log>
<Log Code = "100000102" name = ""> 190020.1.02.01.01 at 67.71.212.210. Back up the database 150.251.232.58. debug the log </log>
<Log Code = "100000103" name = ""> 190020.1.03.01.01 at 62.136.27.255. modify the page 172.4.147.26. assign task 26.204.73.12. force end this program 75.97.77.242.ocr to start 250.102.71.41. open the first batch 42.165.105.105. open the scanner 182.83.161.53. scan the accounting file 47.149.21.117. quality Check 231.67.200.97. open the computer </log>
<Log Code = "100000104" name = "Yang Li"> 190020.1.04.01.01 at 161.160.109.25. no action 143.177.233.213. copy Data 6.139.234.110. set administrator permissions to 173.128.131.118. start the game 90.103.69.14. write documentation 62.250.16.100. developer 93.125.40.121. disable the service 66.160.138.40. disable the computer 239.167.129.100. disable the Web application 27.200.117.192. fix the database 152.212.5.54. backup database 19.27.85.33. debug log 0.137.168.139. modify log211.21.49.173. modify the page 116.91.38.180. assign task 237.135.23.193. forcibly terminate this program 102.118.126.53.ocr start 84.24.150.43. open the first batch 237.25.113.70. open the scanner 222.191.70.172. scan the accounting file 65.23.8.82. quality Check </log>
<Log Code = "100000105" name = ""> 190020.1.05.01.01 at 60.123.65.87. no action 11.123.53.220. copy materials 150.193.237.84. set the administrator privilege 138.21.162.105. start the game 245.29.235.158. write the document 89.38.122.56. development Program 253.33.7.88. disable the service 140.235.137.104. disable the computer 216.211.171.184. disable the Web application 254.87.126.105. fix the database 177.46.108.139. backup database 208.138.109.130. debug log 58.158.125.174. modify log226.94.77.75. modify the page 38.135.57.149. assign task 93.223.122.49. forcibly end the Program </log>
<Log Code = "100000106" name = ""> 190020.1.06.01.01 in 157.199.41.206. Modify log52.244.17.16. modify the page 202.97.118.30. assign a task </log>
<Log Code = "100000107" name = ""> 190020.1.07.01.01 in 12.182.136.143. Start the service 55.119.190.192. Start the workflow </log>
<Log Code = "100000108" name = ""> 190020.1.08.01.01 in 190.23.162.182. start the game 4.110.103.70. write 251.205.178.106. development Program 187.71.91.111. disable the service 241.31.165.89. disable computer 27.47.20.110. disable the Web application 245.138.126.248. fix database 56.97.101.72. backup database 128.35.132.246. debug log 142.23385112. modify log177.16.193.179. modify the page 127.40.57.83. assign task 200.13.132.193. forcibly terminate this program 204.83.248.205.ocr to start 172.231.223.106. open the first batch 31.243.203.178. open the scanner 102.4.43.42. scan accounting records </log>
</Work_logs>

 

PS: The program structure can be analyzed by yourself. I will not analyze it and ask my friends to share it with me.

 

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.