Java WEB程序题 购物车问题+JDBC连接数据库

购物车问题

<%@page import="com.book.entity.Book"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



books list


分别为书名单价和数量
<% request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); %> <% //获取session Map books=(HashMap)session.getAttribute("books"); if(books==null){ books=new HashMap(); session.setAttribute("books",books); } if(books!=null){ //获取要加入购物车的图书名 String bookname=request.getParameter("bookname"); String prize = request.getParameter("prize"); String count = request.getParameter("count"); boolean isEmpty = (bookname==null)&&(prize == null)&&(count ==null); if(!isEmpty){ int bookCount=Integer.parseInt(count); if(bookCount==0){ out.println(""); }else{ books.put(bookname, new Book(Double.parseDouble(prize), bookCount)); out.println(""); } } } %>

JDBC连接数据库

package com.gen.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gen.entity.Student;

public class Query extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
	private static String DB_URL = "jdbc:mysql://localhost:3306/student?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
	private static String UNAME = "root";
	private static String UPASS = "123456";

	private String sql_query = "SELECT * FROM t_student WHERE STUNO LIKE ? or STUNAME LIKE ? OR STUSEX LIKE ?";

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String param = request.getParameter("param");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			Class.forName(JDBC_DRIVER);
			connection = DriverManager.getConnection(DB_URL, UNAME, UPASS);
			preparedStatement = connection.prepareStatement(sql_query);
			for (int i = 0; i < 3; i++) {
				preparedStatement.setString(i + 1, '%'+param +'%');
			}

			resultSet = preparedStatement.executeQuery();
			List<Student> students = new LinkedList<>();
			while (resultSet.next()) {
				students.add(new Student(resultSet.getString(1), resultSet.getString(2), resultSet.getString(3)));
			}
			System.out.println(students);
			request.setAttribute("students", students);
			request.getRequestDispatcher("result.jsp").forward(request, response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				resultSet.close();
				preparedStatement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

result.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
<style type="text/css">
table {
	border-collapse: collapse;
	width: 100%;
}

tr, td, th {
	border: 1px solid orange;
}
style>
head>
<body>
	<form action="<%=request.getContextPath()%>/Query" method="post">
		<p>
			<span>keywords: span><input type="text" name="param"
				required="required">
		p>
		<input type="submit" value="query">
	form>
	<hr>
	<table>
		<tr>
			<th>Noth>
			<th>stdentNoth>
			<th>studentNameth>
			<th>studentSexth>
		tr>
		<c:forEach var='student' items="${requestScope.students}"
			varStatus="status">
			<tr>
				<td>${status.count }td>
				<td>${student.stuNo }td>
				<td>${student.stuName }td>
				<td>${student.stuSex }td>
			tr>
		c:forEach>
	table>
body>
html>

你可能感兴趣的:(Java后端,数据库,jdbc,servlet)