Mega Code Archive

 
Categories / Java Tutorial / Database
 

Check for a warning using a Connection object

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 {       try {         Connection conn = getConnection(); // get a java.sql.Connection object         SQLWarning warning = conn.getWarnings();         while (warning != null) {             // process connection warning             String message = warning.getMessage();             String sqlState = warning.getSQLState();             int errorCode = warning.getErrorCode();             warning = warning.getNextWarning();         }     }     catch (SQLException e) {         // ignore the exception     }     finally {       // close JDBC resources: ResultSet, Statement, Connection     }          }   private static Connection getConnection() throws Exception {     Class.forName("org.hsqldb.jdbcDriver");     String url = "jdbc:hsqldb:mem:data/tutorial";     return DriverManager.getConnection(url, "sa", "");   } }