0

I wrote a simple program to execute a particularly large SQL file on a mysql database . The program is working fine, but because the SQL has large amount of data , the log generated is very large and unreadable. Is there some way to make jdbc print only errors or make it write the log into a file .

5
  • In catch() block, you can create if conditions and accordingly you can insert those logs wherever you want Commented Aug 10, 2017 at 7:31
  • read this stackoverflow.com/questions/15758685/… Commented Aug 10, 2017 at 7:32
  • How do you generate the log currently? Normally, you use a logging framework for this kind of stuff, which allows you to simply configure which logging messages go where. Commented Aug 10, 2017 at 7:32
  • are you using log4j? Commented Aug 10, 2017 at 7:34
  • @sForSujit no I am not using log4j or anyother logger . All logs are written to console . Commented Aug 10, 2017 at 8:14

1 Answer 1

2

You can use any Logger, like Log4j and log in Catch statement:

Steps to follow:
Download latest log4j distribution.
Add log4j’s jar library into your program’s classpath.
Create log4j’s configuration.
Initialize log4j with the configuration.
Create a logger.
Put logging statements into your code.

 Logger logger = Logger.getLogger(MyClass.class);

try {
      // Your risky code goes between these curly braces!!!
 }
 catch(Exception ex) {
    // Log your errors here
       logger.info("This is my first log4j's statement");

}
 finally {
      // Your must-always-be-executed code goes between these 
     // curly braces. Like closing database connection.
  }

`

Ref:http://www.codejava.net/coding/how-to-configure-log4j-as-logging-mechanism-in-java

Sign up to request clarification or add additional context in comments.

3 Comments

Ya I think log4j is the way to go . I'll give it a try .
@born let me know you exact need so that i can modify code accordingly , meanwhile this will give you a fair idea about logging SQL errors
Sorry for replying late . Followed your Idea . Got it working thanks . @mohit sharma

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.