我們可以將JSF應(yīng)用程序集成到j(luò)dbc。 JDBC可將數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫表中。在本教程中,我們創(chuàng)建一個(gè)應(yīng)用程序并創(chuàng)建jdbc連接來存儲(chǔ)用戶輸入的數(shù)據(jù)。
打開 NetBeans IDE,創(chuàng)建一個(gè)名稱為:JdbcConnectivity 的 Web 工程,其目錄結(jié)構(gòu)如下所示 -

提示: 需要加入
mysql-connector-javaJar包。
此應(yīng)用程序包含用戶輸入表單,委托bean和響應(yīng)頁面,如以下步驟。
我們使用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)建一個(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>
此文件還包含屬性,數(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)建一個(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ù)了 -
