0

I have created 3 classes: The first for connection(Connectionx),the second I'am using it to fill UI as checkbox,selectonemenu(BeanTools) and the third is for my "operations"(NumberOfIssue). 1)The problem that I want to connect to the database just a once ,I have done the connection in the class Connectionx and I want to use the same connection on all my Beans.But I don't know how I have tried to add the proprity Connection to the managed Bean but I don't know if that is the good solution and if yes it dosen't work for me:( ,there are my classess :

**1/Class Connectionx**
public class Connectionx implements java.io.Serializable{
public Connection cx=null;
public Statement st;
public Connectionx() {}
public Connection Connx()
{ 
System.out.println("enregister le driver");
    try {
        Class.forName("com.mysql.jdbc.Driver");

    }
    catch( Exception ex )
            {
            System.err.println("Erreur lors du chargement du              driver"+ex.getMessage() );

            }
    try {
        /** Connection  */
         cx =  (Connection) DriverManager.getConnection( "jdbc:mysql://localhost/jiradb","root","root" );

        System.out.println("conexion reussite ");

            }
            catch( SQLException ex )
            {
            System. err. println( "Erreur lors de la connexion à la       baseeeeee" );

            }
  return cx;
}
public void deConnx(Connection cx) throws SQLException{

cx.close();
System. err. println( "deconnection");   
 }
 }

2/Class BeanTools

 public class BeanTools implements java.io.Serializable {

 /***************selectonemenu***********************/   
 private List<SelectItem> mesElements;
 private  String maValeur;
 public String[] favNumber1 ; //checkbox
 public PreparedStatement st;
 public Connectionx cx = new Connectionx();
 public Connection con = (Connection)cx.Connx();

 /**************checkbox***************/
  public BeanTools() {
  }

   public String getMaValeur() {
    return maValeur;
  }

   public void setMaValeur(String maValeur) {
    this.maValeur = maValeur;
   }

   public String[] getFavNumber1() {
    return favNumber1;
    }

   public void setFavNumber1(String[] favNumber1) {
    this.favNumber1 = favNumber1;
  }

   public String getFavNumber1InString() {

    return Arrays.toString(favNumber1);
}


    public String getFavNumber1onebyone()
    {  String Val =null;
    for(int i=0;i<favNumber1.length;i++)
    {
    if (favNumber1[i].equals("1"))
        System.out.println("priority checked");
    if (favNumber1[i].equals("2"))
        System.out.println("project checked");
    if (favNumber1[i].equals("3"))
   System.out.println("Month checked");

    }

    return Val;
    }


   public Iterable<String> remplireItem() throws ClassNotFoundException, SQLException
  {

   String sql;
   sql ="select issuestatus.pname from issuestatus;";
   System.out.println("sql");
   st=(PreparedStatement) con.prepareStatement(sql);
   ResultSet rs1 = st.executeQuery();System.out.println("execute");

   String val;
   ArrayList<String> list = new ArrayList<String>();
   while(rs1.next())
    {
      val= rs1.getString("pname");
      System.out.println(val);
      list.add(nbb);
      System.out.println("list"+list.toString());
    }
     return list;
    }


    public List<SelectItem> getMesElements() throws SQLException,ClassNotFoundException {

   if (mesElements == null) {
   mesElements = new ArrayList<SelectItem>();
   for (String val : remplireItem()) {
   mesElements.add(new SelectItem(val));
   }
   }
     return mesElements;
   }

3/class NumberOfIssue

public class NumberOfIssue implements java.io.Serializable{

public  String numberIssue;
private  String maValeur;
public Connectionx cx = new Connectionx();
public Connection con = (Connection)cx.Connx();
public PreparedStatement st;


public NumberOfIssue() {
}


   public String getMaValeur() {
    return maValeur;
}

public void setMaValeur(String maValeur) {
    this.maValeur = maValeur;
}

public String getNumberIssue() throws SQLException
{
//Connection con
BeanTools beTools=new BeanTools();
System.out.println("beTools.maValeur");
String sql;
sql ="select count(OS_CURRENTSTEP.entry_id) as nbissue     ,project.pname,priority.pname,issuetype.pname from OS_CURRENTSTEP,jiraissue,issuestatus,issuetype,project,resolution,customfield ,customfieldvalue,priority   where OS_CURRENTSTEP.entry_id = jiraissue.id  and jiraissue.priority=priority.id  and jiraissue.issuestatus=issuestatus.id and issuestatus.pname="+maValeur+" and jiraissue.issuetype=issuetype.id and jiraissue.project=project.id   and resolution.id=jiraissue.resolution  and resolution.pname='Fixed'  Group By project.pname,priority.pname,issuetype.pname;";

st= (PreparedStatement) con.prepareStatement(sql); 

ResultSet rs1 = st.executeQuery();

while(rs1.next())
{
 numberIssue=  rs1.getString(1);
 String strissue2=  rs1.getString(2);
 String strissue3=  rs1.getString(3);
 System.out.println("nb issue  "+numberIssue.toString()+"  ||project      "+strissue2.toString()+"  ||proiority  "+strissue3.toString());

}
return numberIssue;
}

}

4/Managed Bean

<managed-bean>
<managed-bean-name>Connectionx</managed-bean-name>
<managed-bean-class>KPIs.Connectionx</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

<managed-bean>
<managed-bean-name>BeanTools</managed-bean-name>
<managed-bean-class>KPIs.BeanTools</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>cx</property-name>
<property-class>java.sql.Connection</property-class>
<value>#{Connectionx.cx}</value>
</managed-property>
</managed-bean>

<managed-bean>
<managed-bean-name>NumberOfIssue</managed-bean-name>
<managed-bean-class>KPIs.NumberOfIssue</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>maValeur</property-name>
<property-class>java.lang.String</property-class>
<value>#{BeanTools.maValeur}</value>
</managed-property>
<managed-property>
<property-name>cx</property-name>
<property-class>java.sql.Connection</property-class>
<value>#{Connectionx.cx}</value>
</managed-property>
</managed-bean>

<navigation-rule>
<from-view-id>/welcomeJSF.jsp</from-view-id>
<navigation-case>
<from-outcome>submit</from-outcome>
<to-view-id>/Result.jsp</to-view-id> 
</navigation-case>
</navigation-rule>
</faces-config>

THE STACKTRACE

com.sun.faces.mgbean.ManagedBeanCreationException: Impossible de créer le bean géré    BeanTools.  Les problèmes suivants ont été détectés :
 - La propriété cx du bean géré BeanTools nexiste pas.
at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:265)
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 org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:71)
at org.apache.el.parser.AstValue.getValue(AstValue.java:147)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189)
at org.apache.jasper.el.JspValueExpression.getValue(JspValueExpression.java:106)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:193)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:181)
at javax.faces.component.UIOutput.getValue(UIOutput.java:169)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getCurrentSelectedValues(MenuRenderer.java:648)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderOptions(MenuRenderer.java:749)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:844)
at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:298)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
at com.sun.faces.application.view.JspViewHandlingStrategy.doRenderView(JspViewHandlingStrategy.java:431)
at com.sun.faces.application.view.JspViewHandlingStrategy.renderView(JspViewHandlingStrategy.java:233)
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:313)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:498)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:394)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
rym
  • 33
  • 1
  • 4

