我有一个名为" myDB"在我的服务选项卡中的数据库下的JavaDB中。我创建了一个表名" RESULTS_TEST"我在其中使用命令行工具插入了一些数据。我已经为所述数据库创建了一个JDBC连接池和一个JDBC资源,当我从Domain Admin中测试连接时,我得到了一个肯定的ping控制台。
问题是,当我运行波纹管脚本来提取并显示三列时,我得到了错误:"表/视图' RESULTS_TEST'不存在。"
这是我的代码(我遵循本教程: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;
}
}