Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.27 KB | None | 0 0
  1. package mysqltest;
  2.  
  3. import java.sql.Statement;
  4. import java.sql.SQLException;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.Connection;
  8.  
  9. public class ThreadForMysql implements Runnable
  10. {
  11.     //Allgemeine Parameter
  12.     private Thread t;
  13.     private String threadName;
  14.     private int counter;
  15.     static int n;
  16.     static int threadCounter;
  17.     Connection conn;
  18.    
  19.     //PreparedStatements
  20.     final PreparedStatement stmt_b;
  21.     final PreparedStatement stmt_a;
  22.     final PreparedStatement stmt_t;
  23.    
  24.     //Datenbankverbindungsparameter
  25.     String user="root";
  26.     String pass="";
  27.     String host="jdbc:mysql://localhost:3306/n-tps?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false"
  28.             + "&serverTimezone=UTC"
  29.             + "&rewriteBatchedStatements=true";
  30.  
  31.     //n-Wert für die Anzahl der erstellten Datensätze und die Anzahl an verwendbaren Threads
  32.     static {
  33.         ThreadForMysql.n =10;
  34.         ThreadForMysql.threadCounter = 8;
  35.     }
  36.    
  37.     //Threads
  38.     ThreadForMysql(final String name, final int counter) throws SQLException {
  39.         this.threadName = name;
  40.         this.counter = counter;
  41.         this.conn = DriverManager.getConnection(host,user,pass);
  42.         this.conn.setAutoCommit(false);
  43.         this.stmt_b = this.conn.prepareStatement("INSERT INTO branches (branchid,branchname,balance,address) VALUES"
  44.                 + "(?,'aaaaaaaaaaaaaaaaaaaa',0,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')");
  45.         this.stmt_a = this.conn.prepareStatement("INSERT INTO accounts (accid,name,balance,branchid,address) VALUES"
  46.                 + "(?,'aaaaaaaaaaaaaaaaaaaa',0,?,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')");
  47.         this.stmt_t = this.conn.prepareStatement("INSERT INTO tellers (tellerid,tellername,balance,branchid,address) VALUES"
  48.                 + "(?,'aaaaaaaaaaaaaaaaaaaa',0,?,'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')");
  49.     }
  50.    
  51.     //Verbindung zur Datenbank
  52.     public void waitForfinish() throws InterruptedException {
  53.         t.join();
  54.     }
  55.    
  56.     //Ausgabe aller gestarteten Threads
  57.     public void run() {
  58.         System.out.println("Running " + this.threadName);
  59.        
  60.         try {
  61.            
  62.            //Temporäres Ausschalten der Fremschlüsselüberprüfung
  63.             stmt_b.executeUpdate("SET FOREIGN_KEY_CHECKS = 0");
  64.             stmt_t.executeUpdate("SET FOREIGN_KEY_CHECKS = 0");
  65.             stmt_a.executeUpdate("SET FOREIGN_KEY_CHECKS = 0");
  66.            
  67.             //Befüllen der Tabelle Branches
  68.             for (int branchid = this.counter; branchid <= ThreadForMysql.n; branchid += threadCounter) {
  69.                 stmt_b.setInt(1, branchid);
  70.                 stmt_b.addBatch();
  71.                 if (branchid % 1000 == 0) {
  72.                     stmt_b.executeBatch();
  73.                 }
  74.             }
  75.             stmt_b.executeBatch();
  76.            
  77.             //Befüllen der Tabelle Accounts
  78.             for (int accid = this.counter; accid <= ThreadForMysql.n * 100000; accid += threadCounter) {
  79.                 final int branchid2 = 1 + (int)(Math.random() * (ThreadForMysql.n - 1 + 1));
  80.                 stmt_a.setInt(1, accid);
  81.                 stmt_a.setInt(2, branchid2);
  82.                 stmt_a.addBatch();
  83.                 if (accid % 1000 == 0) {
  84.                     stmt_a.executeBatch();
  85.                  
  86.                 }
  87.             }
  88.             stmt_a.executeBatch();
  89.            
  90.             //Befüllen der Tabelle Tellers
  91.             for (int tellerid = this.counter; tellerid <= ThreadForMysql.n * 10; tellerid += threadCounter) {
  92.                 final int branchid2 = 1 + (int)(Math.random() * (ThreadForMysql.n - 1 + 1));
  93.                 stmt_t.setInt(1, tellerid);
  94.                 stmt_t.setInt(2, branchid2);
  95.                 stmt_t.addBatch();
  96.                 if (tellerid % 1000 == 0) {
  97.                     stmt_t.executeBatch();
  98.                 }
  99.             }
  100.             stmt_t.executeBatch();
  101.            
  102.            //Reaktivierung der Fremdschlüsselüberprüfung
  103.             stmt_t.executeUpdate("SET FOREIGN_KEY_CHECKS = 1");
  104.             stmt_b.executeUpdate("SET FOREIGN_KEY_CHECKS = 1");
  105.             stmt_a.executeUpdate("SET FOREIGN_KEY_CHECKS = 1");
  106.            
  107.            
  108.            
  109.             //Schließen der Prepared Statements um Fehlern in der Speicherverwaltung zu entgehen
  110.             stmt_t.close();
  111.             stmt_b.close();
  112.             stmt_a.close();
  113.            
  114.            
  115.            
  116.             //Schließen der Verbindung und Übergabe an die Datenbank
  117.             this.conn.commit();
  118.             this.conn.close();
  119.            
  120.             //Ausgabe aller fertigen Threads
  121.             System.out.println("Fertig " + this.threadName);
  122.         }
  123.        
  124.         //Exception
  125.         catch (SQLException e) {
  126.             System.out.println(e);
  127.         }
  128.     }
  129.    
  130.     //Startfunktion für Threads
  131.     public void start() {
  132.         //System.out.println("Starting " + this.threadName);
  133.         if (this.t == null) {
  134.             (this.t = new Thread(this, this.threadName)).start();
  135.         }
  136.     }
  137.    
  138.     //SQLException
  139.     public static void main(final String[] args) throws SQLException {
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement