Monday, 1 September 2014

JDBC : JDBCRowSet Event Handling Demo


















Click here to watch in Youtube :  https://www.youtube.com/watch?v=p8VzKEpG_KE

Click the below Image to Enlarge
JDBCRowSet Event Handling Demo
JDBCRowSet Event Handling Demo
MyRowSetListener.java
import javax.sql.RowSetEvent;
import javax.sql.RowSetListener;

public class MyRowSetListener implements RowSetListener
{
    
    /*
     * A cursor movement.
     */
    @Override
    public void cursorMoved( RowSetEvent event )
    {
        System.out.println("Cursor Moved method is called in "+this.getClass().getName());
        System.out.println(event.toString());
    }


    /*
     * The update, insertion, or deletion of a row.
     */
    
    @Override
    public void rowChanged( RowSetEvent event )
    {
        System.out.println("Cursor Changed method is called in "+this.getClass().getName());
        System.out.println(event.toString());
    }


    /*
     * A change to the entire RowSet contents.
     */
    @Override
    public void rowSetChanged( RowSetEvent event )
    {
        System.out.println("RowSet changed method is called in "+this.getClass().getName());
        System.out.println(event.toString());
    }
}

JDBCRowSetEventHandlingDemo.java
import java.sql.SQLException;
import java.util.Scanner;

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetFactory;
import javax.sql.rowset.RowSetProvider;

public class JDBCRowSetEventHandlingDemo
{
    // JDBC driver name and database URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL      = "jdbc:mysql://localhost:3306/world";

    // Database credentials
    static final String USERNAME    = "root";
    static final String PASSWORD    = "root";

    public static void main( String[] args )
    {
        JDBCRowSetEventHandlingDemo jdbcRowSetEventHandlingDemo = 
                                          new JDBCRowSetEventHandlingDemo();
        Scanner scanner = new Scanner(System.in);
        while( true )
        {
            System.out.print("Enter City Country Code :");
            String cityCountrycode = scanner.nextLine();

            if( cityCountrycode.equals("exit") )
            {
                break;
            }

            jdbcRowSetEventHandlingDemo.getCityInformation(cityCountrycode);

        }
        scanner.close();
    }

    private void getCityInformation( String cityCountrycode )
    {
        JdbcRowSet jdbcRowSet = null;
        try
        {
            /*
             * Using RowSetFactory create the JdbcRowSet object.
             */
            RowSetFactory rowSetFactory = RowSetProvider.newFactory();
            jdbcRowSet = rowSetFactory.createJdbcRowSet();

            /*
             * Set the JdbcRowSet properties [URL,username,password,command]
             */
            jdbcRowSet.setUrl(DB_URL);
            jdbcRowSet.setUsername(USERNAME);
            jdbcRowSet.setPassword(PASSWORD);

            /*
             * Sets the command property with a query that produces a ResultSet
             * object containing all the data in the table
             */

            jdbcRowSet.setCommand("select * from city where countrycode=?");
            jdbcRowSet.setString(1, cityCountrycode);

            /*
             * The execute method does many things for you in the background:
             * 
             *  1.It makes a connection to the database using the values you assigned
             *  to the url,username, and password properties.
             *    
             *  2.It executes the query you set in the command property.
             *  
             *  3.It reads the data from the resulting ResultSet object into the jdbcRs object.
             *  
             */         
            jdbcRowSet.execute();

        
            /*
             * Adding Listener to the jdbcRowSet
             */
            
            MyRowSetListener myRowSetListener = new MyRowSetListener();
            jdbcRowSet.addRowSetListener(myRowSetListener);
            
            /*
             *  Iterate the jdbcRowSet and get each row information
             *  of city table.
             */
            
            while( jdbcRowSet.next() )
            {
                int id = jdbcRowSet.getInt(1);
                String name = jdbcRowSet.getString(2);
                String countryCode = jdbcRowSet.getString(3);
                String district = jdbcRowSet.getString(4);
                int population = jdbcRowSet.getInt(5);

                /*
                 * Display values
                 */
                System.out.print("ID: " + id);
                System.out.print(", Name: " + name);
                System.out.print(", CountryCode: " + countryCode);
                System.out.print(", District: " + district);
                System.out.println(", Population: " + population);
            }
            
        }
        catch( SQLException se )
        {
            se.printStackTrace();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }   
        
        finally
        {
            if(jdbcRowSet!=null)
            {
                try
                {
                    jdbcRowSet.close();
                }
                catch( SQLException e )
                {
                    e.printStackTrace();
                }
            }
        }

    }
}

Output
Enter City Country Code :ind
Cursor Moved method is called in MyRowSetListener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@87c06e0]
ID: 1024, Name: Mumbai (Bombay), CountryCode: IND, District: Maharashtra, Population: 10500000
Cursor Moved method is called in MyRowSetListener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@87c06e0]
ID: 1025, Name: Delhi, CountryCode: IND, District: Delhi, Population: 7206704
Cursor Moved method is called in MyRowSetListener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@87c06e0]
ID: 1026, Name: Calcutta [Kolkata], CountryCode: IND, District: West Bengali, Population: 4399819
Cursor Moved method is called in MyRowSetListener
javax.sql.RowSetEvent[source=com.sun.rowset.JdbcRowSetImpl@87c06e0]

--
--
Environment Used 

JDK version : 1.7.0_51
Mysql Server version : 5.6.19 

To Download JDBCRowSetEventHandlingDemoApp Project Click the below link

https://sites.google.com/site/javaee4321/jdbc/JDBCRowSetEventHandlingDemoApp.zip?attredirects=0&d=1

See also:


  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge