Advertisement
Guest User

Untitled

a guest
Nov 13th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. package lab6;
  2. import com.sun.rowset.CachedRowSetImpl;
  3. import javax.sql.rowset.CachedRowSet;
  4.  
  5. import java.sql.*;
  6. import com.microsoft.sqlserver.jdbc.SQLServerResultSet;
  7.  
  8. public class CachedRowSetExample {
  9.  
  10. public static void main(String[] args) {
  11.  
  12.  
  13.  
  14. // Declare the JDBC objects.
  15. Connection con = null;
  16. Statement stmt = null;
  17. ResultSet rs = null;
  18. CachedRowSetImpl crs = null;
  19.  
  20. try {
  21.  
  22. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  23.  
  24. String connectionUrl = "jdbc:sqlserver://localhost:1433;"
  25. + "databaseName=NORTHWND;user=Bartek;password=q1w2e3r4;";
  26. con = DriverManager.getConnection(connectionUrl);
  27.  
  28.  
  29.  
  30. try
  31. {
  32. crs = new CachedRowSetImpl();
  33. }
  34. catch (SQLException e1)
  35. {
  36. // TODO Auto-generated catch block
  37. e1.printStackTrace();
  38. }
  39. stmt = con.createStatement(SQLServerResultSet.TYPE_SS_SERVER_CURSOR_FORWARD_ONLY, +
  40. SQLServerResultSet.CONCUR_READ_ONLY);
  41. ResultSet resultSet = stmt.executeQuery("select * from Orders");
  42.  
  43.  
  44.  
  45. resultSet.first();
  46. resultSet.deleteRow();
  47. System.out.println("ROW DELETED");
  48.  
  49.  
  50.  
  51. crs.populate(rs);
  52.  
  53. while (crs.next()) {
  54. System.out.println(crs.getString(2));
  55. }
  56.  
  57.  
  58. // Create and execute an SQL statement that returns a large
  59. // set of data and then display it.
  60. //String SQL = "SELECT * FROM Customers;";
  61.  
  62.  
  63. // Perform a fetch for every row in the result set.
  64. // rs = stmt.executeQuery(SQL);
  65. // timerTest(1, rs);
  66. // rs.beforeFirst();
  67. // while(rs.next())
  68. // {
  69. //
  70. // System.out.println(rs.getString(1));
  71. // }
  72. // rs.close();
  73.  
  74. // // Perform a fetch for every tenth row in the result set.
  75. // rs = stmt.executeQuery(SQL);
  76. // timerTest(10, rs);
  77. // rs.close();
  78. //
  79. // // Perform a fetch for every 100th row in the result set.
  80. // rs = stmt.executeQuery(SQL);
  81. // timerTest(100, rs);
  82. // rs.close();
  83. //
  84. // // Perform a fetch for every 1000th row in the result set.
  85. // rs = stmt.executeQuery(SQL);
  86. // timerTest(1000, rs);
  87. // rs.close();
  88. //
  89. // // Perform a fetch for every 128th row (the default) in the result set.
  90. // rs = stmt.executeQuery(SQL);
  91. // timerTest(0, rs);
  92. // rs.close();
  93.  
  94. }
  95. // Handle any errors that may have occurred.
  96. catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99.  
  100. finally {
  101. if (rs != null) try { rs.close(); } catch(Exception e) {}
  102. if (stmt != null) try { stmt.close(); } catch(Exception e) {}
  103. if (con != null) try { con.close(); } catch(Exception e) {}
  104. }
  105. }
  106.  
  107. private static void timerTest(int fetchSize, ResultSet rs) {
  108. try {
  109.  
  110. // Declare the variables for tracking the row count and elapsed time.
  111. int rowCount = 0;
  112. long startTime = 0;
  113. long stopTime = 0;
  114. long runTime = 0;
  115.  
  116. // Set the fetch size then iterate through the result set to
  117. // cache the data locally.
  118. rs.setFetchSize(fetchSize);
  119. startTime = System.currentTimeMillis();
  120. while (rs.next()) {
  121. rowCount++;
  122. System.out.println(rs.getString(2));
  123.  
  124. }
  125. stopTime = System.currentTimeMillis();
  126. runTime = stopTime - startTime;
  127.  
  128. // Display the results of the timer test.
  129. System.out.println("FETCH SIZE: " + rs.getFetchSize());
  130. System.out.println("ROWS PROCESSED: " + rowCount);
  131. System.out.println("TIME TO EXECUTE: " + runTime);
  132. System.out.println();
  133.  
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement