Java Reference
In-Depth Information
59
60
public
String[] getTitles() {
61
return
titles;
62 }
63
64
public
String getChoice() {
65
return
choice;
66 }
67
68
public void
setChoice(String choice) {
69
this
.choice = choice;
70 }
71
72
public
ResultSet getStudents()
throws
SQLException {
73
if
(choice ==
null
) {
74
if
(titles.length ==
0
)
75
return null
;
76
else
77 studentStatement.setString(
1
, titles[
0
]);
78 }
79
else
{
80 studentStatement.setString(
1
, choice);
// Set course title
81 }
82
83
get students
set a default course
set a course
// Get students for the specified course
84
return
studentStatement.executeQuery();
return students
85 }
86 }
We use the same MySQL database
javabook
created in Chapter 32, “Java Database Pro-
gramming.” The scope for this managed bean is
application
. The bean is created when the
project is launched from the server. The
initializeJdbc
method loads the JDBC driver for
MySQL (lines 23-24), connects to the MySQL database (lines 27-28), creates statement for
obtaining course titles (lines 31-32), and creates a statement for obtaining the student infor-
mation for the specified course (lines 45-53). Lines 31-42 execute the statement for obtaining
course titles and store them in array
titles
.
The
getStudents()
method returns a
ResultSet
that consists of all students enrolled in
the specified course (lines 72-85). The choice for the title is set in the statement to obtain the
student for the specified title (line 80). If choice is
null
, the first title in the titles array is set
in the statement (line 77). If no titles in the course,
getStudents()
returns
null
(line 75).
TIP
In order to use the MySQL database from this project, you have to add the MySQL JDBC
driver from the Libraries node in the Project pane in NetBeans.
add MySQL in the Libraries
node
L
ISTING
33.14
DisplayStudent.xhtml
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml"
5 xmlns:h="http://xmlns.jcp.org/jsf/html"
6 xmlns:f="http://xmlns.jcp.org/jsf/core">
7 <h:head>
8 <title>Display Student</title>
9 <h:outputStylesheet name="tablestyle.css" />
10 </h:head>
11 <h:body>
style sheet
Search WWH ::
Custom Search