Started working on the database manager

This commit is contained in:
2026-03-25 20:11:28 +13:00
parent c875adfceb
commit b7e376bbe0
3 changed files with 65 additions and 3 deletions

View File

@@ -0,0 +1,60 @@
package src.main;
import java.beans.PropertyVetoException;
import java.sql.*;
import java.util.HashMap;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import src.main.utilities.Configs;
public class DatabaseManager {
private static final HashMap<String, Object> config = new ConfigManager().readConfig(Configs.database.getFileName());
private ComboPooledDataSource dataSource;
public DatabaseManager() {
dataSource = new ComboPooledDataSource();
try {
dataSource .setDriverClass("org.postgresql.Driver"); //loads the jdbc driver
} catch (PropertyVetoException error) {
System.err.println(error);
error.printStackTrace();
}
dataSource.setJdbcUrl( "jdbc:postgresql://"+ config.get("address").toString() + "/" + config.get("defautlTable").toString());
dataSource.setUser(config.get("username").toString());
dataSource.setPassword(config.get("password").toString());
// the settings below are optional -- c3p0 can work with defaults
dataSource.setMinPoolSize(5);
dataSource.setAcquireIncrement(5);
dataSource.setMaxPoolSize(20);
// The DataSource connection is now a fully configured and usable pooled DataSource
}
public void sendQuery() {
// Use try-with-resources to automatically close resources (Connection, Statement, ResultSet)
try (Connection conn = dataSource.getConnection();
PreparedStatement prepedStatement = conn.prepareStatement("SELECT * FROM users WHERE id = ?")) {
// Set parameters for the prepared statement
prepedStatement.setInt(1, 1);
// Execute the query and process the results
try (ResultSet rs = prepedStatement.executeQuery()) {
while (rs.next()) {
System.out.println("User Name: " + rs.getString("name"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

View File

@@ -1,9 +1,11 @@
package src.main;
import org.snmp4j.Snmp;
public class SnmpPoller {
public void getOid() {
public SnmpPoller() {
}
}

View File

@@ -16,7 +16,7 @@ public class main {
int cpuCount = Runtime.getRuntime().availableProcessors();
int threads = cpuCount * 2;
ConfigManager configs = new ConfigManager();
ConfigManager configs = new ConfigManager(true);
}