Guest User

Untitled

a guest
Apr 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 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.      * @throws SQLException
  51.      */
  52.     public void siirra(String tililta, String tilille, int maara) throws TilillaEiKatettaPoikkeus, TiliaEiLoytynytPoikkeus, OdottamatonTietokantaPoikkeus, KannassaEiTilejaPoikkeus, SQLException {
  53.         ArrayList<Tili> tilit = haeTilit();
  54.         int saldo1 = tilit.get(0).getSaldo();
  55.         int saldo2 = tilit.get(1).getSaldo();
  56.         String tilinro1 = tilit.get(0).getIban();
  57.         String tilinro2 = tilit.get(1).getIban();
  58.  
  59.         if (tililta.equals(tilinro1)) {
  60.             saldo1 -= maara;
  61.             saldo2 += maara;
  62.         } else {
  63.             saldo1 += maara;
  64.             saldo2 -= maara;
  65.         }
  66.        
  67.         Connection conn = null;
  68.         try {
  69.  
  70.             // driver
  71.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  72.  
  73.             // connection
  74.             conn = getConnection(Connection.TRANSACTION_SERIALIZABLE);
  75.  
  76.             // query
  77.             Statement stmt = conn.createStatement();
  78.             stmt.executeUpdate(String.format("update tilit set saldo=%s where tilinro='%s'", saldo1, tilinro1));
  79.             stmt.executeUpdate(String.format("update tilit set saldo=%s where tilinro='%s'", saldo2, tilinro2));
  80.  
  81.             // end transaction
  82.             conn.commit();
  83.  
  84.             } catch (Exception ex) {
  85.                 ex.printStackTrace();
  86.                 conn.rollback();
  87.  
  88.             } finally {
  89.                 // close connection
  90.                 if (conn != null) {
  91.                     try {
  92.                         conn.close();
  93.                     } catch (Exception ex) {
  94.                         ex.printStackTrace();
  95.                         conn.rollback();
  96.                     }
  97.                 }
  98.             }
  99.     }
  100.  
  101.     /**
  102.      * Hakee kannasta tilitaulun sisällön ArrayList-tyyppiseen kokoelmaan
  103.      * yhdessä transaktiossa.
  104.      *
  105.      * @return kaikki kannan sisältämät tilit yhdessä kokoelmassa
  106.      * @throws OdottamatonTietokantaPoikkeus
  107.      *             jos tapahtuu tietokantavirhe
  108.      * @throws KannassaEiTilejaPoikkeus
  109.      *             jos kannasta ei löydy vähintään kahta tiliä.
  110.      * @throws SQLException
  111.      */
  112.     public ArrayList<Tili> haeTilit() throws OdottamatonTietokantaPoikkeus,
  113.             KannassaEiTilejaPoikkeus, SQLException {
  114.  
  115.         ArrayList<Tili> tilit = new ArrayList<Tili>();
  116.         Connection conn = null;
  117.         try {
  118.  
  119.             // driver
  120.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  121.  
  122.             // connection
  123.             conn = getConnection(Connection.TRANSACTION_READ_COMMITTED);
  124.  
  125.             // query
  126.             Statement stmt = conn.createStatement();
  127.             ResultSet result = stmt
  128.                     .executeQuery("select tilinro, saldo from tilit");
  129.  
  130.             // end transaction
  131.             conn.commit();
  132.  
  133.             // save results
  134.             if (result == null) {
  135.                 throw new KannassaEiTilejaPoikkeus();
  136.  
  137.             } else {
  138.                 while (result.next()) {
  139.                     int saldo = result.getInt("saldo");
  140.                     String iban = result.getString("tilinro");
  141.                     tilit.add(new Tili(saldo, iban));
  142.                 }
  143.             }
  144.         } catch (Exception ex) {
  145.             ex.printStackTrace();
  146.             conn.rollback();
  147.         } finally {
  148.             // close connection
  149.             if (conn != null) {
  150.                 try {
  151.                     conn.close();
  152.                 } catch (Exception ex) {
  153.                     ex.printStackTrace();
  154.                     conn.rollback();
  155.                 }
  156.             }
  157.         }
  158.         return tilit;
  159.     }
  160. }
Add Comment
Please, Sign In to add comment