在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ JSF JDBC連接
JSF數(shù)據(jù)表(h:dataTable)添加刪除
JSF <h:commandLink>標簽
JSF應用程序入門示例
JSF數(shù)據(jù)表(ui:repeat)創(chuàng)建表
JSF列表框
JSF數(shù)據(jù)表(h:dataTable)DataModel排序數(shù)據(jù)
JSF復合組件
JSF <h:inputText>標簽
JSF表單組合框
JSF <h:messages>標簽
JSF <h:message>標簽
JSF轉換日期時間
JSF JDBC連接
JSF <h:inputHidden>標簽
JSF多選列表框
JSF <h:inputSecret>標簽
JSF自定義轉換器
JSF <f:ajax>標簽
JSF生命周期
JSF可重定位資源
JSFJSF用戶界面組件模型
JSF <h:attribute>標簽
JSF驗證器標簽
JSF驗證字符串長度
JSF轉換器標簽
JSF托管bean(Managed Bean)
JSF值變化的事件
JSF UI組件示例
JSF MySQL CURD實例
JSF數(shù)據(jù)表(h:dataTable)排序數(shù)據(jù)
JSF <h:graphicImage>標簽
JSF <f:convertNumber>標簽
JSF教程
JSF托管Bean
JSF輸出腳本
JSF <h:outputText>標簽
JSF操作事件
JSF驗證正則表達式
JSF數(shù)據(jù)表(h:dataTable)行號
JSF <h:setPropertyActionListener>標簽
JSF注入托管bean實例
JSF <h:commandButton>標簽
JSF Web資源
JSF <h:inputFile>標簽
JSF驗證浮點數(shù)值范圍
JSF Facelets視圖
JSF是什么?
JSF Facelets模板
JSF的特性(特點)
JSF自定義驗證器類
JSF單選按鈕
JSF輸出樣式
JSF數(shù)據(jù)表(h:dataTable)更新數(shù)據(jù)
JSF HTML5友好標記
JSF表單復選框(CheckBox)示例
JSF <h:form>標簽
JSF Facelets技術介紹
JSF輸出格式化
JSF <h:inputtextarea>標簽
JSF驗證整數(shù)范圍
JSF <h:panelGrid>標簽

JSF JDBC連接

我們可以將JSF應用程序集成到jdbc。 JDBC可將數(shù)據(jù)存儲到數(shù)據(jù)庫表中。在本教程中,我們創(chuàng)建一個應用程序并創(chuàng)建jdbc連接來存儲用戶輸入的數(shù)據(jù)。

JSF + MySQL JDBC實例

打開 NetBeans IDE,創(chuàng)建一個名稱為:JdbcConnectivity 的 Web 工程,其目錄結構如下所示 -

提示: 需要加入 mysql-connector-java Jar包。

此應用程序包含用戶輸入表單,委托bean和響應頁面,如以下步驟。

創(chuàng)建數(shù)據(jù)庫和表

我們使用mysql數(shù)據(jù)庫創(chuàng)建數(shù)據(jù)庫和表。

創(chuàng)建數(shù)據(jù)庫

使用以下SQL命令,創(chuàng)建一個數(shù)據(jù)庫:test ;

create database test;

在這個數(shù)據(jù)中,創(chuàng)建一個表: user -

create table user(
    id int not null auto_increment primary key, 
    name varchar(100) not null, 
    email varchar(50) not null 
);

創(chuàng)建數(shù)據(jù)庫和表后,現(xiàn)在創(chuàng)建一個用戶表單來獲取輸入的用戶信息。

創(chuàng)建用戶表單

創(chuàng)建一個名稱為:index.xhtml 的文件,其代碼如下所示 -

<!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">  
    <h:head>  
        <title>User Form</title>  
    </h:head>  
    <h:body>  
        <h:form>  
            <h:outputLabel for="username" value="User Name "/>  
            <h:inputText id="username" value="#{user.userName}">  
            </h:inputText><br/>  
            <h:outputLabel for="email" value="Email ID "/>  
            <h:inputText id="email" value="#{user.email}">  
            </h:inputText><br/><br/>  
            <h:commandButton action="#{user.submit()}" value="submit"/>  
        </h:form>  
    </h:body>  
</html>

創(chuàng)建一個委托Bean

此文件還包含屬性,數(shù)據(jù)庫連接和頁面導航。創(chuàng)建一個名稱為:User.java 的文件,其代碼如下所示 -

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yiibai;

/**
 *
 * @author Maxsu
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ReferencedBean;

@ManagedBean
@ReferencedBean
public class User {

    String userName;
    String email;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean save() {
        int result = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "123456");
            PreparedStatement stmt = con.prepareStatement("insert into user(name,email) values(?,?)");
            stmt.setString(1, this.getUserName());
            stmt.setString(2, this.getEmail());
            result = stmt.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        }
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    public String submit() {
        if (this.save()) {
            return "result.xhtml";
        } else {
            return "index.xhtml";
        }
    }
}

創(chuàng)建響應結果頁面

創(chuàng)建一個名稱為:result.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">  
    <h:head>  
        <title>Response Page</title>  
    </h:head>  
    <h:body>  
        <h1><h:outputText value="Hello #{user.userName}"/></h1>  
        <h:outputText value="Your Record has been Saved Successfully!"/>  
    </h:body>  
</html>

運行上面項目,打開瀏覽器訪問:http://localhost:8084/JdbcConnectivity/,并提交表單,向數(shù)據(jù)庫表中寫入數(shù)據(jù) -

向上面表單中,寫入一個示例數(shù)據(jù),提示得到以下結果 -

現(xiàn)在,您可以看到表:user 中的數(shù)據(jù),應該會看到上面表單提交的數(shù)據(jù)了 -