如何检索数据表中的数据

时间:2015-03-01 13:15:24

标签: jsf jsf-2 arraylist

请帮我检查数据库中的数据到数据表中。我有两个要检索的字段(institutionName和institutionCode)。但是,它正在运行时在#{test1.getCustomerList()}中产生解析错误,该错误来自数据表的值。以下是我的代码:

<h:dataTable id="dt1" var="item" value="#{test1.getCustomerList()}" bgcolor="#F1F1F1" border="10" cellpadding="5" cellspacing="3" rows="4" width="50%" dir="LTR" frame="hsides" rules="all" summary="This is a JSF code to create dataTable." >

dataModel Bean:

package rfc;

public class dataModel {
    public String institutionName="";
    public String institutionCode="";

    public void setinstitutionName(String institutionName) {
        this.institutionName = institutionName;
    }

    public String getinstitutionName() {
        return institutionName;
    }

    public void setinstitutionCode(String institutionCode) {
        this.institutionCode = institutionCode;
    }

    public String getinstitutionCode() {
        return institutionCode;
    }
}

Bean1

package rfc;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;  // for JNDI
import javax.sql.*;     // extended JDBC interfaces (such as data sources)
import java.sql.*;      // standard JDBC interfaces
import java.io.*;
import java.sql.*;
import java.util.*;

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.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import rfc.dataModel;


@ManagedBean(name = "test1")
@SessionScoped



public class Bean1 implements Serializable {

        @Resource(name="jdbc/XYZ")
        private DataSource ds;
        ResultSet rs;
        private List perInfoAll = new ArrayList();
            Connection conn = null;
         String institutionName="";

        public List<dataModel> getCustomerList() 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 * from institution where institution_code = 'AAA'"); 

                        //get customer data from database
                        ResultSet result =  ps.executeQuery();

                        List<dataModel> list = new ArrayList<dataModel>();

                        while(result.next()){
                                dataModel cust = new dataModel();

                                cust.setinstitutionName(result.getString("institution_id"));
                                cust.setinstitutionCode(result.getString("institution_code"));

                                //store all data into a List
                                list.add(cust);
                        }

                        return list;
                }
        }

你能帮忙吗

1 个答案:

答案 0 :(得分:0)

我认为在jsf页面中你必须使用不带 get 短语的属性,如下所示:

value="${test1.customerList}"