Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package repository;
  7.  
  8.  
  9. import gui_design_1.Account;
  10. import gui_design_1.Customer;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.Statement;
  15. import java.sql.SQLException;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18. import java.sql.*;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. /**
  23. *
  24. * @author Allan
  25. */
  26. public class Repository
  27. {
  28. com.mysql.jdbc.Connection connection;
  29. com.mysql.jdbc.Statement statement;
  30.  
  31. // URL
  32. String url = "jdbc:mysql://127.0.0.1:3306/banksystem?user=root&password=root";
  33.  
  34.  
  35. public Repository()
  36. {
  37. try
  38. {
  39. connection = (com.mysql.jdbc.Connection)DriverManager.getConnection(url);
  40. statement = (com.mysql.jdbc.Statement) connection.createStatement();
  41. }
  42. catch (SQLException ex)
  43. {
  44. System.out.println(ex.getMessage());
  45. System.out.println("Fel i Connection till Databas");
  46. //Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
  47. }
  48. }
  49.  
  50. //returns list of customer, ersätter allcustomerarraylist
  51. public List<Customer> getAllCustomers( )
  52. {
  53. List <Customer> repositCustList = new ArrayList<>();
  54. try
  55. {
  56. //step 3. execute sql query
  57.  
  58. ResultSet result = statement.executeQuery("SELECT * FROM banksystem.customers");
  59. while(result.next()){
  60. System.out.println(result.getString("customerName") + result.getString("personalNumber"));
  61. String s1= result.getString("customerName");
  62. String s2 = result.getString("personalNumber");
  63. Customer c = new Customer(s1, Long.parseLong(s2));
  64. //repositCustList.clear();
  65. repositCustList.add(c);
  66.  
  67. }
  68. } catch (SQLException ex)
  69. {
  70. Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
  71. }
  72. return repositCustList;
  73. }
  74.  
  75. public boolean addCustomer(String name, long pNr) throws Exception
  76. {
  77. boolean added = true;
  78. try
  79. {
  80. statement.executeUpdate("INSERT into Customers (customerName, personalNumber) VALUES ('" + name + "', " + pNr + ")");
  81. } catch (SQLException ex)
  82. {
  83. added = false;
  84. return added;
  85. //Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
  86. }
  87. return added;
  88. }
  89.  
  90. public int addCreditAccount(long pNr)
  91. {
  92. int accountCounter = 0;
  93.  
  94. try
  95. {
  96. ResultSet result = statement.executeQuery("SELECT count(accountID) FROM accounts");
  97. while (result.next())
  98. accountCounter = result.getInt("count(accountID)") + 1000;
  99.  
  100. String insertSqlAddCreditAcc = " insert into accounts "
  101. + "(accountID, balance, interestRate, accountType, Customers_personalNumber)"
  102. + " values (" + (accountCounter + 1) + ", '0', 5, 'Credit Account', " + pNr + ")";
  103.  
  104. statement.executeUpdate(insertSqlAddCreditAcc);
  105.  
  106. } catch (SQLException ex)
  107. {
  108. Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
  109. }
  110.  
  111. return accountCounter + 1;
  112.  
  113. }
  114.  
  115. public String getAccount(long pNr, int accountId)
  116. {
  117. List <Account> repositAccountList = new ArrayList<>();
  118. String getAccountReturnString = null;
  119.  
  120. try
  121. {
  122. ResultSet result = statement.executeQuery("SELECT * FROM accounts");
  123. while (result.next())
  124. {
  125.  
  126. }
  127. }
  128.  
  129. catch (SQLException ex)
  130. {
  131. Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
  132. }
  133.  
  134. return getAccountReturnString;
  135. }
  136.  
  137.  
  138.  
  139. public static void main( String [] args ){
  140. Repository repo = new Repository();
  141. for(Customer c : repo.getAllCustomers())
  142. System.out.println();
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement