Table of contents
1.
Introduction
2.
Implementation
2.1.
Creating the Database
2.2.
Creating the Form
2.2.1.
index.xhtml
2.3.
Creating the Managed Bean
2.4.
user.java
2.5.
Creating a Response
2.5.1.
response.xhtml
2.6.
Output
3.
Frequently Asked Questions
3.1.
What is JDBC integration?
3.2.
What are the JDBC API components?
3.3.
What is the JSF form?
3.4.
What is a JSF tag?
4.
Conclusion
Last Updated: Mar 27, 2024

JDBC Integration in JSF

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaServer Faces or JSF is a new Java standard technology for building component-based and event-oriented web interfaces. Similar to JavaServer Pages (JSP), JSF allows server-side access to data and logic.

JDBC(Java Database Connectivity) is a Java API that we use to connect and execute the query with a Database. It comes as a part of JavaSE (Java Standard Edition). This article will show how you can integrate databases in JSF using JDBC.

Implementation

To demonstrate the integration of databases in JSF using JDBC, we will firstly create a database using tools like MySQL or SQLite. Then we will create a .xhtml form allowing users to edit the database and add data. We will use a Java program file to implement this with the help of JDBC.

So without further ado, let's get started with the implementation.

Creating the Database

We will start by creating a database using mysql on the terminal with the following command.

create database Emp; 

 

 

After that, we will select the database that we just created.

use Emp 

 

 

Now we need to create a table in the database. You can do this using the given command on the terminal.

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

 


 

You can also view the table.

desc user; 

 

 

Having created a database, you can now go forward to the form, managed bean, and response code to demonstrate the JDBC integration in JSF.

Creating the Form

We will create a .xhtml form that will allow the user to enter data that they want to insert to the database we created above.

index.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:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/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> 

Creating the Managed Bean

The Java code for the managed bean is where we will include properties, database connection and page navigation. This program will also include the implementation of the JDBC connection in JSF.

user.java

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import javax.faces.bean.ReferencedBean;  
import javax.faces.bean.ManagedBean;  


     
@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";  
}     
} 
You can also try this code with Online Java Compiler
Run Code

Creating a Response

Finally, we can write an elementary xhtml code for a response page that confirms that the data has been inserted successfully to the database.

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:f = "http://java.sun.com/jsf/core"    
   xmlns:h = "http://java.sun.com/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 is Saved Successfully!"/>  
</h:body>  
</html> 

Output

 

 

And just like that! You have successfully inserted new data into your database table. You can also check to see if the data has been inserted successfully or not. If all is done right, the following command should show your table with the new data you created.

select * from user 

Frequently Asked Questions

What is JDBC integration?

In simple words, JDBC(Java Database Connectivity) is an API specification that defines how you can interact with corporate data sources from Java applets, applications, and servlets and how to use JDBC drivers.

What are the JDBC API components?

JDBC has these four major components that we can use to interact with the database.

  • JDBC Test Suite.
  • JDBC ODBC Bridge Driver.
  • JDBC API.
  • JDBC Driver Manager.

What is the JSF form?

The <h:form> tag in JSF represents an input form with child components that may contain data presented to the user or submitted with the form. It might also include HTML markup laying out the components on the page.

What is a JSF tag?

JSF provides a standard HTML tag library. These tags render into corresponding Html output. For these tags, we need to use some namespaces of URI in the Html node.

Conclusion

Throughout this article, we have understood the implementation to integrate JDBC in JSF with the help of an example. We hope this article helped you in your journey towards mastering JSF. You can also learn more about JDBC through articles like Introduction to JDBC and JDBC Connection in Java.

Please see our Code studio guided routes to learn more about DSA, Competitive Programming, JavaScript, System Design, and other topics. Also, enroll in our courses and use the accessible sample tests and problems. For placement preparations, have a look at the interview experiences and interview bundle.

Happy Learning!

Live masterclass