In previous couple of posts Insert and Select Rows with JdbcRowSet Objects and Update and Delete Rows with JdbcRowSet Objects we learnt how we can perform CRUD operation using JdbcRowSet
In this post we will learn about Jdbc RowSetListener using an example
SQL Query to create employee_table and insert few rows in MySQL (jdbcdb schema)
(Note:Make sure you have created employee_table and inserted few records in jdbcdb database schema)
DB.sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
CREATE TABLE `employee_table` ( `employee_id` int(11) NOT NULL auto_increment, `employee_name` varchar(60) NOT NULL, `email` varchar(45) NOT NULL, `salary` double default NULL, `date_of_joining` datetime default NULL, `bonus` decimal(10,0) default NULL, PRIMARY KEY (`employee_id`) ) ; INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) INSERT INTO `employee_table` (`employee_name`,`email`,`salary`,`date_of_joining`,`bonus`) |
After running above db script in your MySQL jdbcdb schema employee_table will have following content.
EmployeeServiceRowSetListener.java listener class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
package com.infotech.listener; import java.math.BigDecimal; import java.sql.SQLException; import javax.sql.RowSetEvent; import javax.sql.RowSetListener; import com.sun.rowset.JdbcRowSetImpl; /* * @author KK JavaTutorials * A Listener class which implements RowSetListener interface * that wants to be notified when a significant * event happens in the life of a RowSetobject. */ public class EmployeeServiceRowSetListener implements RowSetListener{ @Override public void rowSetChanged(RowSetEvent event) { System.out.println("RowSetChanged event.."); Object source = event.getSource(); System.out.println(source); } @Override public void rowChanged(RowSetEvent event) { System.out.println("RowChanged event.."); Object source = event.getSource(); System.out.println(source); } @Override public void cursorMoved(RowSetEvent event) { Object source = event.getSource(); JdbcRowSetImpl jdbcRowSet=(JdbcRowSetImpl)source; try { if(jdbcRowSet.isAfterLast()||jdbcRowSet.isBeforeFirst()){ //do nothing }else{ System.out.println("Cursor Moved event occured with following employee details:::"); int empId = jdbcRowSet.getInt("employee_id"); String eName = jdbcRowSet.getString("employee_name"); String email = jdbcRowSet.getString("email"); Double salary = jdbcRowSet.getDouble("salary"); BigDecimal bonus = jdbcRowSet.getBigDecimal("bonus"); System.out.println(empId + "\t" + eName + "\t" + salary + "\t" + email + "\t" + bonus); } } catch (SQLException e) { e.printStackTrace(); }finally { try { if(jdbcRowSet.isAfterLast()){ jdbcRowSet.close(); } } catch (SQLException e) { e.printStackTrace(); } } System.out.println("---------------------------------------------------"); } } |
SelectRowsWithRowSetListenerClientTest.java this class basically register EmployeeServiceRowSetListener to show how life cycle method called for selecting rows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.infotech.client; import java.math.BigDecimal; import java.sql.SQLException; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; import com.infotech.listener.EmployeeServiceRowSetListener; /* * @author KK JavaTutorials * This class basically register EmployeeServiceRowSetListener to show how life cycle * method called for selecting rows */ public class SelectRowsWithRowSetListenerClientTest { private static final String DB_USERNAME="root"; private static final String DB_PASSWORD="root"; private static final String DB_URL ="jdbc:mysql://localhost:3306/jdbcdb"; public static void main(String[] args) throws SQLException { //Creates a new instance of a RowSetFactory implementation. RowSetFactory rowSetFactory = RowSetProvider.newFactory(); //Creates a new instance of a JdbcRowSet. JdbcRowSet jdbcRowSet = rowSetFactory.createJdbcRowSet(); jdbcRowSet.setUrl(DB_URL); jdbcRowSet.setUsername(DB_USERNAME); jdbcRowSet.setPassword(DB_PASSWORD); //Sets this RowSet object's command property to the given SQL query. jdbcRowSet.setCommand("SELECT *FROM employee_table"); jdbcRowSet.execute(); //Registers the given listener so that it will be notified of events that occur on this RowSet object. jdbcRowSet.addRowSetListener(new EmployeeServiceRowSetListener()); showEmployeesInfo(jdbcRowSet); } private static void showEmployeesInfo(JdbcRowSet jdbcRowSet) throws SQLException { while (jdbcRowSet.next()) { int empId = jdbcRowSet.getInt("employee_id"); String eName = jdbcRowSet.getString("employee_name"); String email = jdbcRowSet.getString("email"); Double salary = jdbcRowSet.getDouble("salary"); BigDecimal bonus = jdbcRowSet.getBigDecimal("bonus"); System.out.println(empId + "\t" + eName + "\t" + salary + "\t" + email + "\t" + bonus); } } } |
AddRowWithRowSetListenerClientTest.java this class basically register EmployeeServiceRowSetListener to show how life cycle method called for event for adding a row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.infotech.client; import java.math.BigDecimal; import java.sql.SQLException; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; import com.infotech.listener.EmployeeServiceRowSetListener; /* * @author KK JavaTutorials * This class basically register EmployeeServiceRowSetListener to show how life cycle * method called for event for adding a row. */ public class AddRowWithRowSetListenerClientTest { private static final String DB_USERNAME = "root"; private static final String DB_PASSWORD = "root"; private static final String DB_URL = "jdbc:mysql://localhost:3306/jdbcdb"; public static void main(String[] args) throws SQLException { // Creates a new instance of a RowSetFactory implementation. RowSetFactory rowSetFactory = RowSetProvider.newFactory(); // Creates a new instance of a JdbcRowSet. JdbcRowSet jdbcRowSet = rowSetFactory.createJdbcRowSet(); jdbcRowSet.setUrl(DB_URL); jdbcRowSet.setUsername(DB_USERNAME); jdbcRowSet.setPassword(DB_PASSWORD); // Sets this RowSet object's command property to the given SQL query. jdbcRowSet.setCommand("SELECT *FROM employee_table"); jdbcRowSet.execute(); // Registers the given listener so that it will be notified of events // that occur on this RowSet object. jdbcRowSet.addRowSetListener(new EmployeeServiceRowSetListener()); addEmployee(jdbcRowSet); } private static void addEmployee(JdbcRowSet jdbcRowSet) throws SQLException { jdbcRowSet.moveToInsertRow(); jdbcRowSet.updateString("employee_name", "Barry"); jdbcRowSet.updateDouble("salary", 580000.00); jdbcRowSet.updateTimestamp("date_of_joining", new java.sql.Timestamp(new java.util.Date().getTime())); jdbcRowSet.updateBigDecimal("bonus", new BigDecimal(3000.00)); jdbcRowSet.insertRow(); System.out.println("A new EMployee is added.."); jdbcRowSet.beforeFirst(); } } |
DeleteRowWithRowSetListenerClientTest.java this class basically register EmployeeServiceRowSetListener to show how life cycle method called for event for deleting a row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.infotech.client; import java.sql.SQLException; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; import com.infotech.listener.EmployeeServiceRowSetListener; /* * @author KK JavaTutorials * This class basically register EmployeeServiceRowSetListener to show how life cycle * method called for event for deleting a row. */ public class DeleteRowWithRowSetListenerClientTest { private static final String DB_USERNAME="root"; private static final String DB_PASSWORD="root"; private static final String DB_URL ="jdbc:mysql://localhost:3306/jdbcdb"; public static void main(String[] args) throws SQLException { //Creates a new instance of a RowSetFactory implementation. RowSetFactory rowSetFactory = RowSetProvider.newFactory(); //Creates a new instance of a JdbcRowSet. JdbcRowSet jdbcRowSet = rowSetFactory.createJdbcRowSet(); jdbcRowSet.setUrl(DB_URL); jdbcRowSet.setUsername(DB_USERNAME); jdbcRowSet.setPassword(DB_PASSWORD); //Sets this RowSet object's command property to the given SQL query. jdbcRowSet.setCommand("SELECT *FROM employee_table"); jdbcRowSet.execute(); //Registers the given listener so that it will be notified of events that occur on this RowSet object. jdbcRowSet.addRowSetListener(new EmployeeServiceRowSetListener()); deleteEmployeeById(jdbcRowSet); } private static void deleteEmployeeById(JdbcRowSet jdbcRowSet) throws SQLException { int employeeId = 4; while (jdbcRowSet.next()) { int empId = jdbcRowSet.getInt("employee_id"); if(employeeId ==empId ){ jdbcRowSet.deleteRow(); System.out.println("Employee is deleted with ID:"+employeeId); break; } } jdbcRowSet.beforeFirst(); } } |
UpdateRowWithRowSetListenerClientTest.java this class basically register EmployeeServiceRowSetListener to show how life cycle method called for event for update a row.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.infotech.client; import java.sql.SQLException; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetFactory; import javax.sql.rowset.RowSetProvider; import com.infotech.listener.EmployeeServiceRowSetListener; /* * @author KK JavaTutorials * This class basically register EmployeeServiceRowSetListener to show how life cycle * method called for event for update a row. */ public class UpdateRowWithRowSetListenerClientTest { private static final String DB_USERNAME = "root"; private static final String DB_PASSWORD = "root"; private static final String DB_URL = "jdbc:mysql://localhost:3306/jdbcdb"; public static void main(String[] args) throws SQLException { //Creates a new instance of a RowSetFactory implementation. RowSetFactory rowSetFactory = RowSetProvider.newFactory(); //Creates a new instance of a JdbcRowSet. JdbcRowSet jdbcRowSet = rowSetFactory.createJdbcRowSet(); jdbcRowSet.setUrl(DB_URL); jdbcRowSet.setUsername(DB_USERNAME); jdbcRowSet.setPassword(DB_PASSWORD); //Sets this RowSet object's command property to the given SQL query. jdbcRowSet.setCommand("SELECT *FROM employee_table"); jdbcRowSet.execute(); //Registers the given listener so that it will be notified of events that occur on this RowSet object. jdbcRowSet.addRowSetListener(new EmployeeServiceRowSetListener()); updateEmployeeEmailById(jdbcRowSet); } private static void updateEmployeeEmailById(JdbcRowSet jdbcRowSet) throws SQLException { int employeeId = 3; while (jdbcRowSet.next()) { int empId = jdbcRowSet.getInt("employee_id"); if (employeeId == empId) { jdbcRowSet.updateRow(); System.out.println("Email is updated."); break; } } jdbcRowSet.beforeFirst(); } } |
That’s all about this topic Jdbc RowSetListener example
You May Also Like:
Transaction basic concept
JDBC transaction management example
Insert and Select Rows with JdbcRowSet Objects
Update and Delete Rows with JdbcRowSet Objects
If you have any feedback or suggestion please feel free to drop in blow comment box.