跟踪 数据库 值 变化

log4j.properties(srcrootdirectory)

log4j.rootLogger=trace,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

#Patterntooutputthecaller'sfilenameandlinenumber.

log4j.appender.stdout.layout.ConversionPattern=%p%d{yyyy-MM-ddHH:mm:ss}%m%n

log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

#log4j.logger.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE

log4j.logger.org.hibernate.SQL=DEBUG

#log4j.logger.org.hibernate.engine.QueryParameters=DEBUG

#log4j.logger.org.hibernate.engine.query.HQLQueryPlan=DEBUG

commons-logging.properties

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

importjava.io.BufferedReader;

importjava.io.File;

importjava.io.FileNotFoundException;

importjava.io.FileReader;

importjava.io.FileWriter;

importjava.io.IOException;

importjava.io.PrintStream;

importjava.sql.Connection;

importjava.sql.DatabaseMetaData;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.sql.ResultSetMetaData;

importjava.sql.SQLException;

importjava.sql.Statement;

importjava.util.ArrayList;

importjava.util.Arrays;

importjava.util.Collection;

importjava.util.Collections;

importjava.util.Iterator;

importjava.util.List;

importorg.apache.commons.logging.Log;

importorg.apache.commons.logging.LogFactory;

importorg.apache.commons.logging.impl.Log4JLogger;

importorg.hibernate.Session;

importvalueobjects.Privilege;

publicclassTest1{

Filefile=newFile("c:\\out.txt");

publicvoidsetConsoleToFile()throwsIOException{

//createfiletostoreconsoleinfo

PrintStreamps=newPrintStream(file);

System.setOut(ps);

}

publicvoidcreateInfoToConsole(){

System.out.println("insertintoprivilegevalues(nextval('privilege_sq'),'StuPrivilege')");

}

/**

*getconsoletablename

*@return"tableName1,tableName2"

*@throwsException

*/

publicList<String>getTableNameFromConsole()throwsException{

//readfileandfiletablename

List<String>tableNames=newArrayList<String>();

FileReaderfileReader=newFileReader(file);

BufferedReaderbufReader=newBufferedReader(fileReader);

StringtempStore="";

Stringtablename="";

while(true){

tempStore=bufReader.readLine();

//ifreachtheendofthefile,break

if(tempStore==null){

break;

}

//findplaceinsert,delete,update

intplace=0;

if(tempStore.indexOf("insert")!=-1){

place=tempStore.indexOf("insert");

tableName=tempStore.substring(place+10,tempStore.indexOf("",place+11));

//tableNames.add(tableName);

}elseif(tempStore.indexOf("delete")!=-1){

place=tempStore.indexOf("delete");

tableName=tempStore.substring(place+12,tempStore.indexOf("",place+13));

//tableNames.add(tableName);

}elseif(tempStore.indexOf("update")!=-1&&"".equals(tempStore.substring(tempStore.indexOf("update")+6,tempStore.indexOf("update")+7))){

place=tempStore.indexOf("update");

try{

tableName=tempStore.substring(place+7,tempStore.indexOf("",place+8));

}catch(Exceptione){

}

//tableNames.add(tableName);

}

//findplacebinding

Stringbinding="";

if(tempStore.indexOf("binding")!=-1){

binding=tempStore.substring(tempStore.indexOf("binding"));

tableNames.add(binding);

}

}

returntableNames;

}

/**

*comparethetableinthedababase

*@paramtableNames

*@throwsException

*/

publicvoidcompare(List<String>tableNames)throwsException{

if(tableNames==null||tableNames.isEmpty()){

return;

}

Stringusername="postgres";

Stringpassword="welcome";

Stringurl="jdbc:postgresql://lo:5432/provost_suite";

Class.forName("org.postgresql.Driver");

Connectioncon=DriverManager.getConnection(url,username,password);

for(inti=0;i<tableNames.size();i++){

//getdatafromdatabase

Statementstmt=con.createStatement();

ResultSetrs=stmt.executeQuery("select*from"+tableNames.get(i));

ResultSetMetaDatarsmd=rs.getMetaData();

intcolumnCount=rsmd.getColumnCount();

while(rs.next()){

for(intj=1;j<columnCount+1;j++){

System.out.print(rs.getString(j)+",");

}

System.out.println("");

}

}

}

publicvoidtestHibernate(){

Privilegeprivilege=newPrivilege();

privilege.setName_field("test002");

Sessionsession=HibernateUtil.getSessionFactory().getCurrentSession();

try{

session.beginTransaction();

session.save(privilege);

session.getTransaction().commit();

}catch(Exceptione){

e.printStackTrace();

session.getTransaction().rollback();

}

}

publicvoidtestLog(){

Loglog=LogFactory.getLog(Test1.class);

log.debug("Debuginfo.");

log.info("Infoinfo");

log.warn("Warninfo");

log.error("Errorinfo");

log.fatal("Fatalinfo");

}

publicstaticvoidmain(String[]args)throwsException{

Test1test=newTest1();

//test.testLog();

test.setConsoleToFile();

test.testHibernate();

//test.createInfoToConsole();

List<String>tableNames=test.getTableNameFromConsole();

Iterator<String>itTables=tableNames.iterator();

while(itTables.hasNext()){

System.err.println(itTables.next());

}

}

}

相关推荐