jsf insert data into database table.pdf

8
JSF Insert Data Into Database Table To insert the data into a table at first we will required to create a table in the database. Here I have created the table as follows : CREATE TABLE `user1` ( `userId` bigint(10) NOT NULL, `name` varchar(15) NOT NULL, `address` varchar(255) NOT NULL, `created_date` date NOT NULL, PRIMARY KEY (`userId`) ) Then I have created a project in Eclipse IDE where I have created a class named User.java which contains the data member userID, name, address, created_date, and their setter getter methods then created a method named add() where write the code for inserting the data. Then I have created an XHTML page named insert.xhtml using which I have created an User Interface to provide the data to insert into the table. When you will try to insert the data to the table after executing this example and if the data is added successfully to the table the add function will return the String 'output' and it will redirect to the output.xhtml page and if the data is not added to the table successfully then the add() function will return the String 'invalid' and it will redirect to the invalid.xhtml page. For the page redirection I have created a navigation-rule in faces-config.xml file Directory Structure User.java package com.dev.user.model; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped;

Upload: riadelidrissi

Post on 13-Sep-2015

241 views

Category:

Documents


4 download

TRANSCRIPT

  • JSF Insert Data Into Database Table

    To insert the data into a table at first we will required to create a table in the database. Here I have created the

    table as follows :

    CREATE TABLE `user1` (

    `userId` bigint(10) NOT NULL,

    `name` varchar(15) NOT NULL,

    `address` varchar(255) NOT NULL,

    `created_date` date NOT NULL,

    PRIMARY KEY (`userId`)

    )

    Then I have created a project in Eclipse IDE where I have created a class named User.java which contains the

    data member userID, name, address, created_date, and their setter getter methods then created a method

    named add() where write the code for inserting the data. Then I have created an XHTML page named insert.xhtml

    using which I have created an User Interface to provide the data to insert into the table. When you will try to insert

    the data to the table after executing this example and if the data is added successfully to the table the add

    function will return the String 'output' and it will redirect to the output.xhtml page and if the data is not added to the

    table successfully then the add() function will return the String 'invalid' and it will redirect to the invalid.xhtml page.

    For the page redirection I have created a navigation-rule in faces-config.xml file

    Directory Structure

    User.java

    package com.dev.user.model;

    import javax.faces.application.FacesMessage;

    import javax.faces.bean.ManagedBean;

    import javax.faces.bean.RequestScoped;

  • import javax.faces.context.FacesContext;

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.text.SimpleDateFormat;

    import java.util.Date;

    @ManagedBean(name="user")

    @RequestScoped

    public class User {

    private long userID;

    private String name;

    private String address;

    private Date created_date;

    public long getUserID() {

    return userID;

    }

    public void setUserID(long userID) {

    this.userID = userID;

    }

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public String getAddress() {

    return address;

    }

    public void setAddress(String address) {

    this.address = address;

    }

    public Date getCreated_date() {

    return created_date;

    }

    public void setCreated_date(Date created_date) {

    this.created_date = created_date;

    }

    public String add()

    {

    SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");

    int i = 0;

    if(userID !=0)

  • {

    PreparedStatement ps = null;

    Connection con = null;

    try

    {

    Class.forName("com.mysql.jdbc.Driver");

    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/record", "root",

    "root");

    String sql = "INSERT INTO user1(userId, name, address, created_date)

    VALUES(?,?,?,?)";

    ps= con.prepareStatement(sql);

    ps.setLong(1, userID);

    ps.setString(2, name);

    ps.setString(3, address);

    if(created_date != null)

    {

    String date = fmt.format(created_date);

    Object obj = date;

    if(obj == null)

    {

    ps.setDate(4, null);

    }

    else

    {

    java.sql.Date dt = java.sql.Date.valueOf(new String(date));

    ps.setDate(4, dt);

    }

    }

    i = ps.executeUpdate();

    System.out.println("Data Added Successfully");

    }

    catch(Exception e)

    {

    System.out.println(e);

    }

    finally

    {

    try

    {

    con.close();

    ps.close();

    }

    catch(Exception e)

    {

    e.printStackTrace();

    }

    }

    if(i >0)

  • {

    return "output";

    }

    else

    {

    return "invalid";

    }

    }

    else

    {

    return "invalid";

    }

    }

    }

    insert.xhtml

    Insert Data

  • output.xhtml

    invalid.xhtml

    Try Again

  • web.xml

    jsfJdbcInsert

    /insert.xhtml

    Faces Servlet

    javax.faces.webapp.FacesServlet

    1

    Faces Servlet

    /faces/*

    *.jsf

    *.xhtml

    State saving method: 'client' or 'server' (=default). See JSF

    Specification 2.5.2

    javax.faces.STATE_SAVING_METHOD

    client

    javax.servlet.jsp.jstl.fmt.localizationContext

    resources.application

    com.sun.faces.config.ConfigureListener

    Faces Servlet

    *.faces

    faces-config.xml

  • xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"

    version="2.0">

    user

    com.dev.user.model.User

    request

    add user

    /insert.xhtml

    #{user.add}

    output

    /output.xhtml

    #{user.add}

    invalid

    /invalid.xhtml

    Output

    When you will execute the above example you will get the output as follows :

    When you will provide the value to the textboxes then the output will be as follows :

  • When you will click on inset button then the output will be as follows :

    An error page like if you will try to reinsert the value with duplicate id in the table then the output will be as follows

    :

    And at the console you will see the message as follows :