JavaEE课程设计:数据库的增删改查之添加页(图书)

2022-07-27,,,,

JDBC(Java Data Base Connectivity)。

创建图书(book)类

public class Book{
	private int bid;
	private String name;
	private double price;
	private int bookCount;
	private String author;
	
	public int getBid() {
		return bid;
	}
	public void setBid(int bid) {
		this.bid = bid;
	}
	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;
	}
	public int getBookCount() {
		return bookCount;
	}
	public void setBookCount(int bookCount) {
		this.bookCount = bookCount;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
}

创建AddBook.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加书籍</title>
</head>
<body>
<form action="#" method="post" onsubmit="return check(this);">
	<table align="center" width="450">
		<tr>
			<td align="center" colspan="2">
			<h2>添加图书信息</h2>
			</td>
		</tr>
		<tr>
			<td align="right">图书名称:</td>
			<td><input type="text" name="name" /></td>
		</tr>
		<tr>
			<td align="right">价    格:</td>
			<td><input type="text" name="price" /></td>
		</tr>
		<tr>
			<td align="right">数    量:</td>
			<td><input type="text" name="bookCount"/></td>
		</tr>
		<tr>
			<td align="right">作    者:</td>
			<td><input type="text" name="author"/></td>
		</tr>
		<tr>
			<td align="center" colspan="2">
				<input type="submit" value="添加"/>
			</td>
		</tr> 
	</table>
</form>
</body>
</html>

通过jsp文件实现添加操作:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" import="com.entity.Book"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加页</title>
</head>
<body>
<%request.setCharacterEncoding("UTF-8"); %>
<jsp:useBean id="book" class="com.entity.Book"/>
<jsp:setProperty property="*" name="book"/>
<% 
try{
	Class.forName("com.mysql.jdbc.Driver");
	String url="jdbc:mysql://localhost:3306/#";
	String username="root";
	String password="1234";
	Connection conn=DriverManager.getConnection(url,username,password);
	String sql="insert into book(name,price,bookCount,author)values(?,?,?,?)";
	
	PreparedStatement ps=conn.prepareStatement(sql);
	ps.setString(1,book.getName());
	ps.setDouble(2,book.getPrice());
	ps.setInt(3,book.getBookCount());
	ps.setString(4,book.getAuthor());
	int row=ps.executeUpdate();
	if(row>0){
		out.print("成功添加了"+row+"条数据!");
	}
	ps.close();
	conn.close();
}catch(Exception e){
	out.print("图书信息添加失败!");
	e.printStackTrace();
}

%>
</body>
</html>

通过Servlet实现添加操作:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/#", "root", "1234");
			String name=request.getParameter("name");
			String price=request.getParameter("price");
			String bookCount=request.getParameter("bookCount");
			String author=request.getParameter("author");
			String sql="insert into book(name,price,bookCount,author)values(?,?,?,?)";
			
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setString(1,name);
			ps.setString(2,price);
			ps.setString(3,bookCount);
			ps.setString(4,author);
			int row=ps.executeUpdate();
			if(row>0) {
				response.getWriter().print("添加成功!");
			}
			ps.close();
			conn.close();
			
		} catch (ClassNotFoundException e) {
			response.getWriter().print("添加失败!");
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}

本文地址:https://blog.csdn.net/qq_45893285/article/details/109922521

《JavaEE课程设计:数据库的增删改查之添加页(图书).doc》

下载本文的Word格式文档,以方便收藏与打印。