JSF JDBC Connection

You can integrate JSF application to the jdbc. JDBC allows you to store data into the database table.

In this tutorial, we are creating an application and creating jdbc connectivity to store data input by the user.

This application contains a user input form, a managed bean, a response page and the following steps.


# Create Database and Table

we have used mysql database to create database and table.

# Create Database


create database Emp;
JSF Jdbc 1

# Select Database


use Emp

# Create table


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

# See Table Structure


desc user;
JSF Jdbc 3

After creating database and table, now create a user form to get user input.

# Creating a Form

// 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>

# Create a Manged Bean

This file contains properties, database connection and page navigation as well.

// User.java


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/Emp","root","mysql");
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 "response.xhtml";
}else return "index.xhtml";
}   
}

# Create a Response Page

// response.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>

Output:

User Form (Index Page)

JSF Jdbc 4

Response Page

JSF Jdbc 5

Now, you can see data into the table.


select * from user
JSF Jdbc 6

All set! Data has been inserted.