Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package conectare;
- import java.beans.Statement;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import java.sql.PreparedStatement;
- public class ConectareBD {
- private static final Logger LOGGER = Logger.getLogger(ConectareBD.class.getName());
- private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
- private static final String DBURL = "jdbc:mysql://localhost:3306/fooddelivery?autoReconnect=true&useSSL=false";//"jdbc:mysql://localhost:3306/project";
- private static final String USER = "root";
- private static final String PASS = "root";
- private static ConectareBD singleInstance = new ConectareBD();
- private ConectareBD() {
- try {
- Class.forName(DRIVER);
- } catch (ClassNotFoundException e) {
- //e.printStackTrace();
- }
- }
- private Connection createConnection() {
- Connection connection = null;
- try {
- connection = DriverManager.getConnection(DBURL, USER, PASS);
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to connect to the database");
- e.printStackTrace();
- }
- return connection;
- }
- public static Connection getConnection() {
- return singleInstance.createConnection();
- }
- public static void close(Connection connection) {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to close the connection");
- }
- }
- }
- public static void close(PreparedStatement statement) {
- if (statement != null) {
- try {
- statement.close();
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to close the statement");
- }
- }
- }
- public static void close(ResultSet resultSet) {
- if (resultSet != null) {
- try {
- resultSet.close();
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to close the ResultSet");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement