27 August 2016

Login JSF vs mysql demo Java NetBeans

index.xhtml
 confirm.xhtml
===============Demo JSF================
create project DemoJsf

Create JSF index.xhtml & confirm.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>User Manager Bean</title>
    </h:head>
    <h:body>
        <f:view>
            <h:form>
                <h:inputText  value="#{userManagerBean.user}"></h:inputText>
                <h:inputText  value="#{userManagerBean.name}"></h:inputText>
                <h:commandButton value="Login" action="#{userManagerBean.login}"></h:commandButton>
            </h:form>
        </f:view>
    </h:body>
</html>
Create ManagerBean UserManagerBean.java
UserManagerBean.java
Java 2016
package controller;

import dao.UserDAO;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import model.User;

/**
 *
 * @author Lonely
 */
@ManagedBean
@RequestScoped
public class UserManagerBean {

    private String user;
    private String name;
    List<User> list;

    public List<User> getList() {

        if (list == null) {
            list = new ArrayList();
        }
        User u = new User();
        u.setPass("12345");
        u.setUser("tony");
        list.add(u);
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getName() {
        return name;
    }

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

    public String login() {

        UserDAO u = new UserDAO();
        if (u.loginDemo(user, name)) {
            return "confirm";
        }

        FacesContext fc = FacesContext.getCurrentInstance();
        fc.addMessage(null, new FacesMessage("Sai name hoac pw"));
        return null;
    }

}
 Create class UserDAO.java

UserDAO.java
Java 2016
package dao;

import DBConnect.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

    public Boolean loginDemo(String user, String pass) {
        String sql = "SELECT * FROM product.`user` where pass = ? AND user = ?";
        try {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, pass);
            preparedStatement.setString(2, user);
            ResultSet rs = preparedStatement.executeQuery();
            int count = 0;
            while (rs.next()) {
                count++;
            }
            if (count == 1) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
//    Test loginDemo
//    public static void main(String[] args) {
//        UserDAO d = new UserDAO();
//        System.out.println(d.loginDemo("buny", "1235"));
//    }
}
 Create class User.java
User.java
Java 2016
package model;

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

    private String user;
    private String pass;

    public User() {
    }

    public User(String user, String pass) {
        this.user = user;
        this.pass = pass;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

}
Create slass DBUtil.java
Class: DBUtil.java
Java 2016
package DBConnect;

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

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

    private static Connection conn;
    private static String DRIVER = "com.mysql.jdbc.Driver";
    private static String URL = "jdbc:mysql://localhost:3306/product";
    private static String USER = "root";
    private static String PASS = "1234567";

    public static Connection getConnection() {
        try {
            Class.forName(DRIVER);
            conn = DriverManager.getConnection(URL, USER, PASS);
        } catch (Exception e) {
            System.out.println(e);
        }
        return conn;
    }

    //Run checking
//    public static void main(String[] args) {
//        getConnection();
//    }
}
Create JSF comfirm.xhtml

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang