0

I have a database named "myDB" in my JavaDB under databases in my services tab. I have created a table names "RESULTS_TEST" inside which I have inserted some data using command-line tool.I have created a JDBC connection pool as well as a JDBC resource for the said data base and I'm getting a positive ping when I test the connection from inside Domain Admin Console.

The problem is when I run the bellow scripts to extract and display three columns I get the error:"Table/View 'RESULTS_TEST' does not exist."

here is my code (I'm following this tutorial: https://www.mkyong.com/jsf2/jsf-2-0-jdbc-integration-example/):

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>Facelet Title</title>
    </h:head>
    <h:body>
        <h1>JSF 2.0 + JDBC Example</h1>

        <h:dataTable value="#{runner.getRunnerList()}" var="c"
                styleClass="order-table"
                headerClass="order-table-header"
                rowClasses="order-table-odd-row,order-table-even-row"
            >
            <h:column>
                <f:facet name="header">
                    First Name
                </f:facet>
                    #{c.fname}
            </h:column>
            <h:column>
                <f:facet name="header">
                    Last Name
                </f:facet>
                    #{c.lname}
            </h:column>
            <h:column>
                <f:facet name="header">
                    Time
                </f:facet>
                    #{c.time}
            </h:column>
        </h:dataTable>
    </h:body>
</html>

Results.java

package honolulu.marathon;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.sql.DataSource;

@ManagedBean(name="runner")
@SessionScoped
public class Results implements Serializable{
    //resource injection
    @Resource(name="    \n" +
"jdbc/myDB")
    private DataSource ds;
    //if resource injection is not support, you still can get it manually.
    //connect to DB and get runner list
    public List<Runner> getRunnerList() throws SQLException{
            if(ds==null)
                    throw new SQLException("Can't get data source");
            //get database connection
            Connection con = ds.getConnection();
            if(con==null)
                    throw new SQLException("Can't get database connection");
            PreparedStatement ps 
                    = con.prepareStatement(
                       "SELECT FNAME, LNAME, TIME 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.setFname(result.getString("fname"));
                    runner.setLname(result.getString("lname"));
                    runner.setTime(result.getString("time"));
                    //store all data into a List
                    list.add(runner);
            }
            return list;
    }
}

Runner.java

package honolulu.marathon;
public class Runner {
    public String fname;
    public String lname;
        public String time;

    public String getFname() {
        return fname;
    }

    public void setFname(String fname) {
        this.fname = fname;
    }

    public String getLname() {
        return lname;
    }

    public void setLname(String lname) {
        this.lname = lname;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

}
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • What does your ``"jdbc/myDB"`` resource look like? And why do you have a linebreak in ``@Resource``'s value? – f1sh May 16 '17 at 09:55
  • @f1sh I don't know how to show that to you. what informatino do you want? –  May 16 '17 at 09:58
  • Doing SQL from within a managed bean is not good practice for real systems. Put all that in a service (and then your question is directly not JSF related) – Kukeltje May 16 '17 at 10:18
  • @Kukeltje so how can I implement that. Is that the reason that I get the error? –  May 16 '17 at 10:34
  • No it is not related... But start here for the 'service' thing: http://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao/ – Kukeltje May 16 '17 at 10:49

0 Answers0