DriverManager: setLogWriter(PrintWriter out) : DriverManager « java.sql « Java by API






DriverManager: setLogWriter(PrintWriter out)

 
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.SQLWarning;

public class Main {

  public static void main(String[] args) throws Exception {
    String dbURL = "jdbc:odbc:Companies";
    try {
      // Load the jdbc-odbc bridge driver
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      // Enable logging
      DriverManager.setLogWriter(new PrintWriter((System.err)));

      System.out.println("Getting Connection");
      Connection conn = DriverManager.getConnection(dbURL, "user", "password");

      SQLWarning warn = conn.getWarnings();
      while (warn != null) {
        System.out.println("SQLState: " + warn.getSQLState());
        System.out.println("Message:  " + warn.getMessage());
        System.out.println("Vendor:   " + warn.getErrorCode());
        System.out.println("");
        warn = warn.getNextWarning();
      }

      conn.close();
    } catch (ClassNotFoundException e) {
      System.out.println("Can't load driver " + e);
    } catch (SQLException e) {
      System.out.println("Database access failed " + e);
    }
  }
}

           
         
  








Related examples in the same category

1.DriverManager: getConnection(String url) (jdbc:odbc:technical_library)
2.DriverManager: getConnection(String url, String user, String password)
3.DriverManager: getConnection(String url, Properties info)
4.DriverManager: getDrivers()
5.DriverManager: registerDriver(Driver driver)