Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

fill selectOneMenu from database

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Why my code doesn't work ? After code execution on JSP page

an error message appears:

An Error Occurred:
Cannot convert [javax.faces.model.SelectItem@61d27a] of type class java.util.ArrayList to class javax.faces.model.SelectItem

Page: test.jsp

<html>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>

<f:view>
<head>

<title>

</title>
</head>
<body>
<h:form >

<h:selectOneMenu value=" # {Monbean.maValeur}" >
<f:selectItem value="#{Monbean .mesElements}"/>
</h:selectOneMenu>
</h:form>
</body>
</f:view>
</html>

Bean: Monbean.java

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;

import javax.faces.model.SelectItem;

import java.sql.*;

import java.sql.DriverManager;

import java.util.logging.Level;

import java.util.logging.Logger;



public class Monbean {

private String maValeur;
Connection con ;
private List<SelectItem> mesElements;


public String getmaValeur() {
return maValeur;
}

public void setmaValeur(String maValeur) {
this.maValeur = maValeur;
}
private Iterable<String> getMaliste() throws SQLException {
ArrayList unelist = new ArrayList();




try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
System.out.println ("driver etablie");
}
catch(Exception e)
{
System.out.println ("erreur:Driver int" +
"rouvable");
}




try

{
String URL = "jdbc:oracle:thin:@localhost:1521:gmao";

String USER = "pfe";
String PASSWD = "gmao";

con =DriverManager.getConnection(URL,USER, PASSWD);

System.out.println ("connexion base pfe etablie");
}
catch(Exception e)
{

System.out.println ("erreur: base introuvable");

}


Statement st = con.createStatement();

ResultSet resultats = null;

try {

resultats = st.executeQuery("select * from auteur");
}
catch (SQLException ex) {

Logger.getLogger(Monbean.class.getName()).log(Level.SEVERE, null, ex);
}

try {
while(resultats.next()){


String num=resultats.getString("nomaut").toString();

unelist.add(num);

}
}
//session.close();

catch (Exception e) {
e.printStackTrace();
}
return (List) unelist;
}


public List<SelectItem> getMesElements() throws SQLException {

if (mesElements == null) {

mesElements = new ArrayList<SelectItem>();

for (String val : getMaliste()) {

mesElements.add(new SelectItem(val));
}

}
return mesElements;

/** Creates a new instance of Monbean */


}

public Monbean() throws SQLException{


}
}

------------------------------

Could you please help me to correct my code

thank you
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure but I think there's something wrong between getMaliste() and getMesElements().
Try to use this method:

and change
to
 
asmaa kellal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
thanks for the answer
I still want to ask your help if possible when I select the menu item how I can insert it into another table son

I explain - idauteur (Menu) is a primary key in table 'author' (with which I completed) and a foreign key in table 'book'- how I can insert the selected value from the menu on the table' book '

Any advice would be greatly appreciated
thank you
 
Krystian Gor
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have any working code to add 'author' and 'book' to database? Show me if you have. It will be easier to say if I can help you.
Also show me your 'author' class and 'book' class.
 
asmaa kellal
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here is the code to add 'author'






Here is the code to add 'book' but missing the insertion of the selected item in the menu 'idauteur'

here are the two class

the class diagram are not many things to view
cardinalities between class UML '

author '1'---------------book '1...*'



what I need is how to insert an item selected by menu in the table book
 
Krystian Gor
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using entity classes and accessing database with EntityManager would be more elegant and safer. Managing relations would be easier.
Strange usage of managed beans. I'm not familiar with what you're doing here.
Try reading some tutorials like this http://balusc.blogspot.com/2006/06/using-datatables.html
 
Time is mother nature's way of keeping everything from happening at once. And this is a tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic