7 Replies - 1625 Views - Last Post: 25 August 2010 - 06:32 AM Rate Topic: -----

#1 tedy2808   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 10

Log message from command prompt

Posted 24 August 2010 - 02:12 AM

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
  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..
  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.

  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.

Is This A Good Question/Topic? 0
  • +

Replies To: Log message from command prompt

#2 Luckless   User is offline

  • </luck>
  • member icon

Reputation: 294
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Log message from command prompt

Posted 24 August 2010 - 05:54 AM

Never used a Logger and don't hoestly know if it makes a difference, but i know you could use a Buffered Writer to write each line to a .txt file as it is printed to the console. Here is a link to an example:
Was This Post Helpful? 0
  • +
  • -

#3 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Log message from command prompt

Posted 24 August 2010 - 06:54 AM

I really do not understand what is your problem. I do the same thing as you in order to initialize the Logger and after that I just write to the Logger using:
logger.info("Some information...");



Whenever I want save util information about the execution.

The thing that I do not understand is when you say "how to capture the entire message that had been printed out in the command prompt".
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Log message from command prompt

Posted 24 August 2010 - 04:56 PM

Never though of doing
for(int i = 1; i <= 19; i++) 
  state_ment.setString(i, result.getString(i));


Was This Post Helpful? 0
  • +
  • -

#5 tedy2808   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 10

Re: Log message from command prompt

Posted 24 August 2010 - 07:28 PM

Hi all,
I realized that there is other ways to log the info.
just wanna add a bit information so that u guys get a clear understanding on this problem..
Based on the above code,the first code i had posted, when i want to run the script, i type ./run.sh >> /home/java/enhancement/MyLog.log ... Thus, it will output the result in MyLog.log..
The result printed out in the log as i desired.But if there are error in database, lets say there are one attribute that not exist in the table, it will printed error message in the command prompt ONLY. But in the log file, it will print the same message as the process successful.

Below are the sample of output in MyLog.log if there is no problem in the process.

  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!


let say i deleted one of the attribute from one of the table in the the script that makes the database send error message during the process, i got the error msg in command prompt like this:
  Exception in thread "main" java.sql.SQLException: ORA-00913: too many values

        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:330)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:287)
        at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:742)
        at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:212)
        at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:951)
        at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1159)
        at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3284)
        at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3367)
        at production_java.<init>(production_java.java:104)
        at production_java.main(production_java.java:137)


and at the same time the message in log will still be printed out but it will be the same message like if it is successful.

  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, i need the error message to be captured together in the log file as it will be easy for me to identify the actual error occur during the process by referring to the log.

Any idea?
Thanks in advance..Really appreciate your help.
Was This Post Helpful? 0
  • +
  • -

#6 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Log message from command prompt

Posted 24 August 2010 - 07:39 PM

You must launch your programa like this:
$JAVA_HOME/bin/java -outfile $arch_out -errfile $arch_err $clase



Where $arch_out is the path to the log file where all the out.print will be written and $arch_err is the path to the file where every exception will be
written.
Was This Post Helpful? 0
  • +
  • -

#7 tedy2808   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 10

Re: Log message from command prompt

Posted 24 August 2010 - 08:45 PM

View Postguidojavier, on 24 August 2010 - 06:39 PM, said:

You must launch your programa like this:
$JAVA_HOME/bin/java -outfile $arch_out -errfile $arch_err $clase



Where $arch_out is the path to the log file where all the out.print will be written and $arch_err is the path to the file where every exception will be
written.



hi guidojavier,
thanks for the rep..
But sorry, i don't understand what do you meant by "launch the program". Do i have to add the line given by you in my script??
Can you guide me and explain briefly where to add and when should i add the line given by you..
thanks in advanced
Was This Post Helpful? 0
  • +
  • -

#8 guido-granobles   User is offline

  • D.I.C Addict
  • member icon

Reputation: 171
  • View blog
  • Posts: 617
  • Joined: 02-December 09

Re: Log message from command prompt

Posted 25 August 2010 - 06:32 AM

I fix my earlier post. I said that you should use -outfile and -errfile but these options are for JSVC which is intended for run programs as daemons in Java. So the real thing you need to do is:

In the file run.sh I suppose there is a line which call java. So that line would be like this:
[code]
$JAVA_HOME/bin/java [clase name] 1>[path to out file] 2>[path to err file]
[/codee]

So 1>[name file] save all the out.print to the file and 2>[name file] will do the same.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1