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

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

JSF JDBC連接

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

JSF + MySQL JDBC實(shí)例

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

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

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

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

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

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

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

create database test;

在這個(gè)數(shù)據(jù)中,創(chuàng)建一個(gè)表: 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)建一個(gè)用戶表單來獲取輸入的用戶信息。

創(chuàng)建用戶表單

創(chuàng)建一個(gè)名稱為: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)建一個(gè)委托Bean

此文件還包含屬性,數(shù)據(jù)庫連接和頁面導(dǎo)航。創(chuàng)建一個(gè)名稱為: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)建響應(yīng)結(jié)果頁面

創(chuàng)建一個(gè)名稱為: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>

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

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

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