I have small web application where the user should book movies. I created JSF pages and this way I display a list of movies available in the database:
<ui:composition template="/template.xhtml">
<ui:define name="title">
<h:outputText value="#{bundleMovie.ListMovieTitle}"></h:outputText>
</ui:define>
<ui:define name="body">
<h:form styleClass="jsfcrud_list_form">
<h:panelGroup id="messagePanel" layout="block">
<h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
</h:panelGroup>
<h:outputText escape="false" value="#{bundleMovie.ListMovieEmpty}" rendered="#{movieController.items.rowCount == 0}"/>
<h:panelGroup rendered="#{movieController.items.rowCount > 0}">
<h:outputText value="#{movieController.pagination.pageFirstItem + 1}..#{movieController.pagination.pageLastItem + 1}/#{movieController.pagination.itemsCount}"/>
<h:commandLink action="#{movieController.previous}" value="#{bundleMovie.Previous} #{movieController.pagination.pageSize}" rendered="#{movieController.pagination.hasPreviousPage}"/>
<h:commandLink action="#{movieController.next}" value="#{bundleMovie.Next} #{movieController.pagination.pageSize}" rendered="#{movieController.pagination.hasNextPage}"/>
<h:dataTable value="#{movieController.items}" var="item" border="0" cellpadding="2" cellspacing="0" rowClasses="jsfcrud_odd_row,jsfcrud_even_row" rules="all" style="border:solid 1px">
<h:column>
<f:facet name="header">
<h:outputText value="#{bundleMovie.ListMovieTitle_movieId}"/>
</f:facet>
<h:outputText value="#{item.movieId}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundleMovie.ListMovieTitle_movieName}"/>
</f:facet>
<h:outputText value="#{item.movieName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundleMovie.ListMovieTitle_movieGenre}"/>
</f:facet>
<h:outputText value="#{item.movieGenre}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundleMovie.ListMovieTitle_movieRating}"/>
</f:facet>
<h:outputText value="#{item.movieRating}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{bundleMovie.ListMovieTitle_movieDate}"/>
</f:facet>
<h:outputText value="#{item.movieDate}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value=" "/>
</f:facet>
<h:commandLink action="#{movieController.bookAMovie(movie)}" value="Book" />
</h:column>
</h:dataTable>
</h:panelGroup>
<br />
<h:link outcome="/faces/adminIndex.jsp" value="Return to home page"/>
<br />
<br />
</h:form>
</ui:define>
</ui:composition>
As you can see, method <h:commandLink action="#{movieController.bookAMovie(movie)}" value="Book" />
should save the user selection in the new table, that table is bookedmovie
while the data from the table is displayed to the user movie
.
Method is:
public int bookAMovie(Movie movie) throws ClassNotFoundException {
String INSERT_USERS_SQL = "INSERT INTO bookedmovie"
+ " (bookedmoviename, bookedmoviegenre, bookedmovierating) VALUES "
+ " (?, ?, ?);";
int result = 0;
Class.forName("com.mysql.jdbc.Driver");
try (Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/cs230projekat?useSSL=false", "root", "");
// Step 2:Create a statement using connection object
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
preparedStatement.setString(1, movie.getMovieName());
preparedStatement.setString(2, movie.getMovieGenre());
preparedStatement.setDouble(3, movie.getMovieRating());
System.out.println(preparedStatement);
// Step 3: Execute the query or update query
result = preparedStatement.executeUpdate();
} catch (SQLException e) {
// process sql exception
printSQLException(e);
}
return result;
}
I dont know how to fix this. It doesn't have to be this way, if anyone can help in any way, it suits me. Only that the user is shown movies from the database, that the user can select one movie and that his selection is stored in a new table.