com.cerebro.gorgone.database.DbHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.cerebro.gorgone.database.DbHelper.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cerebro.gorgone.database;

import static com.cerebro.gorgone.MyUI.logger;
import com.cerebro.gorgone.costants.ConfigProperties;
import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.server.VaadinServlet;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author matteo
 */
public class DbHelper {

    private final String db_host;
    private final String db_port;
    private final String db_database;
    private final String db_username;
    private final String db_password;

    public DbHelper() {

        Properties props = new Properties();
        InputStream config = VaadinServlet.getCurrent().getServletContext()
                .getResourceAsStream("/WEB-INF/config.properties");
        if (config != null) {
            try {
                logger.info("Carico il file .properties");
                props.load(config);
            } catch (Exception ex) {
                logger.error("Errore nel caricamento del file .properties: " + ex.getMessage());
            }
        }
        db_host = props.getProperty(ConfigProperties.DB_HOST);
        db_port = props.getProperty(ConfigProperties.DB_PORT);
        db_database = props.getProperty(ConfigProperties.DB_DATABASE);
        db_username = props.getProperty(ConfigProperties.DB_USERNAME);
        db_password = props.getProperty(ConfigProperties.DB_PASSWORD);
    }

    public JDBCConnectionPool getJDBCConnPool() {
        JDBCConnectionPool pool = null;
        String connUri = "jdbc:mysql://" + db_host + ":" + db_port + "/" + db_database
                + "?useSSL=false&serverTimezone=UTC";
        try {
            pool = new SimpleJDBCConnectionPool("com.mysql.jdbc.Driver", connUri, db_username, db_password, 2, 5);
            logger.info("Connessione avvenuta con successo");
        } catch (SQLException e) {
            logger.error("Errore durante la connessione: " + e.getMessage());
        }
        //        Connection conn = null;
        //        try {
        //            conn = pool.reserveConnection();
        //            Statement stmn = conn.createStatement();
        //            ResultSet rs = stmn.executeQuery("SELECT * FROM races");
        //            System.out.println(rs);
        //        } catch (Exception ex) {
        //            logger.error("Test del database: " + ex.getMessage());
        //        }
        //        pool.releaseConnection(conn);
        return pool;
    }

    public void closeJDBCConnPool(JDBCConnectionPool pool) {
        if (pool != null) {
            logger.info("Chiudo la connessione con il database");
            pool.destroy();
        }
    }

}