Java by API/java.sql/SQLWarning

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

new SQLWarning(String reason)

   <source lang="java">
 

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

}


 </source>
   
  
 
  



SQLWarning: getNextWarning()

   <source lang="java">
 

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

}


 </source>
   
  
 
  



SQLWarning: setNextWarning(SQLWarning w)

   <source lang="java">
 

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

}


 </source>