Split tables into individual classes

This commit is contained in:
Aelita4 2023-01-20 19:06:04 +01:00
parent eb0b35dd47
commit 46e336bdca
6 changed files with 101 additions and 3 deletions

View File

@ -14,9 +14,9 @@ public class Main {
public static void main(String[] args) {
db = new DatabaseConnection();
System.out.println(db.query("SELECT * FROM blocks"));
System.out.println(db.query("SELECT * FROM transactions"));
System.out.println(db.query("SELECT * FROM nfts"));
System.out.println(db.getBlocksTable().select());
System.out.println(db.getTransactionsTable().select());
System.out.println(db.getNFTsTable().select());
Wallet w1 = new Wallet();
Wallet w2 = new Wallet();

View File

@ -0,0 +1,20 @@
package pl.mikorosa.dziecoin.database;
import pl.mikorosa.dziecoin.Main;
import java.util.List;
import java.util.Map;
public class DatabaseBlocks extends DatabaseFetchedData {
public DatabaseBlocks() {
tableName = "blocks";
}
public List<Map<String, Object>> select() {
return Main.db.query("SELECT * FROM blocks");
}
public void insert(int height, String hash, String prevHash, int nonce) {
Main.db.update("INSERT INTO %s VALUES (%d, '%s', '%s', %d)".formatted(tableName, height, hash, prevHash, nonce));
}
}

View File

@ -9,12 +9,19 @@ import java.util.Map;
public class DatabaseConnection {
private Connection connection = null;
private Statement statement = null;
private DatabaseBlocks blocks;
private DatabaseTransactions transactions;
private DatabaseNFTs nfts;
public DatabaseConnection() {
System.out.println("Connecting to database...");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://helium.mikorosa.pl:3306/dziecoin", "dziecoin", "Y6seMx#HwgYU");
System.out.println("Connected");
blocks = new DatabaseBlocks();
transactions = new DatabaseTransactions();
nfts = new DatabaseNFTs();
} catch(ClassNotFoundException e) {
System.out.println("Error while setting up JDBC driver: " + e.getMessage());
e.printStackTrace();
@ -24,6 +31,18 @@ public class DatabaseConnection {
}
}
public DatabaseBlocks getBlocksTable() {
return blocks;
}
public DatabaseTransactions getTransactionsTable() {
return transactions;
}
public DatabaseNFTs getNFTsTable() {
return nfts;
}
public List<Map<String, Object>> query(String sql) {
List<Map<String, Object>> list = new ArrayList<>();
try {

View File

@ -0,0 +1,19 @@
package pl.mikorosa.dziecoin.database;
import pl.mikorosa.dziecoin.Main;
import java.util.List;
import java.util.Map;
public abstract class DatabaseFetchedData {
protected String tableName;
public List<Map<String, Object>> select() {
return null;
}
public void cleanTable() {
Main.db.deleteAllRecords(tableName);
Main.db.resetCounter(tableName);
}
}

View File

@ -0,0 +1,20 @@
package pl.mikorosa.dziecoin.database;
import pl.mikorosa.dziecoin.Main;
import java.util.List;
import java.util.Map;
public class DatabaseNFTs extends DatabaseFetchedData {
public DatabaseNFTs() {
tableName = "nfts";
}
public List<Map<String, Object>> select() {
return Main.db.query("SELECT * FROM nfts");
}
public void insert(String contract, String owner, String data, int blockHeight) {
Main.db.update("INSERT INTO %s VALUES (null, '%s', '%s', '%s', %d)".formatted(tableName, contract, owner, data, blockHeight));
}
}

View File

@ -0,0 +1,20 @@
package pl.mikorosa.dziecoin.database;
import pl.mikorosa.dziecoin.Main;
import java.util.List;
import java.util.Map;
public class DatabaseTransactions extends DatabaseFetchedData {
public DatabaseTransactions() {
tableName = "transactions";
}
public List<Map<String, Object>> select() {
return Main.db.query("SELECT * FROM transactions");
}
public void insert(String sender, String recipient, int amount, int blockHeight) {
Main.db.update("INSERT INTO %s VALUES (null, '%s', '%s', %d, %d)".formatted(tableName, sender, recipient, amount, blockHeight));
}
}