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 7619127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:36:13+00:00 2026-05-31T03:36:13+00:00

I have a managed bean which gets list of settings and values from database

  • 0

I have a managed bean which gets list of settings and values from database table.

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
// or import javax.faces.bean.SessionScoped;
import javax.inject.Named;
/* include SQL Packages */
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.sql.DataSource;
import javax.annotation.Resource;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
// or import javax.faces.bean.ManagedBean;   

import org.glassfish.osgicdi.OSGiService;

@Named("DashboardController")
@SessionScoped
public class Dashboard implements Serializable {

    private String SessionTTL = null;
    private String MaxActiveUsers = null;
    private String SQL_Statement = null;
    private HashMap<String, String> settingsMap = null;

    public Dashboard() throws SQLException
    {
        initSettings();
        /*try { initSettings(); }
        catch (SQLException ex) { ex.printStackTrace(); }
         * 
         */
    }

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



    public String settingValue(String key)
    {
        try
        {
            return (String) settingsMap.get(key);

        }
        catch(Exception x) { return "error - " + x.getMessage(); }

    }



    public void initSettings() throws SQLException
    {

        settingsMap = new HashMap<String, String>();

        if(ds == null) {
                throw new SQLException("Can't get data source");
        }


        Connection conn = ds.getConnection(); 

        if(conn == null) {
                throw new SQLException("Can't get database connection");
        }

        PreparedStatement ps = conn.prepareStatement("SELECT * from GLOBALSETTINGS");

        try
        {
            //get data from database        
            ResultSet result = ps.executeQuery();
            while (result.next())
            {
               settingsMap.put(result.getString("SettingName"), result.getString("SettingValue"));
            }

        }
        finally
        {
            ps.close();
            conn.close();
        }


    }

}

When I run the code I get this error stack in Glassfish

  com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.DX_57.SM_57.Dashboard.
        at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
        at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
        at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
        at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
        at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
        at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
        at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
        at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
        at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:103)
        at com.sun.el.parser.AstValue.getValue(AstValue.java:179)
        at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:224)
        at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
        at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
        at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
        at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:302)
        at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
        at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
        at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
        at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
        at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1757)
        at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1760)
        at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:402)
        at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
        at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
        at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
        at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
        at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
        at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
        at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
        at java.lang.Thread.run(Thread.java:722)
    Caused by: java.sql.SQLException: Can't get data source
        at com.DX_57.SM_57.Dashboard.initSettings(Dashboard.java:162)
        at com.DX_57.SM_57.Dashboard.<init>(Dashboard.java:47)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
        at java.lang.Class.newInstance0(Class.java:372)
        at java.lang.Class.newInstance(Class.java:325)
        at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
        ... 52 more

I’m trying to initialize a hashmap in the bean constructor and then call
settingValue method in jsf page passing a string parameter.

<h:panelGroup>Key</h:panelGroup>
<h:panelGroup>#{DashboardController.settingValue(SessionTTL)}
</h:panelGroup>

Is it possible to initialize the data source prior invoking bean’s constructor.
How I can make SQL queries right after bean initialization.

  • 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-31T03:36:14+00:00Added an answer on May 31, 2026 at 3:36 am

    Is it possible to initialize the data source prior invoking bean’s constructor. How I can make SQL queries right after bean initialization.

    That’s not possible. The dependency resources can only be injected after construction of the instance. Without having a concrete instance, there’s simply nothing where the resources can be injected in.

    To perform actions directly after dependency injection, the @PostConstruct annotation is been invented. You need to use it instead of the constructor.

    Replace

    public Dashboard() throws SQLException {
        initSettings();
    }
    
    public void initSettings() throws SQLException {
        // ...
    }
    

    by

    @PostConstruct
    public void initSettings() throws SQLException {
        // ...
    }
    

    A method annotated with @PostConstruct is invoked directly after dependency injection as is been performed by @Resource, @Inject, @EJB, @ManagedProperty, etc.


    Unrelated to the concrete problem, the way how you access a map value with an additional method is unnecessarily overcomplicated. Just have a simple getter for it

    public Map<String, String> getSettings() {
        return settingsMap;
    }
    

    and access the map values as follows:

    #{DashboardController.settings['SessionTTL']}
    

    or when the map key doesn’t contain periods:

    #{DashboardController.settings.SessionTTL}
    

    See also:

    • Our EL wiki page

    By the way, starting instance names with an uppercase like #{DashboardController} instead of lowercase like #{dashboardController} (or just the default #{dashboard}) is also a poor coding convention.

    • 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 a list of constant values which are stored in a database table.
I have a situation with a legacy system which uses Java EE Bean Managed
I have this managed bean which makes SQL queries to Oracle database and returns
I have a managed bean which makes SQL queries to Oracle database. This is
I have this java code which is a part of a managed bean which
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.
I have a property in my JSF managed bean: private List<Long> selectedDataSets; I initialize
I have an application scoped managed bean which main purpose is to serve the

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.