2 Answers2

2

Use a connection pool like commons-dbcp.

By the way this kind of code

"select count(OS_CURRENTSTEP.entry_id) as nbissue  [.. snip ...]
and issuestatus.pname="+maValeur+" and jiraissue.issuetype=issuetype.id
[... snip ...] and resolution.pname='Fixed'  Group By     
project.pname,priority.pname,issuetype.pname;";

is vulnerable to SQL injection, you should replace the maValeur variable with a parameter (see http://en.wikipedia.org/wiki/SQL_injection#Parameterized_statements)

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • I have tried sql ="select count(OS_CURRENTSTEP.entry_id) as nbissue ,project.pname,priority.pname,issuetype.pname from OS_CURRENTSTEP,jiraissue,issuestatus,issuetype,project,resolution,customfield ,customfieldvalue,priority where OS_CURRENTSTEP.entry_id = jiraissue.id and jiraissue.priority=priority.id and jiraissue.issuestatus=issuestatus.id and issuestatus.pname=? " st.setNString(1,maValeur); but this error appear(NULL) Caused by: java.lang.NullPointerException – rym Jun 09 '11 at 13:02
  • Have you called `prepareStatement` before calling `setNString`? By the way the Spring JDBC helper classes are very good if you want to use JDBC with less fuss. – artbristol Jun 09 '11 at 13:10
  • Thank you I don't know the Spring JDBC I will take a look :) – rym Jun 09 '11 at 16:12
0

Better way would be to use singleton class no need to register it to faces-context. just initialize it on context startup and use it from any bean

jmj
  • 237,923
  • 42
  • 401
  • 438
  • hi @Jigar Joshi ,how can I initialize the class on context startup!! – rym Jun 13 '11 at 10:34
  • Using `ContextListener` , As I answered here : http://stackoverflow.com/questions/6120831/java-ee-enterprise-application-perform-some-action-on-deploy-startup – jmj Jun 13 '11 at 10:38
  • I'am not using EJB, just simple javabean classes ,it will work for me?? (I sorry for the question but I am a newbie in J2EE world) – rym Jun 13 '11 at 10:56