项目结构
Book.java
package beans;
public class Book {
private Integer id;
private String name;
private double price;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
BookDao.java
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.resource.cci.ResultSet;
import beans.Book;
public class BookDao {
@SuppressWarnings({ "rawtypes", "unchecked" })
public ArrayList queryBookById(String id) {
Connection conn=null;
ArrayList books=new ArrayList();
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
@SuppressWarnings("unused")
String url="jdbc:mysql://localhost:3306/books?useUnicode=true&charaterEncoding=UTF-8";
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/books","root","123456");
String sql="select * from book where id=?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, id);
ResultSet rs=(ResultSet) ps.executeQuery();
while(rs.next()){
Book book=new Book();
book.setId(rs.getInt(1));
book.setName(rs.getString(2));
book.setPrice(rs.getDouble(3));
books.add(book);
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try{
if(conn!=null){
conn.close();
conn=null;
}
}catch(Exception e){}
}
return books;
}
}
BookServlet.java
package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.BookDao;
@SuppressWarnings("serial")
public class BookServlet extends HttpServlet {
@SuppressWarnings("rawtypes")
public void servlet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String id =request.getParameter("id");
BookDao dao=new BookDao();
ArrayList books=dao.queryBookById(id);
request.setAttribute("books", books);
ServletContext application =this.getServletContext();
RequestDispatcher rd=application.getRequestDispatcher("/showbook.jsp");
rd.forward(request, response);
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>查询书籍系统</title>
</head>
<body>
<form action="servlet/BookServlet" method="post">
请输入id: <input type="text" name="id">
<input type="submit" value="查询">
</form>
</body>
</html>
showBook.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="dao.BookDao" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="beans.Book" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>书籍信息</title>
</head>
<body>
<%
ArrayList books=(ArrayList)request.getAttribute("books");
%>
<table border="2">
<tr>
<td>id</td>
<td>name</td>
<td>price</td>
</tr>
<%
for (int i = 0;i < books.size();i++)
{
Book book = (Book)books.get(i);
%>
<tr>
<td><%=book.getId()%></td>
<td><%=book.getName()%></td>
<td><%=book.getPrice()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
books.sql
/*
Navicat Premium Data Transfer
Source Server : 11
Source Server Type : MySQL
Source Server Version : 80030
Source Host : localhost:3306
Source Schema : books
Target Server Type : MySQL
Target Server Version : 80030
File Encoding : 65001
Date: 05/12/2023 21:02:10
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`id` int NOT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`price` double NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (1, '西游记', 100);
SET FOREIGN_KEY_CHECKS = 1;
mysql.jar包下载
MySQLjar包官方版下载丨最新版下载丨绿色版下载丨APP下载-123云盘
--更新于12-6
本文仅仅用了测试,会出现报错!!!
如报错HTTP method POST is not supported by this URL
或者The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files等
经过一晚上测试,发现是由于自己book.java文件使用私有属性,以及在getid方法中出现于bookdao.java的类型并不匹配,加上EE5的library的导入,最后完成调试!
下面是改后的结构
数据库
数据导入用的是MySQL的workbench,可复制下来,直接在workbench中运行即可
-- MySQL dump 10.13 Distrib 8.0.32, for Win64 (x86_64)
--
-- Host: localhost Database: book
-- ------------------------------------------------------
-- Server version 8.0.32
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!50503 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `book`
--
DROP TABLE IF EXISTS `book`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `book` (
`id` varchar(45) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`price` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `book`
--
LOCK TABLES `book` WRITE;
/*!40000 ALTER TABLE `book` DISABLE KEYS */;
INSERT INTO `book` VALUES ('1','水浒传',88.8);
/*!40000 ALTER TABLE `book` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2023-12-06 1:54:04
数据库数据完成后,进行book.java的修改
package beans;
public class Book {
String id;
String name;
float price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
重启tomcat6.0
最后成功
在今晚test时候发现的问题
注意变量的类型,也可以继续使用id int ,price double型
重新生成方法
select all 选择public,生成
代码
package beans;
public class Book {
int id;
String name;
double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
修改bookdao.java
重启后访问报错
HTTP Status 500 - javax.servlet.ServletException: java.lang.NoSuchMethodError: beans.Book.getId()Ljava/lang/String;
(有待解决)
没有该方法调用