Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. package tui;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8.  
  9. public class Service
  10. {
  11. private static Connection conn = null;
  12.  
  13. public static Connection createConnection() {
  14. try {
  15. String userName = "root";
  16. String password = "whenever";
  17. String url = "jdbc:mysql://localhost/bank";
  18. Class.forName("com.mysql.jdbc.Driver").newInstance();
  19. conn = DriverManager.getConnection(url, userName, password);
  20. System.out.println("\tDatabase connection established");
  21. }
  22. catch (Exception e) {
  23. System.err.println("Cannot connect to database server: " + e);
  24. throw new ServiceException(e.getMessage());
  25. }
  26. return conn;
  27. }
  28.  
  29. public static void closeConnection() {
  30. if (conn == null)
  31. return;
  32.  
  33. try {
  34. conn.close();
  35. System.out.println("\tDatabase connection closed");
  36. }
  37. catch (SQLException e) {
  38. System.out.println("Cann't close connection\n " + e.getMessage());
  39. throw new ServiceException(e.getMessage());
  40. }
  41. }
  42.  
  43. public static void startTransaction() {
  44. try {
  45. conn.setAutoCommit(false);
  46. }
  47. catch (SQLException e) {
  48. throw new ServiceException(e.getMessage());
  49. }
  50. }
  51.  
  52. public static void commit() {
  53. try {
  54. conn.commit();
  55. }
  56. catch (SQLException e) {
  57. System.out.println(e);
  58. }
  59. }
  60.  
  61. public static void rollback() {
  62. try {
  63. conn.rollback();
  64. }
  65. catch (SQLException e) {
  66. throw new ServiceException(e.getMessage());
  67. }
  68. }
  69.  
  70. enum IsoLevel
  71. {
  72. READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE;
  73. }
  74.  
  75. public static void settransactionIsolationLevel(IsoLevel level) {
  76. try {
  77. switch (level) {
  78. case READ_UNCOMMITTED:
  79. conn.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
  80. break;
  81. case READ_COMMITTED:
  82. conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
  83. break;
  84. case REPEATABLE_READ:
  85. conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  86. break;
  87. case SERIALIZABLE:
  88. conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
  89. break;
  90. default:
  91. break; //not possible
  92. }
  93. }
  94. catch (SQLException e) {
  95. throw new ServiceException(e.getMessage());
  96. }
  97. }
  98.  
  99. //-------------------------------------------------------------------------
  100.  
  101. public static boolean existsKonto(int kontoNr) {
  102. boolean found = false;
  103. try {
  104. PreparedStatement p;
  105. p = conn.prepareStatement("SELECT COUNT(*) FROM account WHERE number = ?");
  106. p.setInt(1, kontoNr);
  107. ResultSet rs = p.executeQuery();
  108. rs.first();
  109. if (rs.getInt(1) == 1)
  110. found = true;
  111. p.close();
  112. }
  113. catch (SQLException e) {
  114. throw new ServiceException(e.getMessage());
  115. }
  116. return found;
  117. }
  118.  
  119. public static int getSaldo(int kontoNr) {
  120. int saldo = -1;
  121. try {
  122. PreparedStatement p;
  123. p = conn.prepareStatement("SELECT balance FROM account WHERE number = ?");
  124. // p = conn.prepareStatement("SELECT balance FROM account WHERE number = ? LOCK IN SHARE MODE");
  125. p.setInt(1, kontoNr);
  126. ResultSet rs = p.executeQuery();
  127. rs.first();
  128. saldo = rs.getInt("balance");
  129. rs.close();
  130. p.close();
  131. }
  132. catch (SQLException e) {
  133. throw new ServiceException(e.getMessage());
  134. }
  135. return saldo;
  136. }
  137.  
  138. public static void opdaterSaldo(int kontoNr, int nySaldo) {
  139. try {
  140. PreparedStatement p;
  141. p = conn.prepareStatement("UPDATE account SET balance = ? WHERE number = ?");
  142. p.setInt(1, nySaldo);
  143. p.setInt(2, kontoNr);
  144. p.executeUpdate();
  145. p.close();
  146. }
  147. catch (SQLException e) {
  148. throw new ServiceException(e.getMessage());
  149. }
  150. }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement