/*
 * Db.java
 *
 * Created on September 19, 2007, 5:10 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/** 
 *
 * @author lluis
 */
public class Db {
    //String connectionURL = "jdbc:postgresql://storage:5432/siprp";
    String connectionURL = "jdbc:postgresql://localhost:5436/siprp";    
    String User = "postgres";
    String Pass = null;
    Connection connection = null;    
       
    /** Creates a new instance of Db */
    public Db() {
    }
    
    public Connection connect() throws Exception
    {
        Class.forName("org.postgresql.Driver").newInstance();
        connection = DriverManager.getConnection(connectionURL, User, Pass);            
        HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);				                
        session.setAttribute("connection", connection);
        return connection;
    }   
    
    public Statement createStatement()
    {
        HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(false);				        
        connection = (Connection) session.getAttribute("connection");        
        Statement st;
        try
        {
            st = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);       		
            return st;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }		        
    }    
}
