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.provevaadin.database; import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool; import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; import com.vaadin.server.VaadinServlet; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author matteo */ public class MySQLServer { String db_user; String db_pwd; String db_host; String db_port; public MySQLServer() { // String config_path = VaadinServlet.getCurrent().getServletContext().getResourceAsStream(db_pwd) // "/WEB-INF/config.properties"; Properties props = new Properties(); InputStream config = VaadinServlet.getCurrent().getServletContext() .getResourceAsStream("/WEB-INF/config.properties"); // try { // config = new FileInputStream(config_path); // // } catch (FileNotFoundException ex) { // Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex); // } if (config != null) { try { props.load(config); } catch (IOException ex) { Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println(props.getProperty("db_pwd")); db_host = props.getProperty("db_host"); db_user = props.getProperty("db_user"); db_pwd = props.getProperty("db_pwd"); } public JDBCConnectionPool getJDBCConnPool() { JDBCConnectionPool pool = null; try { pool = new SimpleJDBCConnectionPool("com.mysql.jdbc.Driver", db_host, db_user, db_pwd); System.out.println("Connessione avvenuta con successo"); } catch (SQLException e) { System.out.println("Errore durante la connessione"); System.out.println(e); } return pool; } public void closeJDBCConnPool(JDBCConnectionPool pool) { if (pool != null) { System.out.println("Chiudo la connessione con il database"); pool.destroy(); } } public Boolean testJDBCConnPool(JDBCConnectionPool pool) { Boolean result = false; System.out.println("Test della connessione"); try { Connection conn = pool.reserveConnection(); result = conn.isValid(0); } catch (SQLException ex) { Logger.getLogger(MySQLServer.class.getName()).log(Level.SEVERE, null, ex); } return result; } }