Advertisement
Guest User

Untitled

a guest
Oct 11th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class JDBC {
  4.  
  5.  
  6.     public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
  7.  
  8.         String server = "localhost";
  9.         String port = "3306";
  10.         String url = "jdbc:mysql://" + server + ":" + port + "/test?useSSL=false";
  11.         String userid = "root";
  12.         String password = "Stormer77";
  13.         Class.forName("com.mysql.jdbc.Driver");
  14.  
  15.  
  16.         /*
  17.          * C1
  18.          */
  19.         Connection c1 = DriverManager.getConnection(url, userid, password);
  20.         c1.setAutoCommit(false);
  21.         Statement c1Statement = c1.createStatement();
  22.         int test = 8;
  23.         int update = c1Statement.executeUpdate("INSERT INTO Equipment VALUES ('SKIPPER_" + test + "','newCom')");
  24. //        System.out.println(update);
  25.  
  26.  
  27.  
  28.  
  29.         /*
  30.          * C2
  31.          */
  32.         Connection c2 = DriverManager.getConnection(url, userid, password);
  33.         c2.setAutoCommit(false);
  34.         c2.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  35.         Statement c2Statement = c2.createStatement();
  36.         double c2_start = System.nanoTime();
  37.         String selectAllFromEquipment = "SELECT * FROM Equipment";
  38.         ResultSet c2_resultSet = c2Statement.executeQuery(selectAllFromEquipment);
  39.         double c2_end = System.nanoTime();
  40.         System.out.println("\nC2 took " + (c2_end - c2_start) + " nanoseconds for query: " + selectAllFromEquipment + "\n");
  41.         while (c2_resultSet.next()) {
  42.             System.out.println(c2_resultSet.getString(1) + " " + c2_resultSet.getString(2));
  43.         }
  44.  
  45.  
  46. //        System.out.println("\n=================================\n HERE \n=================================\n");
  47.  
  48.         /*
  49.          * C3
  50.          */
  51.         Connection c3 = DriverManager.getConnection(url, userid, password);
  52.         c3.setAutoCommit(false);
  53.         c3.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  54.         Statement c3Statement = c3.createStatement();
  55.  
  56.  
  57.         double c3_start = System.nanoTime();
  58.         ResultSet c3_resultSet = c3Statement.executeQuery(selectAllFromEquipment);
  59.         double c3_end = System.nanoTime();
  60.  
  61.         System.out.println("\nC3 took " + (c3_end - c3_start) + " nanoseconds for query: " + selectAllFromEquipment + "\n");
  62.         while (c3_resultSet.next()) {
  63.             System.out.println(c3_resultSet.getString(1) + " " + c3_resultSet.getString(2));
  64.         }
  65.  
  66.  
  67.         c1Statement.close();
  68.         c2Statement.close();
  69.         c1Statement.close();
  70.  
  71.         // SET TO NOT AUTOCOMMIT
  72.  
  73.         // c1 update
  74.  
  75.         // c2 reaad uncommitted
  76.         // Select  fra c1 kan fĂ„ fat i update
  77.  
  78.  
  79.         // c3 read committed
  80.         // Select Kan ikke fĂ„ fat i update
  81.  
  82.  
  83.         // C1 commit
  84.         // C2 commit
  85.         // C3 commit
  86.  
  87.         // COMMIT
  88.         c1.commit();
  89.         c2.commit();
  90.         c3.commit();
  91.  
  92.         // CLOSE
  93.         c1.close();
  94.         c2.close();
  95.         c3.close();
  96.  
  97.  
  98. //        System.out.println("\n=================================\n HERE \n=================================\n");
  99. //        c1Statement.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  100. //        Statement unCommitStatement = c1Statement.createStatement();
  101. //        ResultSet unCommitResultSet = unCommitStatement.executeQuery("SELECT * FROM People");
  102. //
  103. //        while (unCommitResultSet.next()) {
  104. //            System.out.println(unCommitResultSet.getString(1) + " " +unCommitResultSet.getString(2) );
  105. //        }
  106. //        unCommitStatement.close();
  107. //
  108. //        c1Statement.close();
  109.  
  110.  
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement