Advertisement
Guest User

Untitled

a guest
Mar 6th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. public class TestThreads {
  2. public static class Thread1 implements Runnable {
  3. @Override
  4. public void run() {
  5. try {
  6. Connection connection = DriverManager
  7. .getConnection("jdbc:mysql://localhost/test?"
  8. + "user=myuser&password=mypassword");
  9.  
  10. Statement statment = connection.createStatement();
  11.  
  12. statment.executeQuery("select * from `table1`");
  13.  
  14. [...]
  15.  
  16. } catch (SQLException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21.  
  22. public static class Thread2 implements Runnable {
  23. @Override
  24. public void run() {
  25. try {
  26. Connection connection = DriverManager
  27. .getConnection("jdbc:mysql://localhost/test?"
  28. + "user=myuser&password=mypassword");
  29.  
  30. Statement statment = connection.createStatement();
  31.  
  32. statment.executeQuery("select * from `table2`");
  33.  
  34. [...]
  35.  
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41.  
  42. public static void main(String[] args) {
  43. Thread thread1 = new Thread(new Thread1());
  44. Thread thread2 = new Thread(new Thread2());
  45.  
  46. thread1.start();
  47. thread2.start();
  48.  
  49. try {
  50. thread1.join(1000000);
  51. thread2.join(1000000);
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement