Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pankki;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- /**
- * Tämä luokka hoitaa tietokannan käsittelyn
- */
- public class DAO {
- public Connection getConnection(int isolationLevel) {
- Connection conn = null;
- String username = "a1000104";
- String password = "xiku72";
- String url = "jdbc:mysql://localhost:3306/a1000104";
- // connection
- try {
- conn = DriverManager.getConnection(url, username, password);
- conn.setAutoCommit(false);
- conn.setTransactionIsolation(isolationLevel);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
- /**
- * Siirtää rahaa tietokannassa tililtä toiselle yhdessä transaktiossa.
- *
- * @param tililta
- * Miltä tililtä raha otetaan
- * @param tilille
- * Mille tilille raha siirretään
- * @param maara
- * Kuinka paljon rahaa siirretään
- * @throws TilillaEiKatettaPoikkeus
- * jos kate ei riitä tilisiirtoon (tilin saldo ei saa mennä
- * negatiiviseksi)
- * @throws TiliaEiLoytynytPoikkeus
- * jos jompaa kumpaa annetuista tileistä ei löydy tietokannasta
- * @throws OdottamatonTietokantaPoikkeus
- * jos tietokannassa tapahtuu virhe
- * @throws KannassaEiTilejaPoikkeus
- */
- public void siirra(String tililta, String tilille, int maara) throws TilillaEiKatettaPoikkeus, TiliaEiLoytynytPoikkeus, OdottamatonTietokantaPoikkeus, KannassaEiTilejaPoikkeus {
- ArrayList<Tili> tilit = haeTilit();
- int saldo1 = tilit.get(0).getSaldo();
- int saldo2 = tilit.get(1).getSaldo();
- String tilinro1 = tilit.get(0).getIban();
- String tilinro2 = tilit.get(1).getIban();
- if (tililta.equals(tilinro1)) {
- saldo1 -= maara;
- saldo2 += maara;
- } else {
- saldo1 += maara;
- saldo2 -= maara;
- }
- Connection conn = null;
- try {
- // driver
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- // connection
- conn = getConnection(Connection.TRANSACTION_SERIALIZABLE);
- // query
- Statement stmt = conn.createStatement();
- stmt.executeUpdate(String.format("update tilit set saldo=%s where tilinro='%s'", saldo1, tilinro1));
- stmt.executeUpdate(String.format("update tilit set saldo=%s where tilinro='%s'", saldo2, tilinro2));
- // end transaction
- conn.commit();
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- // close connection
- if (conn != null) {
- try {
- conn.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- }
- /**
- * Hakee kannasta tilitaulun sisällön ArrayList-tyyppiseen kokoelmaan
- * yhdessä transaktiossa.
- *
- * @return kaikki kannan sisältämät tilit yhdessä kokoelmassa
- * @throws OdottamatonTietokantaPoikkeus
- * jos tapahtuu tietokantavirhe
- * @throws KannassaEiTilejaPoikkeus
- * jos kannasta ei löydy vähintään kahta tiliä.
- */
- public ArrayList<Tili> haeTilit() throws OdottamatonTietokantaPoikkeus,
- KannassaEiTilejaPoikkeus {
- ArrayList<Tili> tilit = new ArrayList<Tili>();
- Connection conn = null;
- try {
- // driver
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- // connection
- conn = getConnection(Connection.TRANSACTION_READ_COMMITTED);
- // query
- Statement stmt = conn.createStatement();
- ResultSet result = stmt
- .executeQuery("select tilinro, saldo from tilit");
- // end transaction
- conn.commit();
- // save results
- if (result == null) {
- throw new KannassaEiTilejaPoikkeus();
- } else {
- while (result.next()) {
- int saldo = result.getInt("saldo");
- String iban = result.getString("tilinro");
- tilit.add(new Tili(saldo, iban));
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- } finally {
- // close connection
- if (conn != null) {
- try {
- conn.close();
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
- return tilit;
- }
- }
Add Comment
Please, Sign In to add comment