Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: bilal baig
Added: Dec 18, 2017 3:23 PM
Views: 29
Tags: no tags
  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 dom;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.ResultSet;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.ArrayList;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author Bilal
  20.  */
  21. public class DataBaseManager {
  22.  
  23.     //---Declaration------------------------------------------------
  24.     String query = null;
  25.     Connection conn = null;
  26.     Statement statement = null;
  27.     ResultSet rs = null;
  28.  
  29.     //---Initialization---------------------------------------------
  30.     private Connection getConnection() throws SQLException {
  31.         Connection conn = null;
  32.         conn = DriverManager.getConnection("jdbc:ucanaccess://C:/Users/Bilal/Desktop/testCode.accdb");
  33.         return conn;
  34.     }
  35.  
  36.     @SuppressWarnings("empty-statement")
  37.     public void insertUser(User u) throws SQLException {
  38.  
  39.         try {
  40.             conn = getConnection();
  41.             statement = conn.createStatement();
  42.             query = "INSERT INTO User (Hash,Name,AccountNumber,Amount,coin,PrivateKey,PublicKey,DigitalSignature, CurrencyType) VALUES ('" + u.getHash() + "','" + u.getName() + "','" + u.getAccountNumber() + "','" + u.getAmount() + "','" + u.getCoin() + "','" + u.getPrivateKey() + "','" + u.getPublicKey() + "','" + u.getDigitalSignature() + "','" + u.getCurrencyType() + "')";
  43.             statement.executeUpdate(query);
  44.         } catch (SQLException ex) {
  45.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  46.         } finally {
  47.             try {
  48.                 statement.close();
  49.                 conn.close();
  50.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  51.             };
  52.         }
  53.     }
  54.  
  55.     public User getUserByHash(String hash) {
  56.         User u = new User();
  57.         try {
  58.             conn = getConnection();
  59.             statement = conn.createStatement();
  60.             query = "SELECT * FROM User WHERE Hash=" + hash;
  61.             rs = statement.executeQuery(query);
  62.             while (rs.next()) {
  63.                 u.setAccountNumber(rs.getString("AccountNumber"));
  64.                 u.setAmount(rs.getDouble("Amount"));
  65.                 u.setCoin(rs.getDouble("Coin"));
  66.                 u.setDigitalSignature(rs.getString("DigitalSignature"));
  67.                 u.setHash(rs.getString("Hash"));
  68.                 u.setName(rs.getString("Name"));
  69.                 u.setPrivateKey(rs.getString("PrivateKey"));
  70.                 u.setPublicKey(rs.getString("PublicKey"));
  71.             }
  72.  
  73.         } catch (SQLException ex) {
  74.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  75.         } finally {
  76.             try {
  77.                 statement.close();
  78.                 conn.close();
  79.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  80.             };
  81.         }
  82.         return u;
  83.     }
  84.  
  85.     public ArrayList<User> getAllUsers() throws SQLException {
  86.         conn = getConnection();
  87.         statement = conn.createStatement();
  88.         query = "SELECT * FROM User;";
  89.         rs = statement.executeQuery(query);
  90.  
  91.         ArrayList<User> uList = new ArrayList<>();
  92.         while (rs.next()) {
  93.             User u = new User();
  94.             u.setAccountNumber(rs.getString("AccountNumber"));
  95.             u.setAmount(rs.getDouble("Amount"));
  96.             u.setCoin(rs.getDouble("Coin"));
  97.             u.setDigitalSignature(rs.getString("DigitalSignature"));
  98.             u.setHash(rs.getString("Hash"));
  99.             u.setName(rs.getString("Name"));
  100.             u.setPrivateKey(rs.getString("PrivateKey"));
  101.             u.setPublicKey(rs.getString("PublicKey"));
  102.             u.setCurrencyType(rs.getString("CurrencyType"));
  103.             uList.add(u);
  104.         }
  105.         rs.close();
  106.         statement.close();
  107.         conn.close();
  108.  
  109.         return uList;
  110.     }
  111.  
  112.     public void updateAccountNumber(User u) {
  113.         try {
  114.             conn = getConnection();
  115.             statement = conn.createStatement();
  116.             query = "UPDATE User SET AccountNumber='" + u.getAccountNumber() + "' WHERE Hash ='" + u.getHash() + "'";
  117.             int c = statement.executeUpdate(query);
  118.             //System.out.println(c);
  119.         } catch (SQLException ex) {
  120.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  121.         } finally {
  122.             try {
  123.                 statement.close();
  124.                 conn.close();
  125.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  126.             };
  127.         }
  128.     }
  129.  
  130.     public void updateAccountAmount(User u) {
  131.         try {
  132.             conn = getConnection();
  133.             statement = conn.createStatement();
  134.             query = "UPDATE User SET Amount='" + u.getAmount() + "' WHERE Hash ='" + u.getHash() + "'";
  135.             int c = statement.executeUpdate(query);
  136.             //System.out.println(c);
  137.         } catch (SQLException ex) {
  138.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  139.         } finally {
  140.             try {
  141.                 statement.close();
  142.                 conn.close();
  143.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  144.             };
  145.         }
  146.     }
  147.  
  148.     public void updateAccountCoin(User u) {
  149.         try {
  150.             conn = getConnection();
  151.             statement = conn.createStatement();
  152.             query = "UPDATE User SET coin='" + u.getCoin() + "' WHERE Hash ='" + u.getHash() + "'";
  153.             int c = statement.executeUpdate(query);
  154.             //System.out.println(c);
  155.         } catch (SQLException ex) {
  156.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  157.         } finally {
  158.             try {
  159.                 statement.close();
  160.                 conn.close();
  161.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  162.             };
  163.         }
  164.     }
  165.  
  166.     public void insertInLedger(String leadger) {
  167.         try {
  168.             conn = getConnection();
  169.             statement = conn.createStatement();
  170.             query = "INSERT INTO Ledger (Transfer) VALUES" + "('" + leadger + "')";
  171.             statement.executeUpdate(query);
  172.         } catch (SQLException ex) {
  173.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  174.         } finally {
  175.             try {
  176.                 statement.close();
  177.                 conn.close();
  178.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  179.             };
  180.         }
  181.  
  182.     }
  183.  
  184.     public ArrayList<String> getFromLedger() throws SQLException {
  185.         conn = getConnection();
  186.         statement = conn.createStatement();
  187.         query = "SELECT * FROM Ledger;";
  188.         rs = statement.executeQuery(query);
  189.  
  190.         ArrayList<String> L = new ArrayList<>();
  191.         while (rs.next()) {
  192.             User u = new User();
  193.             String str;
  194.             str = (rs.getString("Transfer"));
  195.             L.add(str);
  196.         }
  197.         rs.close();
  198.         statement.close();
  199.         conn.close();
  200.  
  201.         return L;
  202.     }
  203.  
  204.     public void insertDistributor(Distributor dis) throws SQLException {
  205.  
  206.         try {
  207.             conn = getConnection();
  208.             statement = conn.createStatement();
  209.             query = "INSERT INTO Distributor (Hash,distributorName,coins,PrivateKey,PublicKey,DigitalSignature) VALUES ('" + dis.getHash() + "','" + dis.getName() + "','" + dis.getCoin() + "','" + dis.getPrivateKey() + "','" + dis.getPublicKey() + "','" + dis.getDigitalSignature() + "')";
  210.             statement.executeUpdate(query);
  211.         } catch (SQLException ex) {
  212.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  213.         } finally {
  214.             try {
  215.                 statement.close();
  216.                 conn.close();
  217.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  218.             };
  219.         }
  220.     }
  221.  
  222.     /**
  223.      *
  224.      * @param hash
  225.      * @return
  226.      * @throws java.sql.SQLException
  227.      */
  228.     public Distributor getDistributor(String hash) throws SQLException {
  229.  
  230.         Distributor d=new Distributor();
  231.         try {
  232.             conn = getConnection();
  233.             statement = conn.createStatement();
  234.             query = "SELECT * FROM Distributor WHERE Hash=" + "'"+hash+"'";
  235.             rs = statement.executeQuery(query);
  236.             while (rs.next()) {
  237.                
  238.                 d.setCoin(rs.getDouble("Coins"));
  239.                 d.setDigitalSignature(rs.getString("DigitalSignature"));
  240.                 d.setHash(rs.getString("Hash"));
  241.                 d.setName(rs.getString("distributorName"));
  242.                 d.setPrivateKey(rs.getString("PrivateKey"));
  243.                 d.setPublicKey(rs.getString("PublicKey"));
  244.             }
  245.  
  246.         } catch (SQLException ex) {
  247.             Logger.getLogger(DataBaseManager.class.getName()).log(Level.SEVERE, null, ex);
  248.         } finally {
  249.             try {
  250.                 statement.close();
  251.                 conn.close();
  252.             } catch (SQLException ex) {/*IGNORE EXCEPTION*/
  253.             };
  254.         }
  255.         return d;
  256.     }
  257. }
  258.