aspose file tools*
The moose likes JSF and the fly likes fill selectOneMenu from database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "fill selectOneMenu from database" Watch "fill selectOneMenu from database" New topic
Author

fill selectOneMenu from database

asmaa kellal
Greenhorn

Joined: May 22, 2011
Posts: 18
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 ("erreurriver int" +
"rouvable");
}




try

{
String URL = "jdbcracle: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
Krystian Gor
Ranch Hand

Joined: Jan 11, 2011
Posts: 30
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

Joined: May 22, 2011
Posts: 18
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

Joined: Jan 11, 2011
Posts: 30
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

Joined: May 22, 2011
Posts: 18

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

Joined: Jan 11, 2011
Posts: 30
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fill selectOneMenu from database