如何向用户显示电影列表,让他选择一部电影并将其保存在一个表中

kpbwa7wx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(455)

这个问题在这里已经有答案了

什么是堆栈跟踪,如何使用它调试应用程序错误(7个答案)
jsf控制器、服务和dao(2个答案)
5个月前关门了。
我有一个小的网络应用程序,用户应该预订电影。我创建了jsf页面,并通过这种方式显示数据库中可用的电影列表:

  1. <ui:composition template="/template.xhtml">
  2. <ui:define name="title">
  3. <h:outputText value="#{bundleMovie.ListMovieTitle}"></h:outputText>
  4. </ui:define>
  5. <ui:define name="body">
  6. <h:form styleClass="jsfcrud_list_form">
  7. <h:panelGroup id="messagePanel" layout="block">
  8. <h:messages errorStyle="color: red" infoStyle="color: green" layout="table"/>
  9. </h:panelGroup>
  10. <h:outputText escape="false" value="#{bundleMovie.ListMovieEmpty}" rendered="#{movieController.items.rowCount == 0}"/>
  11. <h:panelGroup rendered="#{movieController.items.rowCount > 0}">
  12. <h:outputText value="#{movieController.pagination.pageFirstItem + 1}..#{movieController.pagination.pageLastItem + 1}/#{movieController.pagination.itemsCount}"/>&nbsp;
  13. <h:commandLink action="#{movieController.previous}" value="#{bundleMovie.Previous} #{movieController.pagination.pageSize}" rendered="#{movieController.pagination.hasPreviousPage}"/>&nbsp;
  14. <h:commandLink action="#{movieController.next}" value="#{bundleMovie.Next} #{movieController.pagination.pageSize}" rendered="#{movieController.pagination.hasNextPage}"/>&nbsp;
  15. <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">
  16. <h:column>
  17. <f:facet name="header">
  18. <h:outputText value="#{bundleMovie.ListMovieTitle_movieId}"/>
  19. </f:facet>
  20. <h:outputText value="#{item.movieId}"/>
  21. </h:column>
  22. <h:column>
  23. <f:facet name="header">
  24. <h:outputText value="#{bundleMovie.ListMovieTitle_movieName}"/>
  25. </f:facet>
  26. <h:outputText value="#{item.movieName}"/>
  27. </h:column>
  28. <h:column>
  29. <f:facet name="header">
  30. <h:outputText value="#{bundleMovie.ListMovieTitle_movieGenre}"/>
  31. </f:facet>
  32. <h:outputText value="#{item.movieGenre}"/>
  33. </h:column>
  34. <h:column>
  35. <f:facet name="header">
  36. <h:outputText value="#{bundleMovie.ListMovieTitle_movieRating}"/>
  37. </f:facet>
  38. <h:outputText value="#{item.movieRating}"/>
  39. </h:column>
  40. <h:column>
  41. <f:facet name="header">
  42. <h:outputText value="#{bundleMovie.ListMovieTitle_movieDate}"/>
  43. </f:facet>
  44. <h:outputText value="#{item.movieDate}"/>
  45. </h:column>
  46. <h:column>
  47. <f:facet name="header">
  48. <h:outputText value="&nbsp;"/>
  49. </f:facet>
  50. <h:commandLink action="#{movieController.bookAMovie(movie)}" value="Book" />
  51. </h:column>
  52. </h:dataTable>
  53. </h:panelGroup>
  54. <br />
  55. <h:link outcome="/faces/adminIndex.jsp" value="Return to home page"/>
  56. <br />
  57. <br />
  58. </h:form>
  59. </ui:define>
  60. </ui:composition>

