Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6934749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:00:03+00:00 2026-05-27T12:00:03+00:00

I have managed bean connected to database. Can you show me how to display

  • 0

I have managed bean connected to database. Can you show me how to display the rows of the table USERS from the database?

This is the 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";               
            }

       }            

}

Can you recommend me a good web site with tutorials how to write SQL statements with Java Server Faces 2.0

Regards
Peter

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:00:04+00:00Added an answer on May 27, 2026 at 12:00 pm

    You have two questions in one post and I will answer them:

    A. regarding the datatable issue:
    I would recommend to you to use either of the below open source components:

    1. PrimeFaces which has a datatable
    2. OpenFaces which also has a datatable.

    Inside each of the show case you will see examples how of to display it.

    I would recommend first to read the getting started guide of each.

    If you want to compare them this answer can help you.

    B. SQL statement has not got to do with JSF. JSF is a web framework, MVC. SQL statements -will be used as in the same way that you would choose in pure java. If you are looking for a framework, the most common one for database approach in Java is Hibernate. It may take you a while to learn it, and there are many tutorials on the internet but it will ease your coding life.

    See Hibernate getting started

    EDITED

    BalusC had pointed out to me that I should recommend also the simple <h:datatable>. See an example here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this managed bean which takes two values from database's table import java.io.Serializable;
I have this managed bean which makes SQL queries to Oracle database and returns
I have this managed bean: package com.DX_57.SR_57; /** include default packages for Beans */
I have this java code which is a part of a managed bean which
I have bean that i recently converted over from being a managed-bean to being
I have a JSF component which is initialized from a managed bean's getter getProperty()
I have one method in my managed bean which returns javascript as a string.
Is it possible to have a managed bean created only on some pages i.e.
I am new to JSF and managed beans. I have a managed bean with
I'm using JSF 2.0 with GlassFish 3.0. I have the following Managed Bean: @ManagedBean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.