首页
论坛
图书
开发资料
在线文档
网址
下载
联系我们
 新闻│Java│JavaScript│Eclipse│Eclipse 英文│J2EE│J2ME│J2SE│JSP│Netbeans│Hibernate│JBuilder│Spring│Struts
站内搜索: 请输入搜索关键词

当前页面: 开发资料首页 → Java 专题 → jsf在bean的构造函数中访问数据库

jsf在bean的构造函数中访问数据库

摘要: jsf在bean的构造函数中访问数据库

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="750" border="0"> <tr> <td> </td> </tr> </table>

JSP页面初始化时,也会初始化对应的JSF中的bean,在bean里加一个构造函数,在构造函数里取数据库的资料,这样在JSP页面上就可以显示数据库中的数据。


给你贴一段代码看看:
package com.jack;
import java.sql.*;
import java.sql.DriverManager;
/**
*
* @author jack
*/
public class DBConnect {
private static Connection conn=null;
/** Creates a new instance of Conn */
public DBConnect()
{
OpenConnection();
}

public void Close()
{
try
{
conn.close();
}
catch(SQLException e)
{
System.out.println(e);
}
}

public static Connection getConnection()
{
if (conn==null)
OpenConnection();
return conn;
}

public static void OpenConnection()
{
if (conn==null)
{
try
{
java.lang.Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:localServer","sa","");
System.out.println("get connection success!");
}
catch(SQLException e)
{
System.out.println(e);
}
catch(ClassNotFoundException e)
{
System.out.println("class not found!");
}
}
}
}
package com.jack;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.SQLException;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
/**
* @author jack
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class CustomerBean
{
public Result getAll() throws SQLException
{
System.out.println("begin get Connection;");
Connection conn=DBConnect.getConnection();
System.out.println("end get Connection");
Statement stmt=conn.createStatement();
System.out.println("create Statement");
ResultSet result=stmt.executeQuery("select JobNo,name,DepartmentNo,HomeAddress,Telphone,ClassName from EmployEmpTable");
System.out.println("get Resultset");
return ResultSupport.toResult(result);
}
}


index.jsp:

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<head>
<link> href="styles.css" rel="stylesheet" type="text/css"/>

<BR> <h:outputText value="#{msgs.pageTitle}"/><BR>
</head>
<body>

styleClass="customers"
headerClass="customersHeader" columnClasses="custid,name">






































</body>


文件faces-config.xml:
<?xml version="1.0"?>
faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN"
"http://java.sun.com/dtd/web-facesconfig_1_0.dtd">


customer
com.jack.CustomerBean
session


function TempSave(ElementID) { CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value); CommentsPersistDiv.save("CommentXMLStore"); } function Restore(ElementID) { CommentsPersistDiv.load("CommentXMLStore"); document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent"); } </td> </tr> <tr>


↑返回目录
前一篇: JSF与Struts的异同
后一篇: 如何创建你自己的JSF组件

首页 | 全站 Sitemap | 联系我们 | 设为首页 | 收藏本站
版权所有 Copyright © 2006-2007, Java 编程资料牛鼻站, All rights reserved