Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.55 KB | None | 0 0
  1. package test;
  2.  
  3. import com.sun.rowset.*;
  4. import org.w3c.dom.ranges.Range;
  5.  
  6. import javax.sql.RowSet;
  7. import javax.sql.rowset.*;
  8. import java.sql.*;
  9. import java.util.Properties;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import static java.lang.System.out;
  13.  
  14.  
  15. public class Application {
  16.  
  17.     private final static Logger log = Logger.getLogger(Application.class.getName());
  18.     private static Connection connection;
  19.  
  20.     public static void main(String[] args) {
  21.         try {
  22.             new Application().start();
  23.         } catch (Throwable e) {
  24.             log.log(Level.SEVERE, e.getMessage(), e);
  25.         } finally {
  26.             close();
  27.         }
  28.     }
  29.  
  30.     private Connection establishConnection() throws Exception {
  31.         Driver driver = (Driver) Class.forName("com.mysql.jdbc.Driver").newInstance();
  32.         return connection = driver.connect("jdbc:mysql://localhost/mysql?user=root&password=", new Properties());
  33.     }
  34.  
  35.     private static void close() {
  36.         if (connection != null) {
  37.             try {
  38.                 connection.close();
  39.             } catch (SQLException e) {
  40.                 log.log(Level.SEVERE, e.getMessage(), e);
  41.             }
  42.         }
  43.     }
  44.  
  45.     private void start() throws Exception {
  46.         testJdbcRowSet();
  47.         testWebRowSet();
  48.         testFilteredRowSet();
  49.         testCachedRowSet();
  50.         testJoinRowSet();
  51.     }
  52.  
  53.     private void testJdbcRowSet() {
  54.         out.println(" -- JdbcRowSet Test -- ");
  55.         try {
  56.             Statement statement = establishConnection().createStatement();
  57.             if (statement.execute("show tables")) {
  58.                 ResultSet rs = statement.getResultSet();
  59.                 JdbcRowSet rowSet = new JdbcRowSetImpl(rs);
  60.  
  61.                 while (rowSet.next()) {
  62.                     rowSet.getString(1);
  63.                     out.println("rowSet.getString(1) = " + rowSet.getString(1));
  64.                 }
  65.                 out.println(" -------- --------- -------- --------- -------- --------- ");
  66.             }
  67.         } catch (Exception e) {
  68.             log.log(Level.SEVERE, e.getMessage(), e);
  69.         }
  70.     }
  71.  
  72.     private void testWebRowSet() {
  73.         out.println(" -- WebRowSet Test -- ");
  74.         try {
  75.             Statement statement = establishConnection().createStatement();
  76.             if (statement.execute("show tables")) {
  77.                 ResultSet rs = statement.getResultSet();
  78.                 WebRowSet rowSet = new WebRowSetImpl();
  79.                 rowSet.populate(rs);
  80.  
  81.  
  82.                 rowSet.absolute(1);
  83.                 rowSet.moveToInsertRow();
  84.                 rowSet.updateString(1, "new_table_in_result");
  85.                 rowSet.insertRow();
  86.  
  87.                 rowSet.moveToCurrentRow();
  88.                 rowSet.absolute(4);
  89.                 rowSet.deleteRow();
  90.  
  91.                 rowSet.writeXml(out);
  92.                 out.println(" -------- ----------------- --------- -------- --------- ");
  93.             }
  94.         } catch (Exception e) {
  95.             log.log(Level.SEVERE, e.getMessage(), e);
  96.         }
  97.     }
  98.  
  99.     private void testFilteredRowSet() {
  100.         out.println(" -- FilteredRowSet Test -- ");
  101.         try {
  102.             Statement statement = establishConnection().createStatement();
  103.             if (statement.execute("show tables")) {
  104.                 ResultSet rs = statement.getResultSet();
  105.                 FilteredRowSet rowSet = new FilteredRowSetImpl();
  106.                 rowSet.populate(rs);
  107.                 rowSet.setFilter(new Predicate() {
  108.  
  109.                     @Override
  110.                     public boolean evaluate(RowSet rs) {
  111.                         try {
  112.                             return rs.getString(1).startsWith("time");
  113.                         } catch (SQLException e) {
  114.                             return false;
  115.                         }
  116.                     }
  117.  
  118.                     @Override
  119.                     public boolean evaluate(Object value, int column) throws SQLException {
  120.                         return value.toString().startsWith("time");
  121.                     }
  122.  
  123.                     @Override
  124.                     public boolean evaluate(Object value, String columnName) throws SQLException {
  125.                         return value.toString().startsWith("time");
  126.                     }
  127.                 });
  128.  
  129.                 while (rowSet.next()) {
  130.                     rowSet.getString(1);
  131.                     out.println("rowSet.getString(1) = " + rowSet.getString(1));
  132.                 }
  133.                 out.println(" -------- --------- -------- --------- -------- --------- ");
  134.             }
  135.         } catch (Exception e) {
  136.             log.log(Level.SEVERE, e.getMessage(), e);
  137.         }
  138.     }
  139.  
  140.     private void testCachedRowSet() {
  141.         out.println(" -- CachedRowSet Test -- ");
  142.         try {
  143.             Statement statement = establishConnection().createStatement();
  144.             if (statement.execute("show tables")) {
  145.                 ResultSet rs = statement.getResultSet();
  146.                 CachedRowSet rowSet = new CachedRowSetImpl();
  147.  
  148.                 rowSet.populate(rs);
  149.                 out.println("rowSet.getSyncProvider() = " + rowSet.getSyncProvider());
  150.                 out.println("rowSet.toCollection() = " + rowSet.toCollection());
  151.  
  152.                 while (rowSet.next()) {
  153.                     rowSet.getString(1);
  154.                     out.println("rowSet.getString(1) = " + rowSet.getString(1));
  155.                 }
  156.                 out.println(" -------- --------- -------- --------- -------- --------- ");
  157.             }
  158.         } catch (Exception e) {
  159.             log.log(Level.SEVERE, e.getMessage(), e);
  160.         }
  161.     }
  162.  
  163.     private void testJoinRowSet() {
  164.         out.println(" -- JoinRowSet Test -- ");
  165.         try {
  166.             Statement statement = establishConnection().createStatement();
  167.             if (statement.execute("select host, user, password from user")) {
  168.                 CachedRowSet firstRowSet = new CachedRowSetImpl();
  169.                 firstRowSet.populate(statement.getResultSet());
  170.                 firstRowSet.setMatchColumn(new int[] {1, 2});
  171.  
  172.                 Statement st = establishConnection().createStatement();
  173.                 if (st.execute("select host, user, max_user_connections from user")) {
  174.                     JdbcRowSet secondRowSet = new JdbcRowSetImpl(st.getResultSet());
  175.                     secondRowSet.setMatchColumn(new int[] {1, 2});
  176.  
  177.  
  178.                     JoinRowSet joinRowSet = new JoinRowSetImpl();
  179.                     joinRowSet.addRowSet(firstRowSet);
  180.                     joinRowSet.addRowSet(secondRowSet);
  181.  
  182.  
  183.                     out.println("joinRowSet.getMetaData() = " + joinRowSet.getMetaData().getColumnCount());
  184.                     while (joinRowSet.next()) {
  185.                         out.println("joinRowSet.getString(\"host\") = " + joinRowSet.getString("host"));
  186.                         out.println("joinRowSet.getString(\"user\") = " + joinRowSet.getString("user"));
  187.                         out.println("joinRowSet.getString(\"password\") = " + joinRowSet.getString("password"));
  188.                         out.println("joinRowSet.getString(\"max_user_connections\") = " + joinRowSet.getString("max_user_connections"));
  189.                         if (!joinRowSet.isLast()) {
  190.                             out.println("-- next row --");
  191.                         }
  192.                     }
  193.                 }
  194.                 out.println(" -------- --------- -------- --------- -------- --------- ");
  195.             }
  196.         } catch (Exception e) {
  197.             log.log(Level.SEVERE, e.getMessage(), e);
  198.         }
  199.     }
  200.  
  201.  
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement