Advertisement
Guest User

Untitled

a guest
Jan 8th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.73 KB | None | 0 0
  1. package database;
  2.  
  3. import account.Account;
  4. import account.ReceiveBlock;
  5. import constants.Constants;
  6.  
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11. import java.util.Scanner;
  12.  
  13. public class Database {
  14. public static String driver = "oracle.jdbc.driver.OracleDriver";
  15. public static String hostname = "localhost";
  16. public static String dbName = "orcl";
  17. public static String url = "jdbc:oracle:thin:@" + hostname + ":1521:" + dbName;
  18. public static String user = "infamia";
  19. public static String password = "****";
  20.  
  21. static {
  22. if(!loadDriver())
  23. throw new RuntimeException("cannot load driver");
  24. }
  25.  
  26. public static void main(String[] args) throws SQLException {
  27.  
  28. try {
  29. Connection connection = connect();
  30. Statement statement = createStatement(connection);
  31. Scanner in = new Scanner(System.in);
  32. System.out.println("[I]nit/[D]rop?");
  33. String decision = in.nextLine();
  34. if(decision.toLowerCase().equals("d")) {
  35. System.out.println("DROP");
  36. InitDatabase.dropTriggers(statement);
  37. //InitDatabase.dropConstraints(statement);
  38. InitDatabase.dropSchema(statement);
  39. InitDatabase.dropSequences(statement);
  40. InitDatabase.dropProcedures(statement);
  41. InitDatabase.dropFunctions(statement);
  42. }else {
  43. System.out.println("INIT");
  44. InitDatabase.createSchema(statement);
  45. //InitDatabase.createConstraints(statement);
  46. InitDatabase.createSequences(statement);
  47. InitDatabase.createTriggers(statement);
  48. InitDatabase.createProcedures(statement);
  49. InitDatabase.createFuntions(statement);
  50. }
  51. closeConnection(connection, statement);
  52.  
  53. }catch (SQLException e) {
  54. e.printStackTrace();
  55. System.out.println("error");
  56. }
  57. }
  58.  
  59. public static boolean loadDriver() {
  60. try {
  61. Class.forName(driver);
  62. return true;
  63. } catch (ClassNotFoundException e) {
  64. return false;
  65. }
  66. }
  67.  
  68.  
  69. public static Connection connect() {
  70. try {
  71. return DriverManager.getConnection(url, user, password);
  72. }catch (SQLException e) {
  73. return null;
  74. }
  75. }
  76.  
  77. public static void closeConnection(Connection connection, Statement s){
  78. try {
  79. if(!s.isClosed())
  80. s.close();
  81. if(!connection.isClosed())
  82. connection.close();
  83. }catch (SQLException e) {
  84. throw new RuntimeException();
  85. }
  86. }
  87.  
  88. public static Statement createStatement(Connection connection) {
  89. try {
  90. return connection.createStatement();
  91. } catch (SQLException e) {
  92. return null;
  93. }
  94. }
  95.  
  96. public static boolean createAccount(Connection con, String publicKey, String hash,
  97. String signature) throws SQLException {
  98. String sql = "{? = call CREATE_ACCOUNT(?, ?, ?, ?, ?)}";
  99. CallableStatement cs = con.prepareCall(sql);
  100.  
  101. cs.registerOutParameter(1, Types.INTEGER);
  102. cs.setString(2, publicKey);
  103. cs.setInt(3, Constants.INITIAL_BALANCE);
  104. cs.setString(4, hash);
  105. cs.setString(5, Constants.GENESIS_PREV_HASH);
  106. cs.setString(6, signature);
  107. cs.execute();
  108. int result = cs.getInt(1);
  109.  
  110. cs.close();
  111. return result == 1;
  112. }
  113.  
  114. public static int getBalance(Connection con, String publicKey) throws SQLException {
  115. System.out.println(publicKey);
  116. String sql = "{? = call GET_BALANCE(?)}";
  117. CallableStatement cs = con.prepareCall(sql);
  118.  
  119. cs.registerOutParameter(1, Types.INTEGER);
  120. cs.setString(2, publicKey);
  121. cs.execute();
  122. int result = cs.getInt(1);
  123.  
  124. cs.close();
  125. return result;
  126. }
  127.  
  128. public static String getPreviousHash(Connection con, String publicKey) throws SQLException {
  129. String sql = "{? = call GET_PREVIOUS_HASH(?)}";
  130. CallableStatement cs = con.prepareCall(sql);
  131.  
  132. cs.registerOutParameter(1, Types.VARCHAR);
  133. cs.setString(2, publicKey);
  134. cs.execute();
  135.  
  136. String previousHash = cs.getString(1);
  137. cs.close();
  138. return previousHash;
  139. }
  140.  
  141. public static void performTransaction(Connection con, String sender, String recipient, int amount, String signature,
  142. String senderHash, String recipientHash) throws SQLException {
  143. String sql = "{call PERFORM_TRANSACTION(?, ?, ?, ?, ?, ?, ?)}";
  144. CallableStatement cs = con.prepareCall(sql);
  145.  
  146. cs.setString(1, sender);
  147. cs.setString(2, recipient);
  148. cs.setInt(3, amount);
  149. cs.setString(4, signature);
  150. cs.setString(5, signature);
  151. cs.setString(6, senderHash);
  152. cs.setString(7, recipientHash);
  153. cs.execute();
  154.  
  155. cs.close();
  156. }
  157.  
  158. public static boolean accountExists(Connection con, String address) {
  159. try {
  160. String sql = "{? = call ACCOUNT_VERIFY(?)}";
  161. CallableStatement cStatement = con.prepareCall(sql);
  162. cStatement.registerOutParameter(1, Types.INTEGER);
  163. cStatement.setString(2, address);
  164. cStatement.execute();
  165. int result = cStatement.getInt(1);
  166.  
  167. cStatement.close();
  168. return result == 0;
  169. } catch (SQLException e) {
  170. e.printStackTrace();
  171. throw new RuntimeException();
  172. }
  173. }
  174. public static List<AccountList> GetAccounts(Connection con) throws SQLException {
  175. List<AccountList> AccountData=new ArrayList<>();
  176. Statement st = con.createStatement();
  177. System.out.println("getAccountlist");
  178. String sql = ("SELECT * FROM ACCOUNT");
  179. ResultSet rs = st.executeQuery(sql);
  180. while(rs.next()) {
  181. String str1 = rs.getString("PUBLIC_KEY");
  182. int id = rs.getInt("BLOCKCHAIN");
  183. int Company_Type =rs.getInt("COMPANY_TYPE");
  184. int Customer_Type= rs.getInt("CUSTOMER_TYPE");
  185. AccountData.add(new AccountList(str1,id,Company_Type,Customer_Type));
  186. System.out.println(id);
  187. }
  188.  
  189. return AccountData;
  190. }
  191. public static List<ReceiveBlockData> GetReciveBlocks(Connection con) throws SQLException {
  192. List<ReceiveBlockData> AccountData=new ArrayList<>();
  193. Statement st = con.createStatement();
  194. System.out.println("GetReceiveBlocks");
  195. String sql = ("SELECT * FROM RECEIVE_BLOCK");
  196. ResultSet rs = st.executeQuery(sql);
  197.  
  198. while(rs.next()) {
  199. String str1 = rs.getString("SENDER");
  200. int id = rs.getInt("ID");
  201.  
  202. AccountData.add(new ReceiveBlockData(id,str1));
  203. System.out.println(id+" "+str1);
  204. }
  205.  
  206. return AccountData;
  207. }
  208. public static List<SendBlockData> GetSendBlocks(Connection con) throws SQLException {
  209. List<SendBlockData> AccountData=new ArrayList<>();
  210. Statement st = con.createStatement();
  211. System.out.println("SendBlockData");
  212. String sql = ("SELECT * FROM SEND_BLOCK");
  213. ResultSet rs = st.executeQuery(sql);
  214.  
  215. while(rs.next()) {
  216. String str1 = rs.getString("RECIPIENT");
  217. int id = rs.getInt("ID");
  218.  
  219. AccountData.add(new SendBlockData(id,str1));
  220. System.out.println(id+" "+str1);
  221. }
  222.  
  223. return AccountData;
  224. }
  225.  
  226. public static List<BlockchainData> GetBlockchain(Connection con) throws SQLException {
  227. List<BlockchainData> AccountData=new ArrayList<>();
  228. Statement st = con.createStatement();
  229. System.out.println("GetBlockchain");
  230. String sql = ("SELECT * FROM BLOCKCHAIN");
  231. ResultSet rs = st.executeQuery(sql);
  232.  
  233. while(rs.next()) {
  234.  
  235. int id = rs.getInt("BLOCKCHAIN_ID");
  236.  
  237. AccountData.add(new BlockchainData(id));
  238. System.out.println(id);
  239. }
  240.  
  241. return AccountData;
  242. }
  243. public static List<AddressData> GetAddressData(Connection con) throws SQLException {
  244. List<AddressData> AddressData=new ArrayList<>();
  245. Statement st = con.createStatement();
  246. System.out.println("GetAddressData");
  247. String sql = ("SELECT * FROM Address");
  248. ResultSet rs = st.executeQuery(sql);
  249.  
  250. while(rs.next()) {
  251.  
  252. int Address_ID= rs.getInt("ADDRESS_ID");
  253. String Country=rs.getString("COUNTRY");
  254. String Postal_code=rs.getString("POSTAL_CODE");
  255. String City=rs.getString("CITY");
  256. String Street=rs.getString("STREET");
  257. String Apartament_number=rs.getString("APARTMENT_NUMBER");
  258.  
  259. AddressData.add(new AddressData(Address_ID,Country,Postal_code,City,Street,Apartament_number));
  260. }
  261.  
  262. return AddressData;
  263. }
  264. public static List<Company> GetCompany(Connection con) throws SQLException {
  265. List<Company> Company=new ArrayList<>();
  266. Statement st = con.createStatement();
  267. System.out.println("GetAddressData");
  268. String sql = ("SELECT * FROM COMPANY");
  269. ResultSet rs = st.executeQuery(sql);
  270.  
  271. while(rs.next()) {
  272.  
  273. int Company_id=rs.getInt("COMPANY_ID");
  274. String Company_name=rs.getString("COMPANY_NAME");
  275. String Sector=rs.getString("SECTOR");
  276. String Contact_tel=rs.getString("CONTACT_TEL");
  277. int Address_id=rs.getInt("ADDRESS_ID");
  278. String Contact_email=rs.getString("CONTACT_EMAIL");
  279.  
  280. Company.add(new Company(Company_id,Company_name,Sector,Contact_tel,Address_id,Contact_email));
  281. }
  282.  
  283. return Company;
  284. }
  285. public static List<Customer> GetCustomer(Connection con) throws SQLException {
  286. List<Customer> Customer=new ArrayList<>();
  287. Statement st = con.createStatement();
  288. System.out.println("GetAddressData");
  289. String sql = ("SELECT * FROM CUSTOMER");
  290. ResultSet rs = st.executeQuery(sql);
  291.  
  292. while(rs.next()) {
  293.  
  294. int Customer_id=rs.getInt("CUSTOMER_ID");
  295. int Company_id=rs.getInt("COMPANY_ID");
  296. int Address_id=rs.getInt("ADDRESS_ID");
  297. String First_name=rs.getString("FIRST_NAME");
  298. String Last_name=rs.getString("LAST_NAME");
  299. String Contact_email=rs.getString("CONTACT_EMAIL");
  300.  
  301. Customer.add(new Customer(Customer_id,Company_id,Address_id,First_name,Last_name,Contact_email));
  302. }
  303.  
  304. return Customer;
  305. }
  306. public static List<BlockData> GetBlocks(Connection con) throws SQLException {
  307. List<BlockData> AccountData=new ArrayList<>();
  308. Statement st = con.createStatement();
  309. System.out.println("GetBlockchain");
  310. String sql = ("SELECT * FROM BLOCK ORDER BY BLOCK_ID");
  311. ResultSet rs = st.executeQuery(sql);
  312.  
  313. while(rs.next()) {
  314. int Block_ID = rs.getInt("BLOCK_ID");
  315. int Previous_Block = rs.getInt("PREVIOUS_BLOCK");
  316. int Blockchain_ID = rs.getInt("BLOCKCHAIN_ID");
  317. String Signature =rs.getString("SIGNATURE");
  318. String Hash_Code = rs.getString("HASH_CODE");
  319. String Previous_Hash_Code =rs.getString("PREVIOUS_HASH_CODE");
  320. int Amount = rs.getInt("AMOUNT");
  321. int Receive_Type = rs.getInt("RECEIVE_TYPE");
  322. int Send_Type = rs.getInt("SEND_TYPE");
  323. Date Transaction_time = rs.getDate("TRANSACTION_TIME");
  324.  
  325.  
  326. AccountData.add(new BlockData(Block_ID,Blockchain_ID,Previous_Block,Signature,Hash_Code,Previous_Hash_Code,Amount,Receive_Type,Send_Type,Transaction_time));
  327.  
  328. }
  329.  
  330. return AccountData;
  331. }
  332. public static void InsertBlocks(Connection con,BlockData Account) throws SQLException {
  333.  
  334. String sql = ("INSERT INTO BLOCK(Block_ID,BLOCKCHAIN_ID,PREVIOUS_BLOCK,SIGNATURE,HASH_CODE,PREVIOUS_HASH_CODE,AMOUNT,RECEIVE_TYPE,SEND_TYPE,TRANSACTION_TIME) VALUES(?,?,?,?,?,?,?,?,?,?)");
  335. PreparedStatement pstmt = con.prepareStatement(sql);
  336. pstmt.setString(4,Account.Signature);
  337. pstmt.setString(5,Account.Hash_Code);
  338. if(Account.Previous_Hash_Code!=null){
  339. pstmt.setString(6,Account.Previous_Hash_Code);}
  340. else
  341. {
  342. pstmt.setString(6,null);
  343. }
  344. pstmt.setInt(1,Account.Block_ID);
  345. pstmt.setInt(2,Account.Blockchain_ID);
  346. if(Account.Previous_Block!=0){
  347. pstmt.setInt(3,Account.Previous_Block);}
  348. else
  349. {
  350. pstmt.setObject(3,null);
  351. }
  352. pstmt.setInt(7,Account.Amount);
  353. if(Account.Receive_Type!=0) {
  354. pstmt.setInt(8, Account.Receive_Type);
  355. }
  356. else
  357. {
  358. pstmt.setObject(8,null);
  359. }
  360. if(Account.Send_Type!=0){
  361. pstmt.setInt(9,Account.Send_Type);}
  362. else {
  363.  
  364. pstmt.setObject(9,null);
  365. }
  366. if (Account.Transaction_time!=null)
  367. {
  368. pstmt.setDate(10,Account.Transaction_time);
  369. }else
  370.  
  371. {
  372. pstmt.setObject(10,null);
  373. }
  374.  
  375.  
  376. pstmt.executeUpdate();
  377.  
  378.  
  379. }
  380. public static void InsertBlockchain(Connection con,BlockchainData Account) throws SQLException {
  381.  
  382. String sql = ("INSERT INTO BLOCKCHAIN (BLOCKCHAIN_ID,LAST_BLOCK) VALUES(?,?)");
  383. PreparedStatement pstmt = con.prepareStatement(sql);
  384. pstmt.setString(2,null);
  385. pstmt.setInt(1,Account.id);
  386. pstmt.executeUpdate();
  387.  
  388.  
  389. }
  390. public static void InsertAddress(Connection con,AddressData Account) throws SQLException {
  391.  
  392. String sql = ("INSERT INTO ADDRESS (ADDRESS_ID,COUNTRY,POSTAL_CODE,CITY,STREET,APARTMENT_NUMBER) VALUES(?,?,?,?,?,?)");
  393. PreparedStatement pstmt = con.prepareStatement(sql);
  394. pstmt.setInt(1,Account.Address_ID);
  395. pstmt.setString(2,Account.Country);
  396. pstmt.setString(3,Account.Postal_code);
  397. pstmt.setString(4,Account.City);
  398. pstmt.setString(5,Account.Street);
  399. pstmt.setString(6,Account.Apartament_number);
  400.  
  401. pstmt.executeUpdate();
  402.  
  403.  
  404. }
  405. public static void InsertCompany(Connection con,Company Account) throws SQLException {
  406.  
  407. String sql = ("INSERT INTO COMPANY (COMPANY_ID,COMPANY_NAME,SECTOR,CONTACT_TEL,ADDRESS_ID,CONTACT_EMAIL) VALUES(?,?,?,?,?,?)");
  408. PreparedStatement pstmt = con.prepareStatement(sql);
  409. pstmt.setInt(1,Account.Company_id);
  410. pstmt.setString(2,Account.Company_name);
  411. pstmt.setString(3,Account.Sector);
  412. pstmt.setString(4,Account.Contact_tel);
  413. pstmt.setInt(5,Account.Address_id);
  414. pstmt.setString(6,Account.Contact_email);
  415.  
  416. pstmt.executeUpdate();
  417.  
  418.  
  419. }
  420. public static void InsertCustomer(Connection con,Customer Account) throws SQLException {
  421.  
  422. String sql = ("INSERT INTO Customer (CUSTOMER_ID,COMPANY_ID,ADDRESS_ID,FIRST_NAME,LAST_NAME,CONTACT_EMAIL) VALUES(?,?,?,?,?,?)");
  423. PreparedStatement pstmt = con.prepareStatement(sql);
  424. pstmt.setInt(1,Account.Customer_id);
  425. pstmt.setInt(2,Account.Company_id);
  426. pstmt.setInt(3,Account.Address_id);
  427. pstmt.setString(4,Account.First_name);
  428. pstmt.setString(5,Account.Last_name);
  429. pstmt.setString(6,Account.Contact_email);
  430.  
  431. pstmt.executeUpdate();
  432.  
  433.  
  434. }
  435. public static void InsertSendBlock(Connection con,SendBlockData Account) throws SQLException {
  436.  
  437. String sql = ("INSERT INTO SEND_BLOCK (ID,RECIPIENT) VALUES(?,?)");
  438. PreparedStatement pstmt = con.prepareStatement(sql);
  439. pstmt.setString(2,Account.Recipient);
  440. pstmt.setInt(1,Account.ID);
  441. pstmt.executeUpdate();
  442.  
  443.  
  444. }
  445. public static void InsertAccounts(Connection con,AccountList Account) throws SQLException {
  446.  
  447. String sql = ("INSERT INTO ACCOUNT (PUBLIC_KEY,BLOCKCHAIN,COMPANY_TYPE,CUSTOMER_TYPE) VALUES(?,?,?,?)");
  448. PreparedStatement pstmt = con.prepareStatement(sql);
  449. pstmt.setString(1,Account.PublicKey);
  450. pstmt.setInt(2,Account.id);
  451. pstmt.setObject(3,Account.Company_Type);
  452. pstmt.setObject(4,Account.Customer_Type);
  453. pstmt.executeUpdate();
  454.  
  455.  
  456. }
  457.  
  458. public static void InsertReceiveBlock(Connection con,ReceiveBlockData Account) throws SQLException {
  459.  
  460. String sql = ("INSERT INTO RECEIVE_BLOCK (ID,SENDER) VALUES(?,?)");
  461. PreparedStatement pstmt = con.prepareStatement(sql);
  462. pstmt.setString(2,Account.Sender);
  463. pstmt.setInt(1,Account.id);
  464. pstmt.executeUpdate();
  465.  
  466.  
  467. }
  468. public static String GetLastHash(Connection con, ReceiveBlock block)throws SQLException{
  469. Statement st = con.createStatement();
  470. System.out.println("GetLastHash");
  471. String sql = ("SELECT HASH_CODE FROM BLOCK WHERE BLOCKCHAIN_ID=(SELECT BLOCKCHAIN FROM ACCOUNT WHERE PUBLIC_KEY=?) AND BLOCK_ID=(SELECT LAST_BLOCK FROM BLOCKCHAIN WHERE BLOCKCHAIN_ID=(SELECT BLOCKCHAIN FROM ACCOUNT WHERE PUBLIC_KEY=?)) ");
  472. PreparedStatement pstmt = con.prepareStatement(sql);
  473. System.out.println(block.toString());
  474. pstmt.setString(1,block.getSource());
  475. pstmt.setString(2,block.getSource());
  476. ResultSet rs =pstmt.executeQuery();
  477. rs.next();
  478. System.out.println(rs.getString(1));
  479. return rs.getString(1);
  480. }
  481. public static void InsertLastBlocks(Connection con) throws SQLException
  482. {
  483. Statement st = con.createStatement();
  484. String sql = ("SELECT max(BLOCK_ID) FROM block group by BLOCKCHAIN_ID order by Blockchain_id");
  485. ResultSet rs = st.executeQuery(sql);
  486. int i=1;
  487. while(rs.next())
  488. {
  489. sql = ("Update blockchain set last_block =? where Blockchain_id =?");
  490. PreparedStatement pstmt = con.prepareStatement(sql);
  491. pstmt.setInt(1,rs.getInt(1));
  492. pstmt.setInt(2,i);
  493. pstmt.executeUpdate();
  494. i++;
  495. }
  496. }
  497. public static void UpdateSeq(Connection con) throws SQLException {
  498. Statement st = con.createStatement();
  499. String sql = ("SELECT count(*) FROM Account");
  500. ResultSet rs = st.executeQuery(sql);
  501. rs.next();
  502. int i=20+rs.getInt(1);
  503. sql = ("alter SEQUENCE Blockchain_seq restart start with ");
  504. sql=sql+i;
  505. PreparedStatement pstmt = con.prepareStatement(sql);
  506. System.out.println(sql);
  507. pstmt.executeUpdate();
  508. }
  509. public static boolean AccountExist(Connection con, Account account) throws SQLException {
  510. String sql = ("Select * from account where public_key=? ");
  511.  
  512. PreparedStatement pstmt = con.prepareStatement(sql);
  513. pstmt.setString(1,account.getAddress());
  514. ResultSet rs =pstmt.executeQuery();
  515. if(rs.next())
  516. {
  517. return true;
  518. }else
  519. {
  520. return false;
  521. }
  522. }
  523.  
  524. private static List<Transaction> getTransaction(ResultSet result) throws SQLException {
  525. List<Transaction> transactions = new LinkedList<>();
  526. while (result.next()) {
  527. Transaction transaction = new Transaction(
  528. result.getString("SENDER"),
  529. result.getString("RECIPIENT"),
  530. result.getInt("AMOUNT"),
  531. result.getTimestamp("TRANSACTION_TIME")
  532. );
  533. transactions.add(transaction);
  534. }
  535. result.close();
  536. return transactions;
  537. }
  538.  
  539. public static List<Transaction> getAllOutgoingTransactions(Connection con) throws SQLException {
  540. String sql = "SELECT SEND_BLOCK.RECIPIENT, ACCOUNT.PUBLIC_KEY SENDER, BLOCK.AMOUNT, TRANSACTION_TIME" +
  541. " FROM BLOCK JOIN SEND_BLOCK ON BLOCK.SEND_TYPE = SEND_BLOCK.ID JOIN BLOCKCHAIN ON" +
  542. " BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID JOIN ACCOUNT ON" +
  543. " BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN ORDER BY TRANSACTION_TIME";
  544.  
  545. Statement s = con.createStatement();
  546. ResultSet result = s.executeQuery(sql);
  547.  
  548. List<Transaction> transactions = getTransaction(result);
  549. s.close();
  550. return transactions;
  551. }
  552.  
  553. public static List<Transaction> getAllOutgoingTransactions(Connection con, String publicKey) throws SQLException {
  554. String sql = "SELECT SEND_BLOCK.RECIPIENT, ACCOUNT.PUBLIC_KEY SENDER, BLOCK.AMOUNT, TRANSACTION_TIME" +
  555. " FROM BLOCK JOIN SEND_BLOCK ON BLOCK.SEND_TYPE = SEND_BLOCK.ID JOIN BLOCKCHAIN ON" +
  556. " BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID JOIN ACCOUNT ON" +
  557. " BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN" +
  558. " WHERE ACCOUNT.PUBLIC_KEY = '" + publicKey + "' ORDER BY TRANSACTION_TIME";
  559.  
  560. Statement s = con.createStatement();
  561. ResultSet result = s.executeQuery(sql);
  562.  
  563. List<Transaction> transactions = getTransaction(result);
  564. s.close();
  565. return transactions;
  566. }
  567.  
  568. public static List<Transaction> getAllIncomingTransactions(Connection con) throws SQLException {
  569. String sql = "SELECT RECEIVE_BLOCK.SENDER, ACCOUNT.PUBLIC_KEY RECIPIENT, BLOCK.AMOUNT, TRANSACTION_TIME " +
  570. "FROM BLOCK JOIN RECEIVE_BLOCK ON BLOCK.RECEIVE_TYPE = RECEIVE_BLOCK.ID JOIN BLOCKCHAIN ON" +
  571. " BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID " +
  572. "JOIN ACCOUNT ON BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN ORDER BY TRANSACTION_TIME";
  573.  
  574. Statement s = con.createStatement();
  575. ResultSet result = s.executeQuery(sql);
  576.  
  577. List<Transaction> transactions = getTransaction(result);
  578. s.close();
  579. return transactions;
  580. }
  581.  
  582. public static List<Transaction> getAllIncomingTransactions(Connection con, String publicKey) throws SQLException {
  583. String sql = "SELECT RECEIVE_BLOCK.SENDER, ACCOUNT.PUBLIC_KEY RECIPIENT, BLOCK.AMOUNT, TRANSACTION_TIME " +
  584. "FROM BLOCK JOIN RECEIVE_BLOCK ON BLOCK.RECEIVE_TYPE = RECEIVE_BLOCK.ID JOIN BLOCKCHAIN ON" +
  585. " BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID " +
  586. "JOIN ACCOUNT ON BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN " +
  587. "WHERE ACCOUNT.PUBLIC_KEY = '" + publicKey + "' ORDER BY TRANSACTION_TIME";
  588.  
  589. Statement s = con.createStatement();
  590. ResultSet result = s.executeQuery(sql);
  591. List<Transaction> transactions = getTransaction(result);
  592. s.close();
  593. return transactions;
  594. }
  595.  
  596. public static List<Transaction> getAllIncomingTransactions(Connection con, String startTime, String stopTime) throws SQLException {
  597. // data format = '04.01.2019 21:33:38'
  598. System.out.println(startTime + " " + stopTime);
  599. String sql = "SELECT RECEIVE_BLOCK.SENDER, ACCOUNT.PUBLIC_KEY RECIPIENT, BLOCK.AMOUNT, TRANSACTION_TIME " +
  600. "FROM BLOCK JOIN RECEIVE_BLOCK ON BLOCK.RECEIVE_TYPE = RECEIVE_BLOCK.ID " +
  601. "JOIN BLOCKCHAIN ON BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID JOIN ACCOUNT ON BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN " +
  602. "WHERE TRANSACTION_TIME >= to_date('"+ startTime +"', 'DD.MM.YYYY HH24:MI:SS') " +
  603. "AND TRANSACTION_TIME <= to_date('"+ stopTime +"', 'DD.MM.YYYY HH24:MI:SS') ORDER BY TRANSACTION_TIME";
  604.  
  605. Statement s = con.createStatement();
  606. ResultSet result = s.executeQuery(sql);
  607.  
  608. List<Transaction> transactions = getTransaction(result);
  609. s.close();
  610. return transactions;
  611. }
  612.  
  613. public static List<Transaction> getAllIncomingTransactions(Connection con, String publicKey, String startTime, String stopTime) throws SQLException {
  614. String sql = "SELECT RECEIVE_BLOCK.SENDER, ACCOUNT.PUBLIC_KEY RECIPIENT, BLOCK.AMOUNT, TRANSACTION_TIME " +
  615. "FROM BLOCK JOIN RECEIVE_BLOCK ON BLOCK.RECEIVE_TYPE = RECEIVE_BLOCK.ID " +
  616. "JOIN BLOCKCHAIN ON BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID JOIN ACCOUNT ON BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN " +
  617. "WHERE ACCOUNT.PUBLIC_KEY = '"+ publicKey +"' AND TRANSACTION_TIME >= to_date('"+ startTime +"', 'DD.MM.YYYY HH24:MI:SS') " +
  618. "AND TRANSACTION_TIME <= to_date('"+ stopTime +"', 'DD.MM.YYYY HH24:MI:SS') ORDER BY TRANSACTION_TIME";
  619.  
  620. Statement s = con.createStatement();
  621. ResultSet result = s.executeQuery(sql);
  622.  
  623. List<Transaction> transactions = getTransaction(result);
  624. s.close();
  625. return transactions;
  626. }
  627.  
  628. public static List<Transaction> getAllOutgoingTransactions(Connection con, String startTime, String stopTime) throws SQLException {
  629. // data format = '04.01.2019 21:33:38'
  630. System.out.println(startTime + " " + stopTime);
  631. String sql = "SELECT SEND_BLOCK.RECIPIENT, ACCOUNT.PUBLIC_KEY SENDER, BLOCK.AMOUNT, TRANSACTION_TIME " +
  632. "FROM BLOCK JOIN SEND_BLOCK ON BLOCK.SEND_TYPE = SEND_BLOCK.ID JOIN BLOCKCHAIN ON" +
  633. " BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID JOIN ACCOUNT ON BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN " +
  634. "WHERE TRANSACTION_TIME >= to_date('"+ startTime +"', 'DD.MM.YYYY HH24:MI:SS') " +
  635. "AND TRANSACTION_TIME <= to_date('"+ stopTime +"', 'DD.MM.YYYY HH24:MI:SS') ORDER BY TRANSACTION_TIME";
  636.  
  637. Statement s = con.createStatement();
  638. ResultSet result = s.executeQuery(sql);
  639.  
  640. List<Transaction> transactions = getTransaction(result);
  641. s.close();
  642. return transactions;
  643. }
  644.  
  645. public static List<Transaction> getAllOutgoingTransactions(Connection con, String publicKey, String startTime, String stopTime) throws SQLException {
  646. String sql = "SELECT SEND_BLOCK.RECIPIENT, ACCOUNT.PUBLIC_KEY SENDER, BLOCK.AMOUNT, TRANSACTION_TIME " +
  647. "FROM BLOCK JOIN SEND_BLOCK ON BLOCK.SEND_TYPE = SEND_BLOCK.ID JOIN BLOCKCHAIN ON" +
  648. " BLOCK.BLOCKCHAIN_ID = BLOCKCHAIN.BLOCKCHAIN_ID JOIN ACCOUNT ON BLOCKCHAIN.BLOCKCHAIN_ID = ACCOUNT.BLOCKCHAIN " +
  649. "WHERE ACCOUNT.PUBLIC_KEY = '"+ publicKey +"' AND TRANSACTION_TIME >= to_date('"+ startTime +"', 'DD.MM.YYYY HH24:MI:SS') " +
  650. "AND TRANSACTION_TIME <= to_date('"+ stopTime +"', 'DD.MM.YYYY HH24:MI:SS') ORDER BY TRANSACTION_TIME";
  651.  
  652. Statement s = con.createStatement();
  653. ResultSet result = s.executeQuery(sql);
  654.  
  655. List<Transaction> transactions = getTransaction(result);
  656. s.close();
  657. return transactions;
  658. }
  659.  
  660.  
  661.  
  662. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement