JSF JDBC Connection

JSF JDBC Connectivity example for beginners and professionals with examples on life cycle, managed beans, ui components, ui tags, validation, bean validation, datatable, converters, facelets etc

JSF JDBC Connection

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

  1. create database Emp; 

Select Database

  1. use Emp  

Create table

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

See Table Structure

  1. desc user; 

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

Creating a Form

// index.xhtml

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml"  
  3. xmlns:h="http://xmlns.jcp.org/jsf/html">  
  4. <h:head>  
  5. <title>User Form</title>  
  6. </h:head>  
  7. <h:body>  
  8. <h:form>  
  9. <h:outputLabel for="username" value="User Name "/>  
  10. <h:inputText id="username" value="#{user.userName}">  
  11. </h:inputText><br/>  
  12. <h:outputLabel for="email" value="Email ID "/>  
  13. <h:inputText id="email" value="#{user.email}">  
  14. </h:inputText><br/><br/>  
  15. <h:commandButton action="#{user.submit()}" value="submit"/>  
  16. </h:form>  
  17. </h:body>  
  18. </html>  

Create a Manged Bean

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

// User.java

  1. import java.sql.Connection;  
  2. import java.sql.DriverManager;  
  3. import java.sql.PreparedStatement;  
  4. import javax.faces.bean.ManagedBean;  
  5. import javax.faces.bean.ReferencedBean;  
  6.   
  7. @ManagedBean  
  8. @ReferencedBean  
  9. public class User {  
  10. String userName;  
  11. String email;  
  12. public String getUserName() {  
  13. return userName;  
  14. }  
  15. public void setUserName(String userName) {  
  16. this.userName = userName;  
  17. }  
  18. public String getEmail() {  
  19. return email;  
  20. }  
  21. public void setEmail(String email) {  
  22. this.email = email;  
  23. }  
  24.   
  25.   
  26. public boolean save(){  
  27. int result = 0;  
  28. try{  
  29. Class.forName("com.mysql.jdbc.Driver");     
  30. Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/Emp","root","mysql");  
  31. PreparedStatement stmt = con.prepareStatement("insert into user(name,email) values(?,?)");  
  32. stmt.setString(1this.getUserName());  
  33. stmt.setString(2this.getEmail());  
  34. result = stmt.executeUpdate();  
  35. }catch(Exception e){  
  36. System.out.println(e);  
  37. }  
  38. if(result == 1){  
  39. return true;  
  40. }else return false;  
  41. }  
  42.   
  43. public String submit(){  
  44. if(this.save()){  
  45. return "response.xhtml";  
  46. }else return "index.xhtml";  
  47. }     
  48. }  

Create a Response Page

// response.xhtml

  1. <?xml version='1.0' encoding='UTF-8' ?>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml"  
  4. xmlns:h="http://xmlns.jcp.org/jsf/html">  
  5. <h:head>  
  6. <title>Response Page</title>  
  7. </h:head>  
  8. <h:body>  
  9. <h1><h:outputText value="Hello #{user.userName}"/></h1>  
  10. <h:outputText value="Your Record has been Saved Successfully!"/>  
  11. </h:body>  
  12. </html>  

Output:

User Form (Index Page)

Response Page

Now, you can see data into the table.

  1. select * from user 

All set! Data has been inserted.