广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州如何设置JSF JDBC设置?_北大青鸟IT学校

作者:邓华发布时间:2021-04-30分类:Java技术浏览:730


导读:Java是现在非常主流的编程语言之一,很多人想转行学习Java。那么,如何设置JSF JDBC设置?下面就让我们一起来看看惠州北大青鸟老师是怎么回答的。

Java是现在非常主流的编程语言之一,很多人想转行学习Java。那么,如何设置JSF JDBC设置?下面就让我们一起来看看惠州北大青鸟老师是怎么回答的。

以下代码显示如何从后端数据库加载数据并使用JSF显示数据。

例子

以下代码来自Customer.java。

package cn.w3cschool.common;

import java.util.Date;public class Customer{  
 public long customerID;  public String name;  public String address;  public Date created_date;  
 public long getCustomerID() {
   return customerID;
 }  public void setCustomerID(long customerID) {
   this.customerID = customerID;
 }  public String getName() {
   return name;
 }  public void setName(String name) {
   this.name = name;
 }  public String getAddress() {
   return address;
 }  public void setAddress(String address) {
   this.address = address;
 }  public Date getCreated_date() {
   return created_date;
 }  public void setCreated_date(Date created_date) {
   this.created_date = created_date;
 }
}

以下代码来自table-style.css。

.order-table{  
 border-collapse:collapse;
}

.order-table-header{
 text-align:center;
 background:none repeat scroll 0 0 #E5E5E5;
 border-bottom:1px solid #BBBBBB;
 padding:16px;
}

.order-table-odd-row{
 text-align:center;
 background:none repeat scroll 0 0 #FFFFFFF;
 border-top:1px solid #BBBBBB;
}

.order-table-even-row{
 text-align:center;
 background:none repeat scroll 0 0 #F9F9F9;
 border-top:1px solid #BBBBBB;
}

以下代码来自CustomerBean.java。

package cn.w3cschool.common;

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;


@ManagedBean(name="customer")
@SessionScopedpublic class CustomerBean implements Serializable{
 //resource injection
 @Resource(name="jdbc/w3cschool")  private DataSource ds;
 //if resource inject is not support, you still can get it manually.  public CustomerBean(){    try {
     Context ctx = new InitialContext();
     ds = (DataSource)ctx.lookup("java:comp/env/jdbc/w3cschool");
   } catch (NamingException e) {
     e.printStackTrace();
   }
 }
 
 //connect to DB and get customer list  public List<Customer> 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");

//        con.createStatement().executeUpdate("create table customer(customer_id varchar(45),"
 //          +" name varchar(45),"
   //        +"address varchar(45),"+
     //      "created_date date)");

        con.createStatement().executeUpdate("insert into customer(customer_id,"
           +" name ,"
           +"address,"+            "created_date)values("1","w3cschool","Main Street",null)");
   
   PreparedStatement ps
     = con.prepareStatement(        "select customer_id, name, address, created_date from customer");
   ResultSet result =  ps.executeQuery();
   List<Customer> list = new ArrayList<Customer>();    while(result.next()){
     Customer cust = new Customer();
     cust.setCustomerID(result.getLong("customer_id"));
     cust.setName(result.getString("name"));
     cust.setAddress(result.getString("address"));
     cust.setCreated_date(result.getDate("created_date"));
     list.add(cust);
   }
   return list;
 }
}

以下代码来自context.xml。

<Context>
<!--
 <Resource name="jdbc/w3cschool"
           auth="Container"
           type="javax.sql.DataSource"
           maxActive="100"
           maxIdle="30"
           maxWait="10000"
           username="sa"
           password=""
           driverClassName="com.mysql.jdbc.Driver"
           url="jdbc:mysql://localhost:3306/w3cschool"/>

-->
 <Resource name="jdbc/w3cschool"
           auth="Container"
           type="javax.sql.DataSource"
           maxActive="100"
           maxIdle="30"
           maxWait="10000"
           username="SA"
           password=""
           driverClassName="org.hsqldb.jdbc.JDBCDriver"
           url="jdbc:hsqldb:mydatabase"/>

</Context>

以下代码来自default.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://java.sun.com/jsf/html"
     xmlns:f="http://java.sun.com/jsf/core"
     >
   <h:head>
     <h:outputStylesheet library="css" name="table-style.css"  />
   </h:head>

   <h:body>
    <h:dataTable value="#{customer.getCustomerList()}" var="c"
         styleClass="order-table"
         headerClass="order-table-header"
         rowClasses="order-table-odd-row,order-table-even-row">
        <h:column>
         <f:facet name="header">Customer ID</f:facet>#{c.customerID}
       </h:column>
       <h:column>
         <f:facet name="header">Name</f:facet>#{c.name}
       </h:column>
      <h:column>
         <f:facet name="header">Address</f:facet>#{c.address}
       </h:column>        
       <h:column>
         <f:facet name="header">Created Date</f:facet>#{c.created_date}
       </h:column>        
     </h:dataTable>    
   </h:body>
</html>

更多Java资讯关注网站动态,或者来惠州北大青鸟新方舟校区了解一下。

Java11.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表