0

I created a drop down menu that contains data from a database.

This is code:

<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>

This is code to populate dropdown menu from database:

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;
}

This is my method to insert in table:

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", "root", "");
            // 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;
}

Until then, everything works perfectly. My main problem is, I want the user to select one option from the drop down menu and have his option stored in a new table in the database. Can someone help me how to achive that? So, The user selects one option from the drop-down menu and after clicking confirms his selection is saved in a new table and database.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
roses
  • 1
  • 1
  • So what's the question here? Do you just not know how to write the query? If you can do a select, you're 90% of the way to doing an insert. – MarsAtomic Aug 23 '20 at 00:23
  • I updated my question with my insert method. I dont know how to make so button do my method – roses Aug 23 '20 at 00:33

0 Answers0