Wednesday, 1 October 2014

SQLWarning class

SQLWarning represents an exception that provides information on database access warnings. You can retrieve warnings from Connection, Statement, and ResultSet objects.

Connection, Statement and ResultSet interfaces provides getWarnings method which retrieves the first warning reported by calls on this object.

/* Step 1: Import sql package */
import java.sql.*;

public class SampleApp {
    static Connection conn;
    static Statement stmt;
    static ResultSet rs;
    
    /* Update username, password and driver details here */
    static Connection getConnection() throws ClassNotFoundException, SQLException{
         /* Step 2: Load Driver */
        System.out.println("Loading/Registering driver");  
       
        Class.forName("com.mysql.jdbc.Driver");
         
        /* Step 3: Open connection to database */
        System.out.println("Connecting to database");
        String url = "jdbc:mysql://localhost/world";
        String userName = "root";
        String pasword = "tiger";
        return DriverManager.getConnection(url, userName, pasword);
    }
    
    static void getConnectionWarnings() throws SQLException{
        /* To get the warnings generated by connection object */
        System.out.println("Warnings generated by connection");
        System.out.println("--------------------");
        SQLWarning warning = conn.getWarnings();
        while (warning != null) {
            String message = warning.getMessage();
            String sqlState = warning.getSQLState();
            int errorCode = warning.getErrorCode();
            System.out.println(message + " " + sqlState +" " + errorCode);
            warning = warning.getNextWarning();
        }
    }
    
    static void getStatementWarnings() throws SQLException{
        System.out.println("Warnings generated by statement");
        System.out.println("--------------------");
        SQLWarning warning = stmt.getWarnings();
        while (warning != null) {
            String message = warning.getMessage();
            String sqlState = warning.getSQLState();
            int errorCode = warning.getErrorCode();
            System.out.println(message + " " + sqlState +" " + errorCode);
            warning = warning.getNextWarning();
        }
    }
    
    static void getResultSetWarnings() throws SQLException{
        System.out.println("Warnings generated by Result set");
        System.out.println("--------------------");
        SQLWarning warning = rs.getWarnings();
        while (warning != null) {
            String message = warning.getMessage();
            String sqlState = warning.getSQLState();
            int errorCode = warning.getErrorCode();
            System.out.println(message + " " + sqlState +" " + errorCode);
            warning = warning.getNextWarning();
        }
    }
    
    public static void main(String args[]) throws SQLException, ClassNotFoundException{
        
        conn = getConnection();
                
        conn.setAutoCommit(false);
          /* Create table employee */
        String query = "CREATE TABLE employee (id int, name varchar(30), PRIMARY KEY(id))";
        stmt = conn.createStatement();
        stmt.execute(query);     
        
        /* Insert data to employee table */
        query = "INSERT INTO employee values(1, \"Krishna\")";
        stmt.execute(query);
        query = "INSERT INTO employee values(2, \"Arjun\")";
        stmt.execute(query);
        
        conn.rollback();
        
        query = "SELECT * FROM employee";
        rs = stmt.executeQuery(query);
        
        while(rs.next()){
            int id  = rs.getInt("id");
            String name = rs.getString("name");
            System.out.println(id +" " + name);
        }
        query = "DROP TABLE employee";
        stmt.execute(query);
        
        getConnectionWarnings();
        getStatementWarnings();
        getResultSetWarnings();
        
    rs.close();
        stmt.close();
        conn.close();
    }
}


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment