Split tables into individual classes
This commit is contained in:
parent
eb0b35dd47
commit
46e336bdca
|
@ -14,9 +14,9 @@ public class Main {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
db = new DatabaseConnection();
|
db = new DatabaseConnection();
|
||||||
|
|
||||||
System.out.println(db.query("SELECT * FROM blocks"));
|
System.out.println(db.getBlocksTable().select());
|
||||||
System.out.println(db.query("SELECT * FROM transactions"));
|
System.out.println(db.getTransactionsTable().select());
|
||||||
System.out.println(db.query("SELECT * FROM nfts"));
|
System.out.println(db.getNFTsTable().select());
|
||||||
|
|
||||||
Wallet w1 = new Wallet();
|
Wallet w1 = new Wallet();
|
||||||
Wallet w2 = new Wallet();
|
Wallet w2 = new Wallet();
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,12 +9,19 @@ import java.util.Map;
|
||||||
public class DatabaseConnection {
|
public class DatabaseConnection {
|
||||||
private Connection connection = null;
|
private Connection connection = null;
|
||||||
private Statement statement = null;
|
private Statement statement = null;
|
||||||
|
private DatabaseBlocks blocks;
|
||||||
|
private DatabaseTransactions transactions;
|
||||||
|
private DatabaseNFTs nfts;
|
||||||
public DatabaseConnection() {
|
public DatabaseConnection() {
|
||||||
System.out.println("Connecting to database...");
|
System.out.println("Connecting to database...");
|
||||||
try {
|
try {
|
||||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||||
connection = DriverManager.getConnection("jdbc:mysql://helium.mikorosa.pl:3306/dziecoin", "dziecoin", "Y6seMx#HwgYU");
|
connection = DriverManager.getConnection("jdbc:mysql://helium.mikorosa.pl:3306/dziecoin", "dziecoin", "Y6seMx#HwgYU");
|
||||||
System.out.println("Connected");
|
System.out.println("Connected");
|
||||||
|
|
||||||
|
blocks = new DatabaseBlocks();
|
||||||
|
transactions = new DatabaseTransactions();
|
||||||
|
nfts = new DatabaseNFTs();
|
||||||
} catch(ClassNotFoundException e) {
|
} catch(ClassNotFoundException e) {
|
||||||
System.out.println("Error while setting up JDBC driver: " + e.getMessage());
|
System.out.println("Error while setting up JDBC driver: " + e.getMessage());
|
||||||
e.printStackTrace();
|
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) {
|
public List<Map<String, Object>> query(String sql) {
|
||||||
List<Map<String, Object>> list = new ArrayList<>();
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue