package com.corejsf;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.inject.Named; 
   // lub import javax.faces.bean.ManagedBean;
import javax.enterprise.context.RequestScoped; 
   // lub import javax.faces.bean.RequestScoped;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;

@Named // lub @ManagedBean
@RequestScoped
public class CustomerBean {
   @Resource(name="jdbc/mydb")
   private DataSource ds;
   
   public ResultSet getAll() throws SQLException {
      Connection conn = ds.getConnection();
      try {
         Statement stmt = conn.createStatement();        
         ResultSet result = stmt.executeQuery("SELECT * FROM Customers");         
         // return ResultSupport.toResult(result);
         CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl();         
            // można też użyć implementacji dostarczonej przez producenta bazy danych
         crs.populate(result);
         return crs;
      } finally {
         conn.close();
      }
   }
}
