2011-12-14 27 views
-1

我有託管bean連接到數據庫。你能告訴我如何從數據庫中顯示錶USERS的行嗎?Java Server Faces - 如何從數據庫中顯示錶?

這是豆:

/**  
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";    
      } 

     }    

} 

你能給我推薦一個好的網站有教程如何編寫SQL語句與Java服務器面臨2.0

問候 彼得

回答

2

你有兩個問題在一個職位,我會回答他們:

A. regar丁DataTable中的問題: 我會建議你使用任何以下開源組件:

  1. PrimeFaces具有datatable
  2. OpenFaces其中也有一個datatable

在每個展示案例中,您將看到示例如何展示它。

我會建議先閱讀每個入門指南。

如果你想比較他們this answer可以幫助你。

B. SQL語句與JSF沒有關係。 JSF是一個Web框架,MVC。 SQL語句 - 將以與您在純Java中選擇相同的方式使用。如果您正在尋找框架,Java中最常見的數據庫方法是Hibernate。學習它可能需要一段時間,並且互聯網上有很多教程,但它會減輕您的編碼時間。

Hibernate getting started

EDITED

BalusC曾指出,我認爲我也應該簡單<h:datatable>建議。看一個例子here

+0

爲什麼不只是標準的``?爲什麼不僅僅是JPA? (OP已經在使用Java EE 6)。 – BalusC 2011-12-14 11:48:47

相關問題