0

(NOTE: I have already inspected the question with similar title available and it did not help or I did not understand the solution properly)

All I'm trying to do is retrieve country names from database and display them in a drop-down list. The problem rises from the implementation of the JSF since the query reading and connection is working just fine.

index.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Top Ten</title>
    </h:head>
    <h:body>
        <h1>Choose a country</h1>
        <h:form>
            <h:selectOneMenu value="#{runner.getCountryList()}" >
            <f:selectItems value="#{runner.getCountryList()}"/>
            </h:selectOneMenu>
        </h:form>
    </h:body>
</html>

Results.java

package honolulu.marathon;

/* imports have been removed to make code compact */
@ManagedBean(name="runner")
@SessionScoped
public class Results implements Serializable{
        private Connection con = null;
    private DataSource ds;
    public Results(){
        try {
                Context ctx = new InitialContext();
                ds = (DataSource)ctx.lookup("jdbc/honolulu2017");
        } catch (NamingException e) {
                e.printStackTrace();
        }   
    }
    private Connection getConnection(){
        try {
            con = ds.getConnection();
        } catch(Exception e){
            e.printStackTrace();
        }
        return con;        
    }
    /* fill the drop-down list */
    public List<Runner> getCountryList() throws SQLException{
        getConnection();
        if(ds==null)     
                throw new SQLException("Can't get data source");
        //get database connection
        if(con==null)
                throw new SQLException("Can't get database connection");
        PreparedStatement ps 
                = con.prepareStatement(
                   "select COUNTRY from RESULTS_TEST");
        //get runner data from database
        ResultSet result =  ps.executeQuery();
        List<Runner> list = new ArrayList<>();
        while(result.next()){
                Runner runner = new Runner();
                runner.setCountry(result.getString("country"));
                //store all data into a List
                list.add(runner);
        }
        return list;  
      }

}

Runner.java

package honolulu.marathon;

public class Runner {

    public String country;

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }    
}

output:Click here for output thanks

  • The problem rises from the implementation of the JSF ? Please post the exact error you face ? – Rahul Saini May 19 '17 at 09:16
  • @RahulSaini if you click on the image at the end you will see the output. I don't get an error, I either display the output wrong (JSF) or I don't retrieve the data correctly. thanks –  May 19 '17 at 09:19
  • Sorry, but you must understand that links to imgur, facebook, twitter etc cannot be opened / resolved when accessed at office as these are prohibited. Hence cannot see the error image. Can you try stubbing out the arrayList i.e. create a dummy arrayList and return it, and see if the error still persists ? – Rahul Saini May 19 '17 at 09:23
  • It has to be a list of String List i.e country names to be returned in the result instead of a list of runners List . You need to just iterate over the resultset and add the country "string" data to the list. What you are actually invoking is one instance of runner Managed Bean and getCountryList method from it which should return a List of Strings (country names) instead of List of Runner. – Rahul Saini May 19 '17 at 09:31
  • @RahulSaini when I put a dummy list in the getCountryList and change the method to instead of everything works fine as it should so it must be with the retrieval of the info. –  May 19 '17 at 09:37

1 Answers1

0

It has to be a list of String List i.e country names to be returned in the result instead of a list of runners List . You need to just iterate over the resultset and add the country "string" data to the list. What you are actually invoking is one instance of runner Managed Bean and getCountryList method from it which should return a List of Strings (country names) instead of List of Runner.

Rahul Saini
  • 2,353
  • 1
  • 16
  • 19
  • thanks for your time but I cannot figure out how to add the items to the list inside the while loop. ResultSet result = ps.executeQuery(); List list = new ArrayList<>(); while(result.next()){ //store all data into a List list.add(???); } –  May 19 '17 at 09:49
  • whatever you setting in the runner.setCountry(result.getString("country")); just this list.add(result.getString("country")); !!!! – Rahul Saini May 19 '17 at 09:56
  • I did that and now I get the error: Can't get database connection –  May 19 '17 at 10:02
  • Debug and check if the JNDI Lookup resolves "jdbc/honolulu2017" . This JNDI Name context has to be pre-configured in your Tomcat / JBoss/ Glassfish etc whaetever application server you are using. If the JNDI resolves successfully, you should have the database connection from the datasource. – Rahul Saini May 19 '17 at 10:36
  • the problem is not with the JNDI since it works fine with other methods in the program, for some reason it is unable to connect to the databse with this new implementation of getCountryList method –  May 19 '17 at 11:02