I need to create a program that accesses a database and passes the values to a dropdown in JSF. I am trying to figure out how that can be done. I Got it working for the 1st dropdown, but the second dropdown is tricky, as it needs a input parameter to work. Here is my JSF code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<head>
<title>Dropdown List</title>
</head>
<body>
<select id="mySelect" onchange="myFunction()">
<h:outputText value="#{helloWorld.getResultSet1()}" escape="false" />
<f:attribute name="action" value="1" />
</select>
<br />
<select id="mySelect1">
</select>
<script>
var x1 = document.getElementById("mySelect1");
x1.style.display = 'none';
function myFunction() {
x1.style.display = 'block';
x1.innerHtml = '<h:outputText value="#{helloWorld.getResultSet2()}" escape="false" />';
}
document.getElementById('myForm:hidden2').value = new_value;
</script>
</body>
</html>
And here is my java code:
public String id = "1";
public void print(String name) {
System.out.println(name);
}
public String getResultSet2(String name) {
String toReturn = "";
try {
rs = st.executeQuery("SELECT * FROM CS_EXAMS where name ="+ name);
System.out.println(name);
while (rs.next()) {
toReturn = toReturn + "<option>" + rs.getString("exam_name") + "</option>";
}
} catch (SQLException e) {
e.printStackTrace();
}
return toReturn;
}
public String getResultSet1() {
String toReturn = "";
try {
rs = st.executeQuery("SELECT * FROM CS_EXAMS");
System.out.println("name");
while (rs.next()) {
toReturn = toReturn + "<option>" + rs.getString("exam_name") + "</option>";
}
} catch (SQLException e) {
e.printStackTrace();
}
return toReturn;
}