Guest User

Untitled

a guest
Dec 7th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.83 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.SQLException;
  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. import exceptions.AccountNotFoundException;
  11. import exceptions.NotMuchMoneyException;
  12. import exceptions.OverflowAccountException;
  13.  
  14. public class Bank  {
  15.     private static long last = 0;
  16.     private static String user = "nauru";//Логин пользователя
  17.     private static String password = "17981798";//Пароль пользователя
  18.     private static String url = "jdbc:mysql://localhost:3306/bank";//URL адрес
  19.     private static String driver = "com.mysql.jdbc.Driver";//Имя драйвера
  20.     private static Connection c = null;//Соединение с БД
  21.     private static Statement st;
  22.     static {
  23.          try {
  24.              Class.forName(driver);//Регистрируем драйвер
  25.         } catch (ClassNotFoundException e) {
  26.              // TODO Auto-generated catch block
  27.              e.printStackTrace();
  28.         }
  29.         try{
  30.             c = DriverManager.getConnection(url, user, password);
  31.             st = c.createStatement();      
  32.         } catch (SQLException e) {
  33.             e.printStackTrace();
  34.         }
  35.     }
  36.     public static long createAccount() {
  37.         long res = 0;
  38.        
  39.         try {              
  40.             ResultSet rs = st.executeQuery("select MAX(account) from accounts");//Выполняем запрос
  41.             while (rs.next()) {
  42.                 System.out.println(rs.getString("max(account)"));
  43.                 res = Long.parseLong(rs.getString("max(account)"));
  44.             }
  45.             res++;
  46.             st.execute("INSERT INTO ACCOUNTS VALUES (" + res + "," + 0 +")");          
  47.         } catch (SQLException e) {
  48.             // TODO Auto-generated catch block
  49.             e.printStackTrace();
  50.         }
  51.         return res;
  52.     }
  53.    
  54.     public static long getAmount(long number) throws AccountNotFoundException {
  55.         long result = -1;
  56.         try {
  57.             ResultSet rs = st.executeQuery("select * from accounts where(account = "+number+")");
  58.             if (rs.next()) {
  59.                 result = rs.getLong("amount");
  60.             } else {
  61.                 throw new AccountNotFoundException();
  62.             }
  63.         } catch (SQLException e) {
  64.             // TODO Auto-generated catch block
  65.             e.printStackTrace();
  66.         }
  67.         return result;
  68.     }
  69.    
  70.     public static void deleteAccount(long account) throws AccountNotFoundException {
  71.         try {
  72.             int count = st.executeUpdate("DELETE from accounts where(account="+account+");");
  73.            
  74.             if (count == 0) {
  75.                 throw new AccountNotFoundException();
  76.             }
  77.         } catch (SQLException e) {
  78.             // TODO Auto-generated catch block
  79.             e.printStackTrace();
  80.         }
  81.     }
  82.    
  83.     public static void withdrawAccount(long account, long count) throws AccountNotFoundException, NotMuchMoneyException {
  84.         long amount = Bank.getAmount(account);
  85.         if (amount < count) {
  86.             throw new NotMuchMoneyException();
  87.         }
  88.         amount -= count;
  89.         try {
  90.             st.executeUpdate("UPDATE accounts set amount = "+ amount + " where (account = "+ account+")");
  91.         } catch (SQLException e) {
  92.             // TODO Auto-generated catch block
  93.             e.printStackTrace();
  94.         }
  95.     }
  96.    
  97.     public static void addMoneyToAccount(long account, long count) throws AccountNotFoundException, OverflowAccountException {
  98.         try {
  99.             int res = st.executeUpdate("Update accounts set amount = amount + "+ count +
  100.                     " where (account = "+ account +")");
  101.             if (res == 0) {
  102.                 throw new AccountNotFoundException();
  103.             }
  104.         } catch (SQLException e) {
  105.             if (e.getMessage().contains("Data truncation: Out of range value for column")) {
  106.                 throw new OverflowAccountException();
  107.             }
  108.             e.printStackTrace();
  109.         }
  110.     }
  111.    
  112.     public static ArrayList<Long> getListAccount() {
  113.         ArrayList<Long> result = new ArrayList<Long>();
  114.         try {
  115.             ResultSet rs = st.executeQuery("select * from accounts where (account > 0)");
  116.             while (rs.next()) {
  117.                 result.add(rs.getLong("account"));
  118.                 result.add(rs.getLong("amount"));
  119.             }
  120.         } catch (SQLException e) {
  121.             // TODO Auto-generated catch block
  122.             e.printStackTrace();
  123.         }
  124.         return result;
  125.     }
  126.    
  127.     public static void transfer(long from, long to, long money) throws AccountNotFoundException, NotMuchMoneyException, OverflowAccountException {
  128.         long amountFrom = Bank.getAmount(from);
  129.         long amountTo = Bank.getAmount(to);
  130.         if (amountFrom < money) {
  131.             throw new NotMuchMoneyException();
  132.         }
  133.         try {
  134.             st.executeUpdate("Update accounts set amount = amount + "+ money +
  135.                     " where (account = "+ to +")");
  136.             st.executeUpdate("Update accounts set amount = amount - " + money +
  137.                     " where (account = "+ from +")");
  138.            
  139.         } catch (SQLException e) {
  140.             if (e.getMessage().contains("Data truncation: Out of range value for column")) {
  141.                 throw new OverflowAccountException();
  142.             }
  143.             e.printStackTrace();
  144.         }      
  145.     }
  146.    
  147.     protected  void finalize() {
  148.         try {
  149.             c.close();
  150.         } catch (SQLException e) {
  151.             // TODO Auto-generated catch block
  152.             e.printStackTrace();
  153.         }
  154.     }
  155.    
  156.    
  157.    
  158.    
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment