30 August 2016

JSF Select Insert Update Delete Search database mysql workbench Java NetBeans




Creata Database name: product
Id Not Auto Increment 
Add data row
===============Example JSF Java==============
Create project 
Database
BDUtil.java
DBUtil.java
Java 2016
package Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author Lonely
 */
public class DBUtil {

    private static Connection connection = null;

    public static Connection getConnection() {
        if (connection != null) {
            return connection;
        } else {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/product", "root", "1234567");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }
    //Test connection
//    public static void main(String[] args) {
//        System.out.println(getConnection());
//    }
}
Bluewave.java
Java 2016
package model;
/**
 *
 * @author Lonely
 */
public class Bluewave {

    private int id;
    private String name, image, content;

    public Bluewave(int id, String name, String image, String content) {
        this.id = id;
        this.name = name;
        this.image = image;
        this.content = content;
    }
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}
BluewaveDAO.java 'showALl & showSearch'
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Bluewave;
/**
 *
 * @author Lonely
 */
public class BluewaveDAO {

    public ArrayList<Bluewave> getAllBluewave() {
        Connection conn = DBUtil.getConnection();
        ArrayList<Bluewave> list = new ArrayList();
        String sql = "SELECT * FROM product.bluewave";
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String image = rs.getString("image");
                String content = rs.getString("content");
                Bluewave b = new Bluewave(id, name, image, content);
                list.add(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    //Test BluewaveDAO
//    public static void main(String[] args) {
//        BluewaveDAO b = new BluewaveDAO();
//        System.out.println(b.getAllBluewave());
//    }

    public ArrayList<Bluewave> getAllByID(int id) {
        Connection conn = DBUtil.getConnection();
        ArrayList<Bluewave> list = new ArrayList();
        String sql = "SELECT * FROM product.bluewave Where id ='" + id + "'";
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int idb = rs.getInt("id");
                String name = rs.getString("name");
                String image = rs.getString("image");
                String content = rs.getString("content");
                Bluewave b = new Bluewave(idb, name, image, content);
                list.add(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    //Test BluewaveDAO
//    public static void main(String[] args) {
//        BluewaveDAO b = new BluewaveDAO();
//        System.out.println(b.getAllByID(1));
//    }
}
InsertDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.Bluewave;
/**
 *
 * @author Lonely
 */
public class InsertDAO {
    public boolean insertBluewave(Bluewave b) throws SQLException{
        Connection conn = DBUtil.getConnection();
        String sql ="INSERT INTO `product`.`bluewave` (`id`, `name`, `image`, `content`) VALUES (?, ?, ?, ?)";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setInt(1, b.getId());
        stmt.setString(2, b.getName());
        stmt.setString(3, b.getImage());
        stmt.setString(4, b.getContent());
        if (stmt == null) {
            throw new SQLException();
        } else {
            return stmt.executeUpdate() > 0;
        }
    }
    //Test InsertDAO
//    public static void main(String[] args) throws SQLException {
//        InsertDAO i = new InsertDAO();
//        Bluewave bb = new Bluewave(100,"Giaima","Blogspot","com");
//        System.out.println(i.insertBluewave(bb));
//    }
}
UpdateDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import model.Bluewave;
/**
 *
 * @author Lonely
 */
public class UpdateDAO {

    public boolean updateBluewave(Bluewave b, int id) throws SQLException {
        Connection conn = DBUtil.getConnection();
        String sql = "UPDATE `product`.`bluewave` SET `name`=?, `image`=?, `content`=? WHERE `id`='"+id+"'";
        PreparedStatement stmt = conn.prepareStatement(sql);
        stmt.setString(1, b.getName());
        stmt.setString(2, b.getImage());
        stmt.setString(3, b.getContent());
        if (stmt == null) {
            throw new SQLException();
        } else {
            return stmt.executeUpdate() > 0;
        }
    }
    //Test UpdateBluewave
//    public static void main(String[] args) throws SQLException {
//        UpdateDAO u = new UpdateDAO();
//        System.out.println(u.updateBluewave(new Bluewave("hello","hello","giaima"), 1));
//    }
}
DeleteDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
 *
 * @author Lonely
 */
public class DeleteDAO {
    public boolean deleteBluewave(int id) throws SQLException{
        Connection conn = DBUtil.getConnection();
        String sql = "Delete From product.bluewave Where id='"+id+"'";
        PreparedStatement stmt = conn.prepareStatement(sql);
        if (stmt == null) {
            throw new SQLException();
        } else {
            return stmt.executeUpdate() > 0;
        }
    }
    //Test DeleteDAO       
//    public static void main(String[] args) throws SQLException {
//        DeleteDAO d = new DeleteDAO();
//        System.out.println(d.deleteBluewave(11));
//    }
}
SearchDAO.java
Java 2016
package dao;

import Util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import model.Bluewave;
/**
 *
 * @author Lonely
 */
public class SearchDAO {
    public ArrayList<Bluewave> SearchBluewave(String key) {
        Connection conn = DBUtil.getConnection();
        ArrayList<Bluewave> list = new ArrayList();
        String sql = "SELECT * FROM product.bluewave Where name LIKE ?";
        try {
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setString(1, "%"+key+"%");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String image = rs.getString("image");
                String content = rs.getString("content");
                Bluewave b = new Bluewave(id, name, image, content);
                list.add(b);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    //Test SearchDB
//    public static void main(String[] args) throws SQLException {
//        SearchDAO s = new SearchDAO();
//        System.out.println(s.SearchBluewave("Navi"));
//    }
}

Create BluewaveManagedBean.java


@SessionScoped
BluewaveManagedBean.java
Java 2016
package controller;

import dao.BluewaveDAO;
import dao.DeleteDAO;
import dao.InsertDAO;
import dao.SearchDAO;
import dao.UpdateDAO;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Bluewave;

/**
 *
 * @author Lonely
 */
@ManagedBean
@SessionScoped
public class BluewaveManagedBean {
    
    private int id;
    private String name, imageurl, content, search;

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImageurl() {
        return imageurl;
    }

    public void setImageurl(String imageurl) {
        this.imageurl = imageurl;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
    //Show all
    public ArrayList<Bluewave> getBluewaveShow() {
        return new BluewaveDAO().getAllBluewave();
    }
    //Show by id
    public void getAllByIDShow(int id, String name, String imageurl, String content) {
        this.id = id;
        this.name = name;
        this.imageurl = imageurl;
        this.content = content;
    }
    //Reset setter null;
    public void resetValue(){
        //Reset update
        this.id = 0;
        this.name = "";
        this.imageurl = "";
        this.content = "";
    }
    //Insert database
    public boolean insertB() throws SQLException {
        Bluewave b = new Bluewave(id, name, imageurl, content);
        return new InsertDAO().insertBluewave(b);
    }
    //Update database by id
    public boolean updateB() throws SQLException {
        Bluewave b = new Bluewave(id, name, imageurl, content);
        return new UpdateDAO().updateBluewave(b, id);
    }
    //Delet database by id
    public boolean deleteB(int id) throws SQLException {
        return new DeleteDAO().deleteBluewave(id);
    }
    //Search database by name
    public ArrayList<Bluewave> getSearchBluewave() {
        return new SearchDAO().SearchBluewave(search);
    }
}
Create file.xhtml
index.xhtml
Java 2016
<?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"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Select Database Mysql JSF Java</title>
        <style>
            tr,th,td{border: solid 1px}
            table{width: 70%; margin: auto;}
            form,h1{text-align: center}
        </style>
    </h:head>
    <h:body>
        <h1>Index Home</h1>
        <f:view>
            <h:form>
                <h:dataTable value="#{bluewaveManagedBean.bluewaveShow}" var="item">
                    
                    <h:column>
                        <f:facet name="header">ID</f:facet>
                        <h:outputText value="#{item.id}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Name</f:facet>
                        <h:outputText value="#{item.name}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">URL Image</f:facet>
                        <h:outputText value="#{item.image}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Content</f:facet>
                        <h:outputText value="#{item.content}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header"><h:commandLink value="Insert" action="insert" actionListener="#{bluewaveManagedBean.resetValue()}"></h:commandLink></f:facet>
                        <h:commandLink value="Update" actionListener="#{bluewaveManagedBean.getAllByIDShow(item.id, item.name, item.image, item.content)}" action="update"></h:commandLink><br></br>
                        <h:commandLink value="Delete" actionListener="#{bluewaveManagedBean.deleteB(item.id)}" action="index"></h:commandLink>
                    </h:column>
                </h:dataTable>
            </h:form>
        </f:view>
        <f:view>
            <h:form>
                    <h:commandLink action="search" value="Search by name"></h:commandLink>
            </h:form>
        </f:view>

    </h:body>
</html>
insert.xhtml
Java 2016
<?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"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Insert Bluewave</title>
        <style>
            body{margin: auto; width: 30%;}
            textarea{width: 298px; height: 111px;}
        </style>
    </h:head>
    <h:body>
        <f:view >
            <h:form >
                <br><h1>Insert Bluewave</h1></br>
                ID:<br><h:inputText value="#{bluewaveManagedBean.id}"></h:inputText></br>
                Name:<br><h:inputText value="#{bluewaveManagedBean.name}"></h:inputText></br>
                Image URL:<br><h:inputText value="#{bluewaveManagedBean.imageurl}"></h:inputText></br>
                Content:<br><h:inputTextarea value="#{bluewaveManagedBean.content}"></h:inputTextarea></br>
                <h:commandButton value="Insert" actionListener="#{bluewaveManagedBean.insertB()}" action="index"></h:commandButton>
                //actionListener là di chuyển đến ManagerBean
                //action là di chuyển đến trang index.xhtml
            </h:form>
        </f:view>
    </h:body>
</html>
update.xhtml
Java 2016
<?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"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Update Bluewave</title>
        <style>
            body{margin: auto; width: 30%;}
            textarea{width: 298px; height: 111px;}
        </style>
    </h:head>
    <h:body >
        <f:view >
            <h:form >
                <br><h1>Update Bluewave</h1></br>
                ID:<br><h:inputText value="#{bluewaveManagedBean.id}"></h:inputText></br>
                Name:<br><h:inputText value="#{bluewaveManagedBean.name}"></h:inputText></br>
                Image URL:<br><h:inputText value="#{bluewaveManagedBean.imageurl}"></h:inputText></br>
                Content:<br><h:inputTextarea value="#{bluewaveManagedBean.content}"></h:inputTextarea></br>
                <h:commandButton value="Update" actionListener="#{bluewaveManagedBean.updateB()}" action="index"></h:commandButton>
            </h:form>
        </f:view>
    </h:body>
</html>
search.xhtml
Java 2016
<?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"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Search</title>
        <style>
            tr,th,td{border: solid 1px}
            table{width: 70%; margin: auto;}
            form,h1{text-align: center}
        </style>
    </h:head>
    <h:body>
        <h1>Search by name</h1>
       <f:view>
            <h:form>
                <h:inputText value="#{bluewaveManagedBean.search}"></h:inputText>
                <h:commandButton value="Searh" ></h:commandButton>
            </h:form>
        </f:view>
        <br></br>
         <f:view>
            <h:form>
                <h:dataTable value="#{bluewaveManagedBean.searchBluewave}" var="itemSearch">
                    
                    <h:column>
                        <f:facet name="header">ID</f:facet>
                        <h:outputText value="#{itemSearch.id}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Name</f:facet>
                        <h:outputText value="#{itemSearch.name}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">URL Image</f:facet>
                        <h:outputText value="#{itemSearch.image}"></h:outputText>
                    </h:column>
                    <h:column>
                        <f:facet name="header">Content</f:facet>
                        <h:outputText value="#{itemSearch.content}"></h:outputText>
                    </h:column>
                    
                </h:dataTable>
            </h:form>
        </f:view>
    </h:body>
</html>
Download zip (Import NetBeans)

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang