JSF2与JDBC集成实例

2010年的最后一天写了一个JSF2与JDBC集成的例子,记录一下。

准备数据库表创建:

 

SQL> create table customer(

  2  id number(8) not null primary key,

  3  name varchar2(20),

  4  age number(3),

  5  gender varchar2(8)

  6  );

insert into customer(id,name,age,gender) values (1,'zhanghong',1,'male');

insert into customer(id,name,age,gender) values (2,'zhanghong2',2,'female');

insert into customer(id,name,age,gender) values (3,'zhanghong3',3,'male');

insert into customer(id,name,age,gender) values (4,'zhanghong4',4,'female');

 

定义POJO:

 

public class Customer {
private long id;
private String name;
private int age;
private String sex;
.........
}
 

 

配置jndi数据源
weblogic.xml添加以下配置:
<wls:resource-description>
     <wls:res-ref-name>jdbc/oracleDateSource</wls:res-ref-name>
     <wls:jndi-name>ora1</wls:jndi-name>
</wls:resource-description>
其中jndi-name为在weblogic中配置数据源名,我这里配了一个ora1的数据源,而res-ref-name则是项目中可以应用的名称。
web.xml中添加以下配置:
<resource-ref>
<res-ref-name>jdbc/oracleDateSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
其中res-ref-name中的字符串与weblogic.xml中配置的一致。这样我们就可以通过Annotation将数据源住入到ManageBean中:
@ManagedBean(name = "userBean")
@RequestScoped
public class UserBean {
.....................
	private List<Customer> customers;

	@Resource(name = "jdbc/oracleDateSource")
	private DataSource dataSource;


         public List<Customer> getCustomers() throws SQLException{
		if (dataSource == null)
			throw new SQLException("Can't get data source");

		// get database connection
		Connection con = dataSource.getConnection();

		if (con == null)
			throw new SQLException("Can't get database connection");

		PreparedStatement ps = con
				.prepareStatement("select id, name,age,gender from customer");

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

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

		while (result.next()) {
			Customer customer = new Customer();

			application.setId(result.getLong("id"));
			application.setName(result.getString("name"));
			application.setAge(result.getInt("age"));
			application.setSex(result.getString("gender"));

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

		return list;
	}

	public void setApplications(List<Customer> customers) {
		this.customers = customers;
	}
 

页面:

 

<h:dataTable value="#{userBean.customers}" var="a" styleClass="data-table"
			headerClass="tableHeader" rowClasses="odd,even" >
			<h:column>
				<f:facet name="header">
					<h:outputText value="ID"></h:outputText>
				</f:facet>
				<h:outputText value="#{a.id}"></h:outputText>
			</h:column>
			<h:column>
				<f:facet name="header">
					<h:outputText value="NAME"></h:outputText>
				</f:facet>
				<h:outputText value="#{a.name}"></h:outputText>
			</h:column>
			<h:column>
				<f:facet name="header">
					<h:outputText value="AGE"></h:outputText>
				</f:facet>
				<h:outputText value="#{a.age}"></h:outputText>
			</h:column>
			<h:column>
				<f:facet name="header">
					<h:outputText value="SEX"></h:outputText>
				</f:facet>
				<h:outputText value="#{a.sex}"></h:outputText>
			</h:column>
		</h:dataTable>

页面效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值