Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. package OpgA;
  2.  
  3. import java.awt.List;
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6.  
  7. public class Service {
  8.  
  9. private static Connection conn = null;
  10. private static int returId = -1;
  11. private static int returNr = -1;
  12. private static int delId = -1;
  13. private static int delIdNy = -1;
  14.  
  15. public static Connection createConnection() {
  16. try {
  17. String userName = "root";
  18. String password = "whenever";
  19. String url = "jdbc:mysql://localhost/carletti1";
  20. Class.forName("com.mysql.jdbc.Driver").newInstance();
  21. conn = DriverManager.getConnection(url, userName, password);
  22. System.out.println("\tDatabase connection established");
  23. } catch (Exception e) {
  24. System.err.println("Cannot connect to database server: " + e);
  25. throw new RuntimeException(e.getMessage());
  26. }
  27. return conn;
  28.  
  29. }
  30.  
  31. public static void closeConnection() {
  32. if (conn == null)
  33. return;
  34.  
  35. try {
  36. conn.close();
  37. System.out.println("\tDatabase connection closed");
  38. } catch (SQLException e) {
  39. System.out.println("Cann't close connection\n " + e.getMessage());
  40. throw new RuntimeException(e.getMessage());
  41. }
  42. }
  43.  
  44. public static void startTransaction() {
  45. try {
  46. conn.setAutoCommit(false);
  47. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  48. } catch (SQLException e) {
  49. throw new RuntimeException(e.getMessage());
  50. }
  51. }
  52.  
  53. public static void commit() {
  54. try {
  55. conn.commit();
  56. } catch (SQLException e) {
  57. System.out.println(e);
  58. }
  59. }
  60.  
  61. public static void rollback() {
  62. try {
  63. conn.rollback();
  64. } catch (SQLException e) {
  65. throw new RuntimeException(e.getMessage());
  66. }
  67. }
  68.  
  69. enum IsoLevel
  70. {
  71. READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE;
  72. }
  73.  
  74. public static void settransactionIsolationLevel(IsoLevel level) {
  75. try {
  76. switch (level) {
  77. case READ_UNCOMMITTED:
  78. conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  79. break;
  80. case READ_COMMITTED:
  81. conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  82. break;
  83. case REPEATABLE_READ:
  84. conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  85. break;
  86. case SERIALIZABLE:
  87. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  88. break;
  89. default:
  90. break; //not possible
  91. }
  92. }
  93. catch (SQLException e) {
  94. throw new RuntimeException(e.getMessage());
  95. }
  96. }
  97.  
  98. public static String afslutDelbehandling(int mellemvare) {
  99. try {
  100. PreparedStatement p = conn
  101. .prepareStatement("SELECT retur_id, returnr, del_id FROM retur WHERE mellemvare_id = ? ORDER BY tidspunkt DESC LIMIT 1");
  102. p.setInt(1, mellemvare);
  103. ResultSet rs = p.executeQuery();
  104. rs.first();
  105. returId = rs.getInt("retur_id");
  106. returNr = rs.getInt("returnr");
  107. delId = rs.getInt("del_id");
  108.  
  109. p = conn.prepareStatement("UPDATE retur SET afsluttet = 1 WHERE retur_id = ?");
  110. p.setInt(1, returId);
  111.  
  112. rs.close();
  113. p.close();
  114.  
  115. } catch (SQLException e) {
  116. throw new RuntimeException(e.getMessage());
  117. }
  118. return " ";
  119. }
  120.  
  121. public static String begyndNyDelbehandling(int mellemvare, String date) {
  122. try {
  123. PreparedStatement p = conn
  124. .prepareStatement("SELECT del_id, raeknr FROM delbehandling LEFT JOIN raekkefoelge ON delbehandling.del_id = raekkefoelge.delbehandling_id LEFT JOIN behandling ON behandling.beh_id = raekkefoelge.behandling_id LEFT JOIN produkttype ON produkttype.beh_id = behandling.beh_id LEFT JOIN mellemvare ON mellemvare.prod_id = produkttype.prod_id WHERE mellemvare.mel_id = ? AND raeknr > ? ORDER BY raekkefoelge.raeknr ASC LIMIT 1");
  125. p.setInt(1, mellemvare);
  126. p.setInt(2, returNr);
  127. ResultSet rs2 = p.executeQuery();
  128. rs2.first();
  129. delIdNy = rs2.getInt("del_id");
  130.  
  131. p = conn.prepareStatement("INSERT INTO retur VALUES(0, ?, ?, 0, ?, ?)");
  132. p.setInt(1, returNr + 1);
  133. p.setString(2, date);
  134. p.setInt(3, delIdNy);
  135. p.setInt(4, mellemvare);
  136.  
  137. p.executeUpdate();
  138. rs2.close();
  139. p.close();
  140. } catch (SQLException e) {
  141. throw new RuntimeException("Mellemvaren er færdigbehandlet og der kan derfor ikke tilføjes flere delbehandlinger");
  142. }
  143. return " ";
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement