如何解决将下拉列表中的值插入数据库表
我创建了一个下拉菜单,其中包含来自数据库的数据。
这是代码:
<h:form>
Category
<h:selectOneMenu value="#{movie.movieName}">
<f:selectItems id="movieName" value="#{movie.get_movie_list()}"></f:selectItems>
</h:selectOneMenu><br></br>
<h:commandButton type="Submit" label="Submit" action=""></h:commandButton>
</h:form>
这是用于从数据库填充下拉菜单的代码:
private String movieName;
private List<String> movie_list = new ArrayList<>();
public List<String> get_movie_list() {
try {
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/cs230projekat","root","");
PreparedStatement ps = null;
ps = connection.prepareStatement("select * from movie");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
movie_list.add(rs.getString("movieName"));
}
} catch (Exception e) {
System.out.println(e);
}
return movie_list;
}
这是我要插入表格的方法:
public int addMovie(Movie movie) throws ClassNotFoundException {
String INSERT_USERS_SQL = "INSERT INTO bookedmovi"
+ " (moviename) VALUES "
+ " (?);";
int result = 0;
Class.forName("com.mysql.jdbc.Driver");
try (Connection connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/cs230projekat?useSSL=false","");
// Step 2:Create a statement using connection object
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
preparedStatement.setString(1,movie.getMovieName());
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;
}
到那时,一切都将正常运行。我的主要问题是,我希望用户从下拉菜单中选择一个选项,并将其选项存储在数据库的新表中。有人可以帮我实现这一目标吗?因此,用户从下拉菜单中选择一个选项,单击确定后,他的选择将保存在新表和数据库中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。