如你所见,方法 <h:commandLink action="#{movieController.bookAMovie(movie)}" value="Book" /> 应将用户选择保存在新表中,该表为 bookedmovie 同时将表中的数据显示给用户 movie .
方法是:

  1. public int bookAMovie(Movie movie) throws ClassNotFoundException {
  2. String INSERT_USERS_SQL = "INSERT INTO bookedmovie"
  3. + " (bookedmoviename, bookedmoviegenre, bookedmovierating) VALUES "
  4. + " (?, ?, ?);";
  5. int result = 0;
  6. Class.forName("com.mysql.jdbc.Driver");
  7. try (Connection connection = DriverManager
  8. .getConnection("jdbc:mysql://localhost:3306/cs230projekat?useSSL=false", "root", "");
  9. // Step 2:Create a statement using connection object
  10. PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
  11. preparedStatement.setString(1, movie.getMovieName());
  12. preparedStatement.setString(2, movie.getMovieGenre());
  13. preparedStatement.setDouble(3, movie.getMovieRating());
  14. System.out.println(preparedStatement);
  15. // Step 3: Execute the query or update query
  16. result = preparedStatement.executeUpdate();
  17. } catch (SQLException e) {
  18. // process sql exception
  19. printSQLException(e);
  20. }
  21. return result;
  22. }

我不知道怎么解决这个问题。不一定要这样,如果有人能帮上什么忙,那就适合我了。只向用户显示数据库中的电影,用户可以选择一部电影,并且他的选择存储在一个新表中。

flmtquvp

flmtquvp1#

关于java代码,下面的代码运行良好。我能运行它。
只是要确保,您已经在您的项目中添加了适当的mysql库 UserName/Password 应该是正确的。现在您提供了空密码。
因为你没有提供任何错误堆栈。我不能直接对此发表评论。

  1. class Sample3 {
  2. public static void main(String[] args) throws ClassNotFoundException {
  3. bookAMovie(Movie.builder().movieGenre("Comic").movieRating(8).movieName("Movie1").build());
  4. }
  5. public static int bookAMovie(Movie movie) throws ClassNotFoundException {
  6. String INSERT_USERS_SQL = "INSERT INTO bookedmovie"
  7. + " (bookedmoviename, bookedmoviegenre, bookedmovierating) VALUES "
  8. + " (?, ?, ?);";
  9. int result = 0;
  10. Class.forName("com.mysql.jdbc.Driver");
  11. try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sample?useSSL=false", "root", "PASSWORD");
  12. // Step 2:Create a statement using connection object
  13. PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
  14. preparedStatement.setString(1, movie.getMovieName());
  15. preparedStatement.setString(2, movie.getMovieGenre());
  16. preparedStatement.setDouble(3, movie.getMovieRating());
  17. System.out.println(preparedStatement);
  18. // Step 3: Execute the query or update query
  19. result = preparedStatement.executeUpdate();
  20. Statement statement=connection.createStatement();
  21. ResultSet resultSet=statement.executeQuery("SELECT * FROM sample.bookedmovie");
  22. List<Map<String, String>> mapList = getList(resultSet);
  23. System.out.println(mapList);
  24. } catch (SQLException e) {
  25. // process sql exception
  26. e.printStackTrace();
  27. }
  28. return result;
  29. }
  30. public static List<Map<String, String>> getList(ResultSet resultSet) throws SQLException {
  31. List<Map<String, String>> mapList = new ArrayList<>();
  32. ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
  33. int columnNumber = resultSetMetaData.getColumnCount();
  34. while (resultSet.next()) {
  35. Map<String, String> map = new HashMap<>();
  36. int count = 1;
  37. while (count <= columnNumber) {
  38. String columnName = resultSetMetaData.getColumnName(count);
  39. map.put(columnName, resultSet.getString(columnName));
  40. count++;
  41. }
  42. mapList.add(map);
  43. }
  44. return mapList;
  45. }
  46. }
  47. @Data
  48. @Builder
  49. class Movie {
  50. public String movieName;
  51. public String movieGenre;
  52. public double movieRating;
  53. }

这里我附上了java run的成功截图,mysql中也插入了相同的记录

mysql数据库

展开查看全部

相关问题