2011-12-14 5 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";    
      } 

     }    

} 

は、あなたは、Java Serverが2.0

よろしく ピーター

答えて

2

次の2つを持っているに直面してSQL文を書く方法のチュートリアルで私によいウェブサイトをお勧めすることができます:

この

は豆です1つの投稿の質問と私はそれらに答えます:

A. regar鼎のDataTable問題:

  1. PrimeFacesdatatableを持ってdatatable
  2. OpenFacesがあります は、私は以下のオープンソースコンポーネントのいずれかを使用することをあなたにお勧めします。

ショーケースのそれぞれには、どのように表示するかの例が表示されます。

まず、それぞれのスタートガイドを読むことをお勧めします。

あなたがそれらを比較したい場合は、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

+0

@BalusCもちろん彼はそれを使うことができます。私は私の答えを編集します。私は彼が初心者だと信じており、彼の発展を容易にする他のフレームワークを知っているのは良いことです – Dejell

関連する問題