2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.db;
|
2016-07-08 11:33:22 +02:00
|
|
|
|
|
|
|
import java.sql.Connection;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
2022-08-01 22:21:04 +02:00
|
|
|
import org.apache.commons.dbcp2.BasicDataSource;
|
2020-02-02 16:36:43 +01:00
|
|
|
|
2022-08-01 22:21:04 +02:00
|
|
|
/**
|
|
|
|
* A class holding the connection to the database.
|
|
|
|
*/
|
2016-07-08 11:33:22 +02:00
|
|
|
public class DBConnection {
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-08-01 22:21:04 +02:00
|
|
|
private final BasicDataSource connSource;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new connection with the provided settings.
|
|
|
|
* @param host the MySQL DB host.
|
|
|
|
* @param port the MySQL DB port.
|
|
|
|
* @param dbname the MySQL DB name.
|
|
|
|
* @param login the login/username.
|
|
|
|
* @param password the password.
|
|
|
|
*/
|
|
|
|
public DBConnection(String host, int port, String dbname, String login, String password) {
|
|
|
|
this("jdbc:mysql://" + host + ":" + port + "/" + dbname
|
|
|
|
+ "?useUnicode=true"
|
|
|
|
+ "&useSSL=false"
|
|
|
|
+ "&characterEncoding=utf8"
|
|
|
|
+ "&characterSetResults=utf8"
|
|
|
|
+ "&character_set_server=utf8mb4"
|
|
|
|
+ "&character_set_connection=utf8mb4",
|
|
|
|
login, password);
|
2019-03-15 19:09:11 +01:00
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-08-01 22:21:04 +02:00
|
|
|
/**
|
|
|
|
* Create a new connection with the provided settings.
|
|
|
|
* @param url the JDBC URL.
|
|
|
|
* @param login the login/username.
|
|
|
|
* @param password the password.
|
|
|
|
*/
|
|
|
|
public DBConnection(String url, String login, String password) {
|
|
|
|
connSource = new BasicDataSource();
|
|
|
|
connSource.setUrl(url);
|
|
|
|
connSource.setUsername(login);
|
|
|
|
connSource.setPassword(password);
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-08-01 22:21:04 +02:00
|
|
|
/* package */ Connection getConnection() throws SQLException {
|
|
|
|
return connSource.getConnection();
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2022-08-01 22:21:04 +02:00
|
|
|
/**
|
|
|
|
* Closes the connection.
|
|
|
|
*/
|
|
|
|
public void close() {
|
|
|
|
try {
|
|
|
|
connSource.close();
|
|
|
|
} catch (SQLException ignored) {}
|
|
|
|
}
|
2016-07-14 14:22:23 +02:00
|
|
|
|
2016-07-08 11:33:22 +02:00
|
|
|
}
|