检索数据并插入数据库

时间:2016-12-28 18:55:22

标签: java html sql jsp

在下图中,当用户按下提交按钮时,应用程序将选择的数字插入到数据库中。但它不适用于活动数量。按下提交按钮后,activityid返回null并且buy返回选择的数字。如何在数据库enter image description here

上插入正确的activityid

music.jsp

  <table border="1" width="30%" height="30%">
            <th><font color='#D18603'>ActivityID</font>
            <th><font color='#D18603'>Type</font></th>
            <th><font color='#D18603'>Description</font></th>
            <th><font color='#D18603'>City</font></th>
            <th><font color='#D18603'>Location</font></th>
            <th><font color='#D18603'>Date</font></th>
            <th><font color='#D18603'>Price</font></th>
            <th><font color='#D18603'>Buy</font>

                        <%
                            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

                            Statement st = con.createStatement();
                            ResultSet rs;
                            rs = st.executeQuery("select * from activities where type='müzik'");
                            while (rs.next()) {

                                out.println("<tr>");
                                out.println("<td>" + rs.getString("id")+"</td>");
                                out.println("<td>" + rs.getString("type")+ "</td>");
                                out.println("<td>" + rs.getString("description")+ "</td>");
                                out.println("<td>" + rs.getString("city") + "</td>");
                                out.println("<td>" + rs.getString("location")+ "</td>");
                                out.println("<td>" + rs.getString("date") + "</td>");
                                out.println("<td>" + rs.getString("price") + "</td>");
                                out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>");
                                out.println("</tr>");

                            }
                            st.close();

                        %>

Reservation.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%

String username = (String) request.getSession().getAttribute("username");
Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

String sorgu = "INSERT INTO reservation(id,username,buy) VALUES ('" + request.getParameter("id") + "', '" + username + "','" + request.getParameter("buy") + "') ";

java.sql.Statement st = con.createStatement();

int rowNum = st.executeUpdate(sorgu);
response.sendRedirect("paypal.html");
st.close();
%>

1 个答案:

答案 0 :(得分:1)

您需要获取每行中所有数据的标记,并向每列添加(隐藏)表单标记,以便所有数据都包含在表单提交中。为了清楚起见,除了如何组织表单标签以及如何包含&#34; id&#34;之外,我已经删除了所有内容。在表单提交

                        rs = st.executeQuery("select * from activities where type='müzik'");
                        while (rs.next()) {

                            out.println("<tr>");
                            out.println("<form action='reservations.jsp'>");
                            out.println("<td>" + rs.getString("id") + "<input type='hidden' name='id' value='" + td.getString("id")+"'></td>");
                            out.println("<td><input type='submit' value='Submit'></form></b></td>");
                            out.println("</td></tr>");

                        }
                        st.close();