【问题标题】:Java Server Faces - How to display table from database? [closed]Java Server Faces - 如何显示数据库中的表? [关闭]
【发布时间】:2011-12-14 08:45:38
【问题描述】:

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

这是豆子:

/**     
 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 语句的教程

问候 彼得

【问题讨论】:

    标签: sql jsf jsf-2


    【解决方案1】:

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

    A. 关于数据表问题: 我建议您使用以下任一开源组件:

    1. PrimeFaces 其中有一个datatable
    2. OpenFaces 也有一个 datatable

    在每个展示案例中,您将看到如何展示它的示例。

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

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

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

    Hibernate getting started

    已编辑

    BalusC 曾向我指出,我还应该推荐简单的&lt;h:datatable&gt;。查看示例here

    【讨论】:

    • 为什么不只是标准的&lt;h:dataTable&gt;?为什么不只是 JPA? (OP 已经在使用 Java EE 6)。
    • @BalusC 他当然可以使用它。我将编辑我的答案。我只是相信他是一个初学者,很高兴知道其他可以简化他的开发的框架
    猜你喜欢
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多