【发布时间】:2020-10-23 09:11:50
【问题描述】:
所以,我使用 JSP 来显示来自数据库的互联网页面数据。我有两个表(“产品目录”和“发票”)。我想在 checkbox 和 insert into 另一个表(Invoices)的帮助下从表(产品目录)中选择一个产品。
productCatalog.jsp
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Product Catalog</title>
</head>
<body>
<%
String sql;
String output;
String table;
ResultSet rs;
Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");
Statement stmt = connection.createStatement();
rs = stmt.executeQuery("SELECT * FROM PRODUCT_CATALOG");
output = "<tr>" + "<th>SELECT PRODUCT</th>" + "<th>PRODUCT ID</th>" + "<th>PRODUCT NAME</th>" + "<th>PRICE</th></tr>";
while (rs.next()) {
output += "<tr><td><input type='checkbox'></td>" + "<td>" + rs.getString(1) + "</td>" + "<td>" + rs.getString(2) + "</td>" + "<td>" + rs.getString(3) + "</td></tr>";
}
table = "<html>"
+ "<head>"
+ "</head>"
+ "<body>"
+ "<div>"
+ "<h1>Product Catalog</h1>"
+ "<form action='invoiceTable.jsp' method='POST'>"
+ "<table>"
+ output
+ "</table>"
+ "<button onclick='history.go(-1)' type='button'>Back</button>"
+ "<button type='submit'>Create invoice</button>"
+ "</form>"
+ "</div>"
+ "</body>"
+ "</html>";
out.println(table);
rs.close();
%>
</body>
</html>
invoiceTable.jsp --- 我写了“???”,因为我选择的表行中必须有数据/值。而且我不知道我必须使用哪个函数或哪个参数。
String Articles = request.getParameter("???");
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Invoices</title>
</head>
<body>
<%
String sql;
String output;
String table;
ResultSet rs;
String Articles = request.getParameter("???");
String Total_price = request.getParameter("???");
Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection connection = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/", "SA", "");
Statement stmt = connection.createStatement();
sql = "INSERT INTO INVOICES (Articles, Total_price)"
+ "VALUES( '" + Articles + "', '" + Total_price + "');";
stmt.executeUpdate(sql);
rs = stmt.executeQuery("SELECT * FROM INVOICES");
output = "<tr><th>INVOICE NR.</th>" + "<th>ARTICLES</th>" + "<th>TOTAL PRICE</th></tr>";
while (rs.next()) {
output += "<tr><td>" + rs.getString(1) + "</td>" + "<td>" + rs.getString(2) + "</td>" + "<td>" + rs.getString(3) + "</td></tr>";
}
table = "<html>"
+ "<head>"
+ "</head>"
+ "<body>"
+ "<div>"
+ "<h1>Invoices</h1>"
+ "<table>"
+ output
+ "</table>"
+ "</form>"
+ "</div>"
+ "</body>"
+ "</html>";
out.println(table);
rs.close();
%>
</body>
</html>
【问题讨论】: