Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import java.text.DecimalFormat;
  6. import java.text.NumberFormat;
  7. import java.util.Random;
  8.  
  9. public class Test1 extends Thread {
  10.  
  11. public static void main(String[] args) throws ClassNotFoundException, SQLException {
  12. long start = System.currentTimeMillis();
  13.  
  14. int evencount = 0;
  15. int oddcount = 0;
  16. int breakcon = 0;
  17. int breakcon1 = 0;
  18.  
  19. Class.forName("com.mysql.jdbc.Driver");
  20. Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1" + "?useSSL=false", "root",
  21. "1234");
  22. Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2" + "?useSSL=false", "root",
  23. "1234");
  24.  
  25. try {
  26.  
  27. Class.forName("com.mysql.jdbc.Driver");
  28.  
  29. for (int n = 1; n <= 1000; n++) {
  30. Random r = new Random();
  31. int val = r.nextInt(100000);
  32.  
  33. if (val % 2 == 0) {
  34. PreparedStatement ps = con.prepareStatement(" insert into try values (?)");
  35. ps.setInt(1, val);
  36. ps.executeUpdate();
  37. ps.addBatch();
  38. breakcon = breakcon + 1;
  39. if (breakcon % 500 == 0 || breakcon == val)
  40. ps.executeBatch();
  41. evencount++;
  42.  
  43. } else {
  44. try {
  45.  
  46. Class.forName("com.mysql.jdbc.Driver");
  47. PreparedStatement ps3 = conn.prepareStatement(" insert into try1 values (?)");
  48. ps3.setInt(1, val);
  49. ps3.executeUpdate();
  50. ps3.addBatch();
  51. breakcon1 = breakcon1 + 1;
  52. if (breakcon1 % 500 == 0 || breakcon1 == val)
  53. ps3.executeBatch();
  54.  
  55. oddcount++;
  56.  
  57. }
  58.  
  59. catch (Exception e2) {
  60. System.out.println(e2);
  61. }
  62. }
  63. }
  64.  
  65. }
  66.  
  67. catch (Exception e) {
  68. System.out.println(e);
  69. }
  70.  
  71. long end = System.currentTimeMillis();
  72. NumberFormat formatter = new DecimalFormat("#0.00000");
  73. System.out.println("Execution time is " + formatter.format((end - start) / 1000d) + " seconds");
  74. System.out.println(oddcount + evencount);
  75.  
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement