一个idea连接MySQL数据库的web小例子(二)
文章目录前言一、创建实体类二、写查询所有链接1.创建对应的servlet三、Dao层增删改查方法四、创建booksall.jsp 显示数据库数据总结前言之前创建了users的实体类,现在对books表进行增删改查一、创建实体类不操作了,看上一个文章(这个表是上次帮同学搞框架遗留的,数据也是,将就着看)二、写查询所有链接这个是在main.jsp中写的查询链接1.创建对应的servlet这里用到的是d
文章目录
# 前言 之前创建了users的实体类,现在对books表进行增删改查 [上一篇跳转地址,点击传送](https://blog.csdn.net/qq_52010333/article/details/117853629)
一、创建实体类
不操作了,看上一个文章(这个表是上次帮同学搞框架遗留的,数据也是,将就着看)
二、写查询所有链接
这个是在main.jsp中写的查询链接
1.创建对应的servlet
这里用到的是doget,用dopost会报500-null的错的
先看下一步,代码后写(先写Dao中调用的方法)
package com.zy.servlet;
import com.zy.Dao.BooksDao;
import com.zy.bean.Books;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
/**
* 类名:BooksAll
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/7 23:54
* 版本:V1.0
*/
@WebServlet("/BooksAll")
public class BooksAll extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BooksDao booksDao = new BooksDao();
try {
List<Books> booksList = booksDao.selectAll();
//这个注释是后面看报错用的,看看有没有取到数据!
System.out.println("@@@@@"+booksList);
//把集合存入request,转发到页面可以取出来
req.setAttribute("booksList",booksList);
//转到jsp,固定方法getRequestDispatcher
req.getRequestDispatcher("booksall.jsp").forward(req,resp);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
三、Dao层增删改查方法
我很熟悉写的过程,就把所有方法写了,不熟悉可以按照(一)的图片顺序来一个一个写
package com.zy.Dao;
import com.zy.bean.Books;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 类名:BooksDao
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/8 0:05
* 版本:V1.0
*/
public class BooksDao {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//查询所有
public List<Books> selectAll() throws SQLException, ClassNotFoundException {
List<Books> list = new ArrayList<>();
connection=Conn.getcoon();
String sql = "select * from books";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet != null && resultSet.next()) {
Books books = new Books(resultSet.getInt("bid"),
resultSet.getString("bookname"),
resultSet.getString("author"),
resultSet.getString("images"),
resultSet.getString("press"),
resultSet.getInt("price"),
resultSet.getString("pubtime"));
list.add(books);
}
Conn.close(connection,preparedStatement,resultSet);
return list;
}
//通过id查询,修改得先查出来数据才能修改呀!这个是用来查询单条数据的
public Books selectByID(int bid) throws SQLException, ClassNotFoundException {
Books books = new Books();
connection=Conn.getcoon();
String sql = "select * from books where bid=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, bid);
resultSet = preparedStatement.executeQuery();
while (resultSet != null && resultSet.next()) {
books = new Books(resultSet.getInt("bid"),
resultSet.getString("bookname"),
resultSet.getString("author"),
resultSet.getString("images"),
resultSet.getString("press"),
resultSet.getInt("price"),
resultSet.getString("pubtime"));
}
Conn.close(connection,preparedStatement,resultSet);
return books;
}
//删除
public void delete(int bid) throws SQLException, ClassNotFoundException {
//·1获取连接
connection=Conn.getcoon();
String sql = "delete from books where bid=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, bid);
preparedStatement.executeUpdate();
Conn.close(connection,preparedStatement,resultSet);
}
// 添加
public void Add(Books books) throws SQLException, ClassNotFoundException {
//·1获取连接
connection=Conn.getcoon();
String sql = "insert into books values(null,?,?,?,?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,books.getBookname());
preparedStatement.setString(2, books.getAuthor());
preparedStatement.setString(3, books.getImages());
preparedStatement.setString(4, books.getPress());
preparedStatement.setInt(5, books.getPrice());
preparedStatement.setString(6, books.getpubtime());
preparedStatement.executeUpdate();
Conn.close(connection,preparedStatement,resultSet);
}
//修改
public void update(Books books) throws SQLException, ClassNotFoundException {
//·1获取连接
connection=Conn.getcoon();
String sql = "update books set bookname=?, author =?,images=?,press=?, price=?,pubtime=? where bid=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,books.getBookname());
preparedStatement.setString(2, books.getAuthor());
preparedStatement.setString(3, books.getImages());
preparedStatement.setString(4, books.getPress());
preparedStatement.setInt(5, books.getPrice());
preparedStatement.setString(6, books.getpubtime());
preparedStatement.setInt(7, books.getBid());
preparedStatement.executeUpdate();
Conn.close(connection,preparedStatement,resultSet);
}
}
四、创建booksall.jsp 显示数据库数据
通过for循环取出来
成功显示,然后开始操作 增删改
五、写添加链接
创建对应的添加jsp
写要添加的数据,id在数据库是自增的,所以这里用hidden隐藏域隐藏。
<center>
<form action="booksadd" method="post" >
<h1>商品添加</h1>
<table border="1" width="40%">
<tr>
<td>图书名称</td>
<td>
<input type="hidden" name="bid" value="">
<input type="text" name="bookname" value="">
</td>
</tr>
<tr>
<td>作者</td>
<td>
<input type="text" name="author" value="">
</td>
</tr>
<tr>
<td>图片</td>
<td>
<input type="text" name="images" value="">
</td>
</tr>
<tr>
<td>出版社</td>
<td>
<input type="text" name="press" value="">
</td>
</tr>
<tr>
<td>价格</td>
<td>
<input type="text" name="price" value="">
</td>
</tr>
<tr>
<td>出版时间</td>
<td>
<input type="datetime-local" name="pubtime" value="">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</center>
这个是form ,post 所以对应的servlet也应该用dopost方法
1.创建对应的servlet
把自己起的名字拿过来
上面说了重写的是dopost,别错了
package com.zy.servlet;
import com.zy.Dao.BooksDao;
import com.zy.bean.Books;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Queue;
/**
* 类名:BooksAdd
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/8 13:50
* 版本:V1.0
*/
@WebServlet("/booksadd")
public class BooksAdd extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//这句话不要忘了,不然添加的数据会乱码,可以尝试(滑稽)
req.setCharacterEncoding("utf-8");
String bookname=req.getParameter("bookname");
String author=req.getParameter("author");
String images=req.getParameter("images");
String press=req.getParameter("press");
//拿来的价格转成int类型
String pricestr=req.getParameter("price");
int price=Integer.parseInt(pricestr);
String pubtime=req.getParameter("pubtime");
//这里调用的不就是books定义的全参函数吗。。
Books books = new Books(0,bookname,author,images,press,price,pubtime);
BooksDao booksDao = new BooksDao();
try {
//直接调用add方法,然后抛出异常就行
booksDao.Add(books);
//添加完,调用查询所有,跳转页面
resp.sendRedirect("BooksAll");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2.测试添加效果
成功!
六、写删除链接
因为删除和修改都是对一条数据操作的,所以基本链接都是跟在每条数据后面的
1.创建对应的servlet
doget和dopost应该能区分什么时候用了吧,基本有form都用dopost
(仅代表个人)
前面直接给的代码,没写怎么调用。。
package com.zy.servlet;
import com.zy.Dao.BooksDao;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* 类名:BooksDel
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/8 15:12
* 版本:V1.0
*/
@WebServlet("/BooksDel")
public class BooksDel extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String bidStr=req.getParameter("bid");
int bid=Integer.parseInt(bidStr);
BooksDao booksDao = new BooksDao();
try {
booksDao.delete(bid);
System.out.println("删除完毕,日志显示!!!");
//添加完,调用查询所有,跳转页面
resp.sendRedirect("BooksAll");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2.测试删除效果
很好用,实在想不出来怎么证明那条数据存在过,拿日志顶着吧。
七、写修改链接
修改比较麻烦,我得先查出来,然后在原来的基础修改,开始操作。
这个是通过id查询数据,跳转到jsp页面,然后在进行修改操作
1.创建对应的servlet
package com.zy.servlet;
import com.zy.Dao.BooksDao;
import com.zy.bean.Books;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* 类名:BooksGoupdate
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/8 19:02
* 版本:V1.0
*/
@WebServlet("/BooksGoupdate")
public class BooksGoupdate extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String bidStr=req.getParameter("bid");
int bid=Integer.parseInt(bidStr);
BooksDao booksDao = new BooksDao();
try {
Books books = booksDao.selectByID(bid);
//《setAttribute的作用》 ,我知道,我解释不了,去搜吧书名号里面的
req.setAttribute("books",books);
req.getRequestDispatcher("updateBooks.jsp").forward(req,resp);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
2.创建修改页面updateBooks.jsp
先把添加的jsp代码拿过来,然后修改修改
这个和servlet 中的 ” req.setAttribute(“books”,books); “ 我觉得应该有关系;
我在sevlet注释中写了,查查看官方的,我的半吊子理解就自己够用。
然后修改链接,在创建servlet,调用修改方法
value=“ ” 里面放什么数据,jsp显示什么,我把通过id查到的数据放在里面,不就可以了吗
<center>
<%
Books books = (Books) request.getAttribute("books");
%>
<form action="booksupdate" method="post" >
<table border="1" width="40%">
<tr>
<td>图书名称</td>
<td>
<!--隐藏域,不给用户显示,但需要bid传参!-->
<input type="hidden" name="bid" value="<%out.print(books.getBid());%>">
<input type="text" name="bookname" value="<%out.print(books.getBookname());%>">
</td>
</tr>
<tr>
<td>作者</td>
<td>
<input type="text" name="author" value="<%out.print(books.getAuthor());%>">
</td>
</tr>
<tr>
<td>图片</td>
<td>
<input type="text" name="images" value="<%out.print(books.getImages());%>">
</td>
</tr>
<tr>
<td>出版社</td>
<td>
<input type="text" name="press" value="<%out.print(books.getPress());%>">
</td>
</tr>
<tr>
<td>价格</td>
<td>
<input type="text" name="price" value="<%out.print(books.getPrice());%>">
</td>
</tr>
<tr>
<td>出版时间</td>
<td>
<input type="datetime-local" name="pubtime" value="<%out.print(books.getpubtime());%>">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</center>
3.创建对应的修改servlet
package com.zy.servlet;
import com.zy.Dao.BooksDao;
import com.zy.bean.Books;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
/**
* 类名:booksupdate
* 读书破万卷,下笔如有神
* 代码反行之,算法记于心
* 作者:劫恋李
* 日期:2021/7/8 20:57
* 版本:V1.0
*/
@WebServlet("/booksupdate")
public class booksupdate extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
String bidStr=req.getParameter("bid");
int bid=Integer.parseInt(bidStr);
String bookname=req.getParameter("bookname");
String author=req.getParameter("author");
String images=req.getParameter("images");
String press=req.getParameter("press");
//拿来的价格转成int类型
String pricestr=req.getParameter("price");
int price=Integer.parseInt(pricestr);
String pubtime=req.getParameter("pubtime");
//这里面是修改完的数据
Books books = new Books(bid,bookname,author,images,press,price,pubtime);
BooksDao booksDao = new BooksDao();
try {
//调用修改方法,修改数据
booksDao.update(books);
resp.sendRedirect("BooksAll");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
4.测试修改效果
看最后一条数据,价格88 我修改成100
查出来这一步没问题,可以出来,然后修改价格
这里没报错,修改成功!
总结
下一篇,上传图片功能
有好多地方没讲解到,重要的我好像标记注释,网上查查详细讲解,我这半吊子技术就不在解释了,执行流程图上一篇文章也画了,就不在画了(可能会在后面把jsp中的java代码优化一下,还有实现图片上传功能) 好了,最后把源码放在这里。
参考答案
更多推荐
所有评论(0)