Java tutorial
/* * 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.cable.xforce.DbHelper; import static com.cerebro.cable.xforce.MyUI.logger; import com.cerebro.cable.xforce.commons.Costants; 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 String db_host; private String db_port; private String db_database; private String db_username; private 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(Costants.DB_HOST); db_port = props.getProperty(Costants.DB_PORT); db_database = props.getProperty(Costants.DB_DATABASE); db_username = props.getProperty(Costants.DB_USERNAME); db_password = props.getProperty(Costants.DB_PASSWORD); } public JDBCConnectionPool getJDBCConnPool() { JDBCConnectionPool pool = null; try { pool = new SimpleJDBCConnectionPool("com.mysql.jdbc.Driver", db_host, db_username, db_password, 2, 5); logger.info("Connessione avvenuta con successo"); } catch (SQLException e) { logger.error("Errore durante la connessione: " + e.getMessage()); } return pool; } public void closeJDBCConnPool(JDBCConnectionPool pool) { if (pool != null) { logger.info("Chiudo la connessione con il database"); pool.destroy(); } } }