Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package lab2;
  2.  
  3. import java.sql.*;
  4. import java.util.Random;
  5.  
  6. public class Main {
  7.    
  8.  
  9.     private static final String jdbcClassName   = "com.ibm.db2.jcc.DB2Driver";
  10.     private static final String databaseName    = "Lab1";
  11.     private static final String baseUrl         = "jdbc:db2://localhost:50000/" + databaseName;
  12.     private static final String user            = "db2inst1";
  13.     private static final String password        = "db2";
  14.    
  15.     private static final int LOOP_CYCLES = 100;
  16.    
  17.     private static final String SELECT_AVG_RATING = "SELECT movie_id,"
  18.                     + "CAST(ROUND(AVG(rating), 3) AS DECIMAL(10,4)),"
  19.                     + "COUNT(movie_id)"
  20.                     + "FROM rating "
  21.                     + "WHERE movie_id = ? GROUP BY movie_id";
  22.    
  23.  
  24.     private static void executeAndPrintResults(ResultSet result, int i) throws SQLException {
  25.         while(result.next()) {  
  26.             System.out.println((i) + ". " + result.getInt(1) + " \t\t" + result.getDouble(2) + " \t\t" + result.getInt(3));
  27.             i++;
  28.         }
  29.     }
  30.  
  31.     private static Statement getStatement(Connection con) throws SQLException {
  32.         return con.createStatement();
  33.     }
  34.    
  35.    
  36.     private static PreparedStatement getPreparedStatement(Connection con, String query) throws SQLException {
  37.         PreparedStatement statement =  con.prepareStatement(query);
  38.         statement.setInt(1, getRandomInt());
  39.         return statement;
  40.     }
  41.    
  42.     private static int getRandomInt() {
  43.         return new Random().nextInt(LOOP_CYCLES) + 1;
  44.     }
  45.    
  46.    
  47.    
  48.     public static void main(String[] args) {
  49.  
  50.         long startTime = System.currentTimeMillis();
  51.         Connection connection = null;
  52.         try {
  53.             //Load class into memory
  54.             Class.forName(jdbcClassName);
  55.             //Establish connection
  56.             connection = DriverManager.getConnection(baseUrl, user, password);
  57.            
  58.             boolean execPreparedStatement = true;
  59.             boolean execStatement = false;
  60.            
  61.             Statement statement = null;
  62.            
  63.             if(execPreparedStatement) {
  64.                 for(int i = 0; i < LOOP_CYCLES;i++) {
  65.                     statement = getPreparedStatement(connection, SELECT_AVG_RATING);
  66.                     executeAndPrintResults(((PreparedStatement)statement).executeQuery(), i + 1);
  67.                 }
  68.             }
  69.            
  70.             if(execStatement) {
  71.                 for(int i = 0; i < LOOP_CYCLES;i++) {
  72.                     statement = getStatement(connection);
  73.                     String generatedQuery = SELECT_AVG_RATING.replace("?", "" + getRandomInt());   
  74.                     executeAndPrintResults(statement.executeQuery(generatedQuery), i + 1);
  75.                 }
  76.             }
  77.            
  78.         } catch (ClassNotFoundException e) {
  79.             e.printStackTrace();
  80.         } catch (SQLException e) {
  81.             e.printStackTrace();
  82.         } finally {
  83.             if(connection != null) {
  84.                 try {
  85.                     connection.close();
  86.                     System.out.println("Connection closed. Execution time " + (System.currentTimeMillis() - startTime) + " ms.");
  87.                 } catch (SQLException e) {
  88.                     e.printStackTrace();
  89.                 }
  90.             }
  91.         }
  92.  
  93.     }
  94.    
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement