跟踪 数据库 值 变化

log4j.properties(src root directory)

log4j.rootLogger=trace, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%p%d{yyyy-MM-dd HH: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


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;
import org.hibernate.Session;

import valueobjects.Privilege;


public class Test1 {

File file = new File("c:\\out.txt");

public void setConsoleToFile() throws IOException{
//create file to store console info
PrintStream ps = new PrintStream(file);
System.setOut(ps);
}

public void createInfoToConsole(){
System.out.println("insert into privilege values(nextval('privilege_sq'), 'StuPrivilege')");
}

/**
* get console table name
* @return "tableName1,tableName2"
* @throwsException
*/
public List<String> getTableNameFromConsole() throws Exception{
//read file and file table name
List<String> tableNames = new ArrayList<String>();
FileReader fileReader = new FileReader(file);
BufferedReader bufReader = new BufferedReader(fileReader);
String tempStore = "";
String tableName = "";
while(true){
tempStore = bufReader.readLine();
// if reach the end of the file, break
if(tempStore == null){
break;
}
// find place insert, delete, update
int place = 0;
if(tempStore.indexOf("insert") != -1){
place = tempStore.indexOf("insert");
tableName = tempStore.substring(place+10, tempStore.indexOf(" ", place+11));
//tableNames.add(tableName);
}else if (tempStore.indexOf("delete") != -1){
place = tempStore.indexOf("delete");
tableName = tempStore.substring(place+12, tempStore.indexOf(" ", place+13));
//tableNames.add(tableName);
}else if (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(Exception e){

}

//tableNames.add(tableName);
}
// find place binding
String binding = "";
if(tempStore.indexOf("binding") != -1){
binding = tempStore.substring(tempStore.indexOf("binding"));
tableNames.add(binding);
}
}
return tableNames;
}

/**
* compare the table in the dababase
* @param tableNames
* @throws Exception
*/
public void compare(List<String> tableNames) throws Exception{
if(tableNames == null || tableNames.isEmpty()){
return;
}

String username = "postgres";
String password = "welcome";
String url = "jdbc:postgresql://lo:5432/provost_suite";
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection(url, username, password);
for(int i = 0; i < tableNames.size(); i++){
//get data from database
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from " + tableNames.get(i));
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while(rs.next()){
for(int j = 1; j < columnCount + 1; j++){
System.out.print(rs.getString(j) + ",");
}
System.out.println("");
}

}
}

public void testHibernate(){
Privilege privilege = new Privilege();
privilege.setName_field("test002");

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
session.beginTransaction();
session.save(privilege);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}

}

public void testLog(){
   Log log = LogFactory.getLog(Test1.class);
   log.debug("Debug info.");
       log.info("Info info");
       log.warn("Warn info");
       log.error("Error info");
       log.fatal("Fatal info");
}

public static void main(String[] args) throws Exception {
Test1 test = new Test1();
// 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());
}
}

}

你可能感兴趣的:(java,apache,sql,Hibernate,log4j)