log message from command prompt
Hi all,
I quite new to java.Thus, i need you guys to help me in order for me to understand and able to solve this problem.i appreciate if someone guide me and explain clearly (detail) to me if you guys provide any codes or explanation.
My problem now is that, i wanted to enhance the system that currently being used in order for it to capture the message printed out during the execution process in log file.
I'm using linux environment to run java code. Below are the codes
Code:
import java.sql.*;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class production_java {
public production_java() throws Exception {
// Get connSQLServer
DriverManager.registerDriver(new com.microsoft.jdbc.sqlserver.SQLServerDriver());
Connection connSQLServer = DriverManager.getConnection("jdbc:microsoft:sqlserver://172.11.10.13:1433;databaseName=Mydb", "db123", "db123");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection connOracle = DriverManager.getConnection("jdbc:oracle:thin:@172.18.4.37:1521:ppfn11","devmnt","dev123");
String strInsert = "insert into resell_table (test_code, test_name, add_1, add_2, phone, fax, state, code_count, the_head1,"+
" the_head2, the_head3, status, postcode, code_region, company_no, contact_num, city, selling_person, selling_person_status , create_date , modify_date )" +
" values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
String strDelete = "delete from resell_table where test_code in (select test_code from (select count(*), test_code from " +
" resell_table group by test_code having count(*) > 1)) and status = 'D'";
if (connSQLServer != null)
{
System.out.println();
System.out.println("Successfully connected");
System.out.println();
// Meta data
DatabaseMetaData meta = connSQLServer.getMetaData();
System.out.println("Driver Information");
System.out.println("\tDriver Name: "+ meta.getDriverName());
System.out.println("\tDriver Version: "+ meta.getDriverVersion());
System.out.println("\nDatabase Information ");
System.out.println("\tDatabase Name: "+ meta.getDatabaseProductName());
System.out.println("\tDatabase Version: "+ meta.getDatabaseProductVersion());
// Select some data
Statement select = connSQLServer.createStatement();
ResultSet result = select.executeQuery("SELECT test_code, test_name, add_1, add_2, phone, fax, case state when '1' then 'JJ' " +
"when '2' then 'KK' when '3' then 'KN' when '4' then 'ML' " +
"when '5' then 'NS' when '6' then 'PG' " +
"when '7' then 'PG' when '8' then 'PK' when '9' then 'PL' " +
"when '10' then 'SB' when '11' then 'SW' "+
"when '12' then 'SL' when '13' then 'TG' "+
"when '14' then 'WPK' when '15' then 'WPL' "+
"when '16' then 'WPP' end as state, "+
"code_count, the_head1, "+
"the_head2, the_head3, o.status, postcode, case code_region when '34' then 'CENTRAL' "+
"when '35' then 'NORTHERN' when '36' then 'SOUTHERN' when '37' then 'EASTERN' " +
"when '38' then 'SK' when '39' then 'SB' end as code_region, "+
"company_no, contact_num, city, staff_name as selling_person, s.status as selling_person_status "+
" ,o.create_date, o.modify_date " +
"FROM COMMON.the_unit o left join common.worker s on o.the_head1 = s.staff_id and s.status <> 'D' ");
if (connOracle != null)
{
Statement ps = connOracle.createStatement();
System.out.println("Truncating resell_table in progress .......");
ps.executeUpdate("truncate table resell_table");
System.out.println("Done");
PreparedStatement state_ment = connOracle.prepareStatement(strInsert);
System.out.println("Inserting records into resell_table in progress ......");
int totalRows = 0 ;
while (result.next())
{
state_ment.setString(1, result.getString(1));
state_ment.setString(2, result.getString(2));
state_ment.setString(3, result.getString(3));
state_ment.setString(4, result.getString(4));
state_ment.setString(5, result.getString(5));
state_ment.setString(6, result.getString(6));
state_ment.setString(7, result.getString(7));
state_ment.setString(8, result.getString(8));
state_ment.setString(9, result.getString(9));
state_ment.setString(10, result.getString(10));
state_ment.setString(11, result.getString(11));
state_ment.setString(12, result.getString(12));
state_ment.setString(13, result.getString(13));
state_ment.setString(14, result.getString(14));
state_ment.setString(15, result.getString(15));
state_ment.setString(16, result.getString(16));
state_ment.setString(17, result.getString(17));
state_ment.setString(18, result.getString(18));
state_ment.setString(19, result.getString(19));
state_ment.setTimestamp(20, result.getTimestamp(20));
state_ment.setTimestamp(21, result.getTimestamp(21));
state_ment.executeUpdate();
int rows = state_ment.executeUpdate();
totalRows = totalRows + rows;
}
connOracle.commit();
System.out.println("Done");
System.out.println("Row(s) inserted: " + totalRows);
int totalRows1 = 0 ;
System.out.println("Removing duplicate records in resell_table in progress (with status = D) ......");
PreparedStatement state_ment1 = connOracle.prepareStatement(strDelete);
state_ment1.executeUpdate();
int rows1 = state_ment1.executeUpdate();
totalRows1 = totalRows1 + rows1;
connOracle.commit();
System.out.println("Done");
System.out.printf("%d row(s) deleted!", totalRows1);
System.out.println(" ");
state_ment1.close();
state_ment.close();
}
else {
System.out.println("Cannot connect to database");
}
select.close(); }
else {
System.out.println("Cannot connect to database");
}
connOracle.close();
connSQLServer.close();
}
}
public static void main (String args[]) throws Exception {
production_java pr = new production_java();
}
the codes will connect to the database and it will truncate,insert and delete certain data.But my question is,when i run the script, i will got this message..
Code:
Successfully connected
Driver Information
Driver Name: SQLServer
Driver Version: 2.2.0022
Database Information
Database Name: Microsoft SQL Server
Database Version: Microsoft SQL Server Yukon - 9.00.1399
Truncating resell_table in progress .......
Done
Inserting records into resell_table in progress ......
Done
Row(s) inserted: 94
Removing duplicate records in resell_table in progress (with status = D) ......
Done
0 row(s) deleted!
thus,based on the above message i got, i wanted to log the message in a log file in order for me to track the progress..below are the line of script that i had added but i did not managed to log all the message in log file..i've tried many times but still do not get the right results.
Code:
public static void main (String args[]) throws Exception {
production_java pr = new production_java();
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
try {
fh = new FileHandler("C:/MyLog.log", true);
logger.addHandler(fh);
logger.setLevel(Level.ALL);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logger.log(Level.debug,"MyLog");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
really need your help to guide me and teach me on how to capture the entire message that had been printed out in the command prompt.
Really appreciate any helps.Thanks in advanced.