Implement database connection
This commit is contained in:
parent
d53a1c820a
commit
eb0b35dd47
|
@ -11,5 +11,7 @@
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="library" name="mysql.connector.java" level="project" />
|
||||||
|
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.30" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
8
pom.xml
8
pom.xml
|
@ -13,4 +13,12 @@
|
||||||
<maven.compiler.target>19</maven.compiler.target>
|
<maven.compiler.target>19</maven.compiler.target>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>8.0.30</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -1,5 +1,6 @@
|
||||||
package pl.mikorosa.dziecoin;
|
package pl.mikorosa.dziecoin;
|
||||||
|
|
||||||
|
import pl.mikorosa.dziecoin.database.DatabaseConnection;
|
||||||
import pl.mikorosa.dziecoin.gui.MainFrame;
|
import pl.mikorosa.dziecoin.gui.MainFrame;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -8,8 +9,15 @@ import java.util.List;
|
||||||
public class Main {
|
public class Main {
|
||||||
public static int difficulty;
|
public static int difficulty;
|
||||||
public static Blockchain blockchain;
|
public static Blockchain blockchain;
|
||||||
|
public static DatabaseConnection db;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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"));
|
||||||
|
|
||||||
Wallet w1 = new Wallet();
|
Wallet w1 = new Wallet();
|
||||||
Wallet w2 = new Wallet();
|
Wallet w2 = new Wallet();
|
||||||
Wallet w3 = new Wallet();
|
Wallet w3 = new Wallet();
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
package pl.mikorosa.dziecoin.database;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class DatabaseConnection {
|
||||||
|
private Connection connection = null;
|
||||||
|
private Statement statement = null;
|
||||||
|
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");
|
||||||
|
} catch(ClassNotFoundException e) {
|
||||||
|
System.out.println("Error while setting up JDBC driver: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch(SQLException e) {
|
||||||
|
System.out.println("Error while connecting to database: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Map<String, Object>> query(String sql) {
|
||||||
|
List<Map<String, Object>> list = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
System.out.println("Executing query " + sql);
|
||||||
|
statement = connection.createStatement();
|
||||||
|
ResultSet result = statement.executeQuery(sql);
|
||||||
|
ResultSetMetaData metaData = result.getMetaData();
|
||||||
|
int columnCount = metaData.getColumnCount();
|
||||||
|
|
||||||
|
while(result.next()) {
|
||||||
|
Map<String, Object> record = new HashMap<>();
|
||||||
|
for(int i = 1; i <= columnCount; i++) {
|
||||||
|
String columnName = metaData.getColumnName(i);
|
||||||
|
int columnType = metaData.getColumnType(i);
|
||||||
|
Object value = null;
|
||||||
|
switch(columnType) {
|
||||||
|
case Types.INTEGER:
|
||||||
|
value = result.getInt(i);
|
||||||
|
break;
|
||||||
|
case Types.VARCHAR:
|
||||||
|
case Types.NVARCHAR:
|
||||||
|
value = result.getString(i);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
value = result.getObject(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
record.put(columnName, value);
|
||||||
|
}
|
||||||
|
list.add(record);
|
||||||
|
}
|
||||||
|
} catch(SQLException e) {
|
||||||
|
System.out.println("Failed to execute query (" + sql + "): " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean update(String sql) {
|
||||||
|
try {
|
||||||
|
System.out.println("Executing query " + sql);
|
||||||
|
statement = connection.createStatement();
|
||||||
|
int modifiedRows = statement.executeUpdate(sql);
|
||||||
|
|
||||||
|
if(modifiedRows > 0) return true;
|
||||||
|
} catch(SQLException e) {
|
||||||
|
System.out.println("Failed to execute query (" + sql + "): " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetCounter(String tableName) {
|
||||||
|
try {
|
||||||
|
System.out.println("Executing query ALTER TABLE " + tableName + " AUTO_INCREMENT=1");
|
||||||
|
statement = connection.createStatement();
|
||||||
|
statement.execute("ALTER TABLE " + tableName + " AUTO_INCREMENT=1");
|
||||||
|
} catch(SQLException e) {
|
||||||
|
System.out.println("Failed to execute query (ALTER TABLE " + tableName + " AUTO_INCREMENT=1): " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deleteAllRecords(String tableName) {
|
||||||
|
try {
|
||||||
|
System.out.println("Executing query DELETE FROM " + tableName);
|
||||||
|
statement = connection.createStatement();
|
||||||
|
statement.executeUpdate("DELETE FROM " + tableName);
|
||||||
|
} catch(SQLException e) {
|
||||||
|
System.out.println("Failed to execute query (DELETE FROM " + tableName + "): " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue