Java JDBC FilteredRowSet create

Description

Java JDBC FilteredRowSet create


import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.RowSet;
import javax.sql.rowset.FilteredRowSet;
import javax.sql.rowset.Predicate;

import com.sun.rowset.FilteredRowSetImpl;

class AuthorFilter implements Predicate {

   private String[] authors;
   private String colName = null;
   private int colNumber = -1;

   public AuthorFilter(String[] authors, String colName) {
      this.authors = authors;
      this.colNumber = -1;
      this.colName = colName;
   }/*w  w w .  ja  v  a  2s  . co m*/

   public AuthorFilter(String[] authors, int colNumber) {
      this.authors = authors;
      this.colNumber = colNumber;
      this.colName = null;
   }

   public boolean evaluate(Object value, String colName) {

      if (colName.equalsIgnoreCase(this.colName)) {
         for (int i = 0; i < this.authors.length; i++) {
            if (this.authors[i].equalsIgnoreCase((String) value)) {
               return true;
            }
         }
      }
      return false;
   }

   public boolean evaluate(Object value, int colNumber) {

      if (colNumber == this.colNumber) {
         for (int i = 0; i < this.authors.length; i++) {
            if (this.authors[i].equalsIgnoreCase((String) value)) {
               return true;
            }
         }
      }
      return false;
   }

   public boolean evaluate(RowSet rs) {

      if (rs == null)
         return false;

      try {
         for (int i = 0; i < this.authors.length; i++) {

            String authorLast = null;

            if (this.colNumber > 0) {
               authorLast = (String) rs.getObject(this.colNumber);
            } else if (this.colName != null) {
               authorLast = (String) rs.getObject(this.colName);
            } else {
               return false;
            }

            if (authorLast.equalsIgnoreCase(authors[i])) {
               return true;
            }
         }
      } catch (SQLException e) {
         return false;
      }
      return false;
   }
}

public class Main {
   public static Connection conn = null;
   public static FilteredRowSet frs = null;

   public static void main(Connection c) {
      try {
         conn = c;
         implementFilteredRowSet();
      } catch (Exception ex) {
         System.out.println(ex);
      } finally {
         if (conn != null) {
            try {
               conn.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
         if (frs != null) {
            try {
               frs.close();
            } catch (SQLException ex) {
               ex.printStackTrace();
            }
         }
      }
   }

   public static void implementFilteredRowSet() {
      String[] authorArray = { "CSS", "HTML" };
      AuthorFilter authorFilter = new AuthorFilter(authorArray, 3);

      try {
         frs = new FilteredRowSetImpl();
         frs.setCommand("SELECT CHAPTER_NUMBER, CHAPTER_TITLE, LAST " + //
               "FROM BOOK_AUTHOR BA, " + //
               "AUTHOR_WORK AW " + //
               "WHERE AW.AUTHOR_ID = BA.ID");
         frs.execute(conn);
         System.out.println("Prior to adding filter:");
         viewRowSet(frs);
         System.out.println("Adding author filter:");
         frs.beforeFirst();
         frs.setFilter(authorFilter);
         viewRowSet(frs);
      } catch (SQLException e) {
         e.printStackTrace();
      }
   }

   public static void viewRowSet(RowSet rs) {
      try {
         while (rs.next()) {
            System.out.println(rs.getString(1) + " " + rs.getString(2) + " - " + rs.getString(3));
         }
      } catch (SQLException ex) {
         ex.printStackTrace();
      }
   }
}



PreviousNext

Related