Advertisement
Guest User

Valeriy likes

a guest
May 27th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6.  
  7. import static java.lang.Thread.sleep;
  8. /**
  9. * Created by bogdan on 12.05.16.
  10. */
  11. public class Main {
  12. static public void main(String[] args){
  13. String login = "root";
  14. String password = "987654321";
  15. String url = "jdbc:mysql://localhost:3306/fifth";
  16.  
  17.  
  18.  
  19.  
  20.  
  21. Thread first = new Thread(){
  22. public void run(){
  23. try {
  24. Class.forName("com.mysql.jdbc.Driver");
  25.  
  26. Connection connection = DriverManager.getConnection(url, login, password);
  27. connection.setAutoCommit(false);
  28. Statement stmt = connection.createStatement();
  29. stmt.executeUpdate("update rooms set name='fromThread1' where id=2 ");
  30.  
  31. try {
  32. sleep(100);
  33. } catch (InterruptedException e) {
  34. e.printStackTrace();
  35. }
  36.  
  37. stmt.executeUpdate("update inhabitan set name='fromThread1' where id=1 ");
  38.  
  39. connection.commit();
  40. stmt.close();
  41. } catch (ClassNotFoundException e) {
  42. e.printStackTrace();
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. };
  48.  
  49.  
  50. Thread second = new Thread(){
  51. public void run(){
  52. try {
  53. Class.forName("com.mysql.jdbc.Driver");
  54.  
  55. Connection connection = DriverManager.getConnection(url, login, password);
  56. connection.setAutoCommit(false);
  57. Statement stmt = connection.createStatement();
  58.  
  59. stmt.executeUpdate("update inhabitan set name='fromThread2' where id=1 ");
  60.  
  61. try {
  62. sleep(100);
  63. } catch (InterruptedException e) {
  64. e.printStackTrace();
  65. }
  66.  
  67. stmt.executeUpdate("update rooms set name='fromThread2' where id=2 ");
  68.  
  69.  
  70.  
  71. connection.commit();
  72. stmt.close();
  73. } catch (ClassNotFoundException e) {
  74. e.printStackTrace();
  75. } catch (SQLException e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. };
  80.  
  81. first.start();
  82. second.start();
  83.  
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement