Guest User

Untitled

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