Guest User

Untitled

a guest
Apr 13th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. package pankki;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9.  
  10. /**
  11.  * Tämä luokka hoitaa tietokannan käsittelyn
  12.  */
  13. public class DAO {
  14.  
  15.     public Connection getConnection(int isolationLevel) {
  16.         Connection conn = null;
  17.  
  18.         String username = "a1000104";
  19.         String password = "xiku72";
  20.         String url = "jdbc:mysql://localhost:3306/a1000104";
  21.  
  22.         // connection
  23.         try {
  24.             conn = DriverManager.getConnection(url, username, password);
  25.             conn.setAutoCommit(false);
  26.             conn.setTransactionIsolation(isolationLevel);
  27.         } catch (SQLException e) {
  28.             e.printStackTrace();
  29.         }
  30.         return conn;
  31.     }
  32.  
  33.     /**
  34.      * Siirtää rahaa tietokannassa tililtä toiselle yhdessä transaktiossa.
  35.      *
  36.      * @param tililta
  37.      *            Miltä tililtä raha otetaan
  38.      * @param tilille
  39.      *            Mille tilille raha siirretään
  40.      * @param maara
  41.      *            Kuinka paljon rahaa siirretään
  42.      * @throws TilillaEiKatettaPoikkeus
  43.      *             jos kate ei riitä tilisiirtoon (tilin saldo ei saa mennä
  44.      *             negatiiviseksi)
  45.      * @throws TiliaEiLoytynytPoikkeus
  46.      *             jos jompaa kumpaa annetuista tileistä ei löydy tietokannasta
  47.      * @throws OdottamatonTietokantaPoikkeus
  48.      *             jos tietokannassa tapahtuu virhe
  49.      * @throws KannassaEiTilejaPoikkeus
  50.      */
  51.     public void siirra(String tililta, String tilille, int maara) throws TilillaEiKatettaPoikkeus, TiliaEiLoytynytPoikkeus, OdottamatonTietokantaPoikkeus, KannassaEiTilejaPoikkeus {  
  52.         ArrayList<Tili> tilit = haeTilit();
  53.         int saldo1 = tilit.get(0).getSaldo();
  54.         int saldo2 = tilit.get(1).getSaldo();
  55.         String tilinro1 = tilit.get(0).getIban();
  56.         String tilinro2 = tilit.get(1).getIban();
  57.  
  58.         if (tililta.equals(tilinro1)) {
  59.             saldo1 -= maara;
  60.             saldo2 += maara;
  61.         } else {
  62.             saldo1 += maara;
  63.             saldo2 -= maara;
  64.         }
  65.        
  66.         Connection conn = null;
  67.         try {
  68.  
  69.             // driver
  70.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  71.  
  72.             // connection
  73.             conn = getConnection(Connection.TRANSACTION_SERIALIZABLE);
  74.  
  75.             // query
  76.             Statement stmt = conn.createStatement();
  77.             stmt.executeUpdate(String.format("update tilit set saldo=%s where tilinro='%s'", saldo1, tilinro1));
  78.             stmt.executeUpdate(String.format("update tilit set saldo=%s where tilinro='%s'", saldo2, tilinro2));
  79.  
  80.             // end transaction
  81.             conn.commit();
  82.  
  83.             } catch (Exception ex) {
  84.                 ex.printStackTrace();
  85.  
  86.             } finally {
  87.                 // close connection
  88.                 if (conn != null) {
  89.                     try {
  90.                         conn.close();
  91.                     } catch (Exception ex) {
  92.                         ex.printStackTrace();
  93.                     }
  94.                 }
  95.             }
  96.     }
  97.  
  98.     /**
  99.      * Hakee kannasta tilitaulun sisällön ArrayList-tyyppiseen kokoelmaan
  100.      * yhdessä transaktiossa.
  101.      *
  102.      * @return kaikki kannan sisältämät tilit yhdessä kokoelmassa
  103.      * @throws OdottamatonTietokantaPoikkeus
  104.      *             jos tapahtuu tietokantavirhe
  105.      * @throws KannassaEiTilejaPoikkeus
  106.      *             jos kannasta ei löydy vähintään kahta tiliä.
  107.      */
  108.     public ArrayList<Tili> haeTilit() throws OdottamatonTietokantaPoikkeus,
  109.             KannassaEiTilejaPoikkeus {
  110.  
  111.         ArrayList<Tili> tilit = new ArrayList<Tili>();
  112.         Connection conn = null;
  113.         try {
  114.  
  115.             // driver
  116.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  117.  
  118.             // connection
  119.             conn = getConnection(Connection.TRANSACTION_READ_COMMITTED);
  120.  
  121.             // query
  122.             Statement stmt = conn.createStatement();
  123.             ResultSet result = stmt
  124.                     .executeQuery("select tilinro, saldo from tilit");
  125.  
  126.             // end transaction
  127.             conn.commit();
  128.  
  129.             // save results
  130.             if (result == null) {
  131.                 throw new KannassaEiTilejaPoikkeus();
  132.  
  133.             } else {
  134.                 while (result.next()) {
  135.                     int saldo = result.getInt("saldo");
  136.                     String iban = result.getString("tilinro");
  137.                     tilit.add(new Tili(saldo, iban));
  138.                 }
  139.             }
  140.         } catch (Exception ex) {
  141.             ex.printStackTrace();
  142.         } finally {
  143.             // close connection
  144.             if (conn != null) {
  145.                 try {
  146.                     conn.close();
  147.                 } catch (Exception ex) {
  148.                     ex.printStackTrace();
  149.                 }
  150.             }
  151.         }
  152.         return tilit;
  153.     }
  154. }
Add Comment
Please, Sign In to add comment