30 August 2016

JSF Java Select database Mysql Workbench Java NetBeans


index.xhtml
=======================Example JSF Select Database=====================
Create project




 Database
DBUtil.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());
//    }
}
  JSF ManagedBean : SelectManagerBean.java
@SessionScoped
SelectManagedBean.java
Java 2016
package controller;

import dao.BluewaveDAO;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import model.Bluewave;

/**
 *
 * @author Lonely
 */
@ManagedBean
@SessionScoped
public class SelectManagedBean {

   public ArrayList<Bluewave> getBluewaveManagerBean(){
        return new BluewaveDAO().getAllBluewave();
    }
}
Class: BluewaveDAO.java
BluewaveDAO.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 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());
//    }
}
 Class: Bluewave.java
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;
    }

}
Index.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;}
        </style>
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h:dataTable value="#{selectManagedBean.bluewaveManagerBean}" 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:dataTable>
            </h:form>
        </f:view>

    </h:body>
</html>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang