Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. import javax.xml.transform.Result;
  2. import java.security.InvalidParameterException;
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6. /**
  7.  * Created by Magnus on 12/10/2016.
  8.  */
  9. public class Main {
  10.  
  11.     static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  12.     static final String DB_URL = "jdbc:mysql://localhost/dDB36";
  13.     static final String USERNAME = "Magnus";
  14.     static final String PASSWORD = "";
  15.     public static void main(String[] args) {
  16.  
  17.         // Run many select queries on one thread
  18.         // Run many update queries on another thread
  19.  
  20.         runUserOfficeUpdate(Connection.TRANSACTION_READ_UNCOMMITTED);
  21.  
  22.  
  23.     }
  24.  
  25.     public static void runUserOfficeUpdate(int commitSettings) {
  26.         if (commitSettings != Connection.TRANSACTION_READ_COMMITTED && commitSettings != Connection.TRANSACTION_READ_UNCOMMITTED)
  27.             throw new InvalidParameterException("Commit setting can be");
  28.  
  29.         Thread t = new Thread(new Runnable() {
  30.             @Override
  31.             public void run() {
  32.                 Connection conn = null;
  33.                 Statement statement = null;
  34.                 PreparedStatement updateUserIdStatement = null;
  35.                 long endTime = 0;
  36.                 try {
  37.                     Class.forName(JDBC_DRIVER);
  38.  
  39.                     conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
  40.  
  41.                     statement = conn.createStatement();
  42.                     conn.setAutoCommit(false);
  43.                     conn.setTransactionIsolation(commitSettings);
  44.  
  45.                     //conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  46.  
  47.                     String sql = "SELECT office, userid FROM People WHERE office IS NOT NULL";
  48.  
  49.                     String updateOfficeNumberSQL = "UPDATE People SET number = ?, office = ? WHERE office LIKE ?";
  50.  
  51.                     updateUserIdStatement = conn.prepareStatement(updateOfficeNumberSQL);
  52.                     long startTime = System.currentTimeMillis();
  53.  
  54.                         ResultSet rs = statement.executeQuery(sql);
  55.  
  56.                     while (rs.next()) {
  57.                         //Retrieve by column name
  58.                         String office = rs.getString("office");
  59.                         String userId = rs.getString("userid");
  60.                         int officeNumber = Integer.parseInt(office.split("-")[1]);
  61.                         String newOffice = office.split("-")[0] + "-" + officeNumber;
  62.                         officeNumber = officeNumber + 1;
  63.                         updateUserIdStatement.setInt(1, officeNumber);
  64.                         updateUserIdStatement.setString(2, newOffice);
  65.                         updateUserIdStatement.setString(3, office.split("-")[0] + "%");
  66.  
  67.                         updateUserIdStatement.executeUpdate();
  68.                     }
  69.                         rs.close();
  70.                     conn.commit();
  71.                     endTime = System.currentTimeMillis() - startTime;
  72.  
  73.                     statement.close();
  74.                     System.out.println(endTime);
  75.                 } catch (Exception e) {
  76.                     e.printStackTrace();
  77.                 } finally {
  78.                     try{
  79.                         if (statement != null)
  80.                             statement.close();
  81.                         if (updateUserIdStatement != null)
  82.                             updateUserIdStatement.close();
  83.                     } catch (SQLException e2){
  84.                         e2.printStackTrace();
  85.                     }
  86.                     try {
  87.                         if (conn!=null)
  88.                             conn.close();
  89.                     } catch (SQLException e3){
  90.                         e3.printStackTrace();
  91.                     }
  92.                 }
  93.             }
  94.  
  95.         });
  96.  
  97.         t.start();
  98.  
  99.     }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement