Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class JDBC {
- public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
- String server = "localhost";
- String port = "3306";
- String url = "jdbc:mysql://" + server + ":" + port + "/test?useSSL=false";
- String userid = "root";
- String password = "Stormer77";
- Class.forName("com.mysql.jdbc.Driver");
- /*
- * C1
- */
- Connection c1 = DriverManager.getConnection(url, userid, password);
- c1.setAutoCommit(false);
- Statement c1Statement = c1.createStatement();
- int test = 8;
- int update = c1Statement.executeUpdate("INSERT INTO Equipment VALUES ('SKIPPER_" + test + "','newCom')");
- // System.out.println(update);
- /*
- * C2
- */
- Connection c2 = DriverManager.getConnection(url, userid, password);
- c2.setAutoCommit(false);
- c2.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
- Statement c2Statement = c2.createStatement();
- double c2_start = System.nanoTime();
- String selectAllFromEquipment = "SELECT * FROM Equipment";
- ResultSet c2_resultSet = c2Statement.executeQuery(selectAllFromEquipment);
- double c2_end = System.nanoTime();
- System.out.println("\nC2 took " + (c2_end - c2_start) + " nanoseconds for query: " + selectAllFromEquipment + "\n");
- while (c2_resultSet.next()) {
- System.out.println(c2_resultSet.getString(1) + " " + c2_resultSet.getString(2));
- }
- // System.out.println("\n=================================\n HERE \n=================================\n");
- /*
- * C3
- */
- Connection c3 = DriverManager.getConnection(url, userid, password);
- c3.setAutoCommit(false);
- c3.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
- Statement c3Statement = c3.createStatement();
- double c3_start = System.nanoTime();
- ResultSet c3_resultSet = c3Statement.executeQuery(selectAllFromEquipment);
- double c3_end = System.nanoTime();
- System.out.println("\nC3 took " + (c3_end - c3_start) + " nanoseconds for query: " + selectAllFromEquipment + "\n");
- while (c3_resultSet.next()) {
- System.out.println(c3_resultSet.getString(1) + " " + c3_resultSet.getString(2));
- }
- c1Statement.close();
- c2Statement.close();
- c1Statement.close();
- // SET TO NOT AUTOCOMMIT
- // c1 update
- // c2 reaad uncommitted
- // Select fra c1 kan fÄ fat i update
- // c3 read committed
- // Select Kan ikke fÄ fat i update
- // C1 commit
- // C2 commit
- // C3 commit
- // COMMIT
- c1.commit();
- c2.commit();
- c3.commit();
- // CLOSE
- c1.close();
- c2.close();
- c3.close();
- // System.out.println("\n=================================\n HERE \n=================================\n");
- // c1Statement.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
- // Statement unCommitStatement = c1Statement.createStatement();
- // ResultSet unCommitResultSet = unCommitStatement.executeQuery("SELECT * FROM People");
- //
- // while (unCommitResultSet.next()) {
- // System.out.println(unCommitResultSet.getString(1) + " " +unCommitResultSet.getString(2) );
- // }
- // unCommitStatement.close();
- //
- // c1Statement.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement