Java by API/java.sql/SQLWarning — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:46, 31 мая 2010

new SQLWarning(String reason)

  
import java.sql.SQLException;
import java.sql.SQLWarning;
public class Main {
  public static void main(String[] args) {
    try {
      throwWarning();
    } catch (SQLException e) {
      System.err.println("An SQL exception occurred: " + e);
      e.printStackTrace();
      while ((e = e.getNextException()) != null) {
        System.err.println("Contained reason: " + e);
      }
    }
  }
  private static void throwWarning() throws SQLException {
    SQLWarning rootWarning = new SQLWarning("Outter warning");
    SQLWarning containedWarning = new SQLWarning("Inner warning");
    rootWarning.setNextWarning(containedWarning);
    throw rootWarning;
  }
}





SQLWarning: getNextWarning()

  
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);
    }
  }
}





SQLWarning: setNextWarning(SQLWarning w)

  
import java.sql.SQLException;
import java.sql.SQLWarning;
public class Main {
  public static void main(String[] args) {
    try {
      throwWarning();
    } catch (SQLException e) {
      System.err.println("An SQL exception occurred: " + e);
      e.printStackTrace();
      while ((e = e.getNextException()) != null) {
        System.err.println("Contained reason: " + e);
      }
    }
  }
  private static void throwWarning() throws SQLException {
    SQLWarning rootWarning = new SQLWarning("Outter warning");
    SQLWarning containedWarning = new SQLWarning("Inner warning");
    rootWarning.setNextWarning(containedWarning);
    throw rootWarning;
  }
}