Java Server Faces - 如何从数据库中显示表?

时间:2011-12-14 08:45:38

标签: sql jsf jsf-2

我有托管bean连接到数据库。你能告诉我如何从数据库中显示表USERS的行吗?

这是Bean:

/**     
 Description
 * Bean for checking users and passwords.
The password is converted into SHA-256 hash
 and compared with the hash from a database.
 If the check is successful the user is
 redirected to sr.xhtml page */

package com.dx.sr_57;
/** include default packages for Beans */
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
   // or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/** include package for SHA-256 encryption */
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import javax.annotation.Resource;
   // or import javax.faces.bean.ManagedBean;   


@Named("loginController")
@SessionScoped
public class user_check implements Serializable {
    private String user;
    private String password;    

       public user_check(){
       }

       /** Call the Oracle JDBC Connection driver */
       @Resource(name="java:/Oracle")
       private DataSource ds;

       /** get the content of the variables from the JSF Login page */
       public void setUser(String newValue) { 
           user = newValue; 
       }

       public String getUser(){
           return user;       
       }

       public void setPassword(String newValue) { 
           password = newValue; 
       } 

       public String getPassword(){
           return password;
       }

       /** method for converting simple string into SHA-256 hash */
       public String string_hash(String hash) throws NoSuchAlgorithmException{

            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(hash.getBytes());

            byte byteData[] = md.digest();

            /** convert the byte to hex format */
            StringBuilder sb = new StringBuilder();
                for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }              
           return sb.toString();
       }

       /** method for checking password into the Oracle database */
       public String CheckUserDB(String userToCheck) throws SQLException {
            String storedPassword = null;
            if (ds == null) throw new SQLException("No data source");      
       Connection conn = ds.getConnection();
            if (conn == null) throw new SQLException("No connection");      

       try {
            conn.setAutoCommit(false);
            boolean committed = false;
                try {
                       PreparedStatement passwordQuery = conn.prepareStatement(
                            "SELECT passwd from USERS WHERE userz = ?");
                       passwordQuery.setString(1, userToCheck);

                       ResultSet result = passwordQuery.executeQuery();

                       if(result.next()){
                            storedPassword = result.getString("passwd");
                       }

                       conn.commit();
                       committed = true;
                 } finally {
                       if (!committed) conn.rollback();
                       }
            }
                finally {               
                conn.close();

                }      
       return storedPassword;

       }       

       /** compare the user and the password */
       public String user_compare() throws NoSuchAlgorithmException, SQLException { 
            String hash_passwd;           
            String passwdQuery;

            /** check the password into Oracle using the username */
            passwdQuery = CheckUserDB(user);

            /** convert the plain password in SHA-256 hash */
            hash_passwd = string_hash(password);                                  

            if (password.equals(passwdQuery)){      // just for example, non encrypted passwords are compared
                return "success";        
            } else {
                return "failure";               
            }

       }            

}

您能否向我推荐一个很好的网站,其中包含如何使用Java Server Faces 2.0编写SQL语句的教程

此致 彼得

1 个答案:

答案 0 :(得分:2)

你在一篇文章中有两个问题我会回答:

关于数据表问题的

A。: 我建议您使用以下任一开源组件:

  1. PrimeFaces,其中包含datatable
  2. OpenFaces也有datatable
  3. 在每个展示案例中,您将看到如何显示它的示例。

    我建议先阅读每个的入门指南。

    如果你想比较它们this answer可以帮助你。

    B。 SQL语句与JSF无关。 JSF是一个Web框架,MVC。 SQL语句 - 将以与在纯java中选择的方式相同的方式使用。如果您正在寻找框架,Java中最常见的数据库方法是Hibernate。学习它可能需要一段时间,互联网上有很多教程,但它会缩短你的编码寿命。

    请参阅Hibernate getting started

    <强> EDITED

    BalusC向我指出我还应该推荐简单的<h:datatable>。查看示例here