69 lines
1.7 KiB
Java
69 lines
1.7 KiB
Java
package de.hsel.itech.db;
|
|
|
|
import de.hsel.itech.config.Configuration;
|
|
import org.jetbrains.annotations.Nullable;
|
|
import org.mariadb.jdbc.MariaDbPoolDataSource;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @author Johannes Theiner
|
|
* @version 0.1
|
|
* @since 0.1
|
|
*/
|
|
public class Database {
|
|
|
|
private MariaDbPoolDataSource dataSource;
|
|
|
|
public Database() {
|
|
Configuration config = Configuration.get();
|
|
dataSource = new MariaDbPoolDataSource("jdbc:mysql://" + config.getDatabase().getHostname() + ":" + config.getDatabase().getPort() + "/" + config.getDatabase().getDatabase());
|
|
try {
|
|
dataSource.setUser(config.getDatabase().getUsername());
|
|
dataSource.setPassword(config.getDatabase().getPassword());
|
|
dataSource.initialize();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
@Nullable
|
|
private Connection getConnection() {
|
|
try {
|
|
return dataSource.getConnection();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public String getHello() {
|
|
Connection connection = getConnection();
|
|
|
|
String hello = "";
|
|
try {
|
|
assert connection != null;
|
|
PreparedStatement statement = connection.prepareStatement("SELECT * FROM test WHERE id = 1");
|
|
ResultSet resultSet = statement.executeQuery();
|
|
resultSet.next();
|
|
hello = resultSet.getString("hello");
|
|
|
|
connection.close();
|
|
} catch (SQLException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return hello;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |