• Core Java
  • PHP
  • jQuery
  • Spring
  • Java IO
  • Servlet
  • RubyonRails
  • JSP
  • JavaScript

  • <

    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.

    Example



    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



    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","password");  
    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>  
    
















    © copyright 2017-2021 Completedone pvt ltd.