struts2+jdbc实现书籍的增删改查

最近在学习struts2.写个struts2+jdbc的代码,我是初学者 ,在写代码时出现了很多问题,七改八改的总算是实现了,互相学习吧,废话不多说,看代码:

JDK1.8+tomcat9+eclipse3

模型层
package model;

public class Book {
	private int id;
	
	private String bookname;
	private float bookprice;
	private String bookauthor;
	private String bookpublisher;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookname() {
		return bookname;
	}
	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	public float getBookprice() {
		return bookprice;
	}
	public void setBookprice(float bookprice) {
		this.bookprice = bookprice;
	}
	public String getBookauthor() {
		return bookauthor;
	}
	public void setBookauthor(String bookauthor) {
		this.bookauthor = bookauthor;
	}
	public String getBookpublisher() {
		return bookpublisher;
	}
	public void setBookpublisher(String bookpublisher) {
		this.bookpublisher = bookpublisher;
	}
	
}

DAO层
package DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Database.BookDatabase;
import model.Book;



public class BookDao {


	BookDatabase db = new BookDatabase();
	Connection conn = db.getConn();
	public void insertBook(Book book){
		try {
			String sql_insert = "insert into book(id,bookname,bookprice,bookauthor,bookpublisher) value(?,?,?,?,?)";
			PreparedStatement pst = conn.prepareStatement(sql_insert);
			pst.setInt(1, book.getId());
			pst.setString(2, book.getBookname());
			pst.setFloat(3, book.getBookprice());
			pst.setString(4, book.getBookauthor());
			pst.setString(5, book.getBookpublisher());
			pst.executeUpdate();
			}catch(Exception e) {
				e.printStackTrace();
			}

	}
	

		public void updateBook(Book book){
			try {
				String sql_update = "update book set bookname=?,bookprice=?,bookauthor=?,bookpublisher=? where id=?";
				PreparedStatement pst = conn.prepareStatement(sql_update);
				
				pst.setString(1, book.getBookname());
				pst.setFloat(2, book.getBookprice());
				pst.setString(3, book.getBookauthor());
				pst.setString(4, book.getBookpublisher());
				pst.setInt(5,book.getId());
				pst.executeUpdate();
			}catch(Exception e) {
				e.printStackTrace();
			}
			
		}
	

	public List<Book> listBooks(){
		ResultSet rs = null;
		
		List<Book> list = new ArrayList<Book>();
		try {
			String sql_select = "select * from book";
			PreparedStatement pst=conn.prepareStatement(sql_select);
			 rs=pst.executeQuery();
			 while(rs.next()){
				 Book book = new Book();
				 book.setId(rs.getInt("id"));
				book.setBookname(rs.getString("bookname"));
				book.setBookprice(rs.getFloat("bookprice"));
				book.setBookauthor(rs.getString("bookauthor"));
				book.setBookpublisher(rs.getString("bookpublisher"));
				 list.add(book);
			 }

		}catch(Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	

	public Book getBook(int id){
		Book book = new Book();
		ResultSet rs = null;
		try {
			String sql_selectid = "select * from book where id=?";
			PreparedStatement pst = conn.prepareStatement(sql_selectid);
			 pst.setInt(1, id);
			 rs=pst.executeQuery();
			 while(rs.next()) {
				 	book.setId(rs.getInt("id"));
				 	book.setBookname(rs.getString("bookname"));
					book.setBookprice(rs.getFloat("bookprice"));
					book.setBookauthor(rs.getString("bookauthor"));
					book.setBookpublisher(rs.getString("bookpublisher"));
				 
			 }
		}catch(Exception e) {
			e.printStackTrace();
		}
		return book;
	}
	

	public void deleteBook(int id){
		try {
			String sql_delete = "delete from book where id=?";
			PreparedStatement pst = conn.prepareStatement(sql_delete);
			pst.setInt(1, id);
			pst.executeUpdate();
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
}

service层
package service;

import java.util.List;
import java.util.Map;

import DAO.BookDao;
import model.Book;

public class BookService {

	public void insertBook(Book book){
		BookDao bookdao = new BookDao();
		bookdao.insertBook(book);
	}
	

	public void updateBook(Book book){
		BookDao bookdao = new BookDao();
		bookdao.updateBook(book);
	}
	

	public List<Book> listBooks(){
		BookDao bookdao = new BookDao();
		return bookdao.listBooks();
	}
	

	public Book getBook(int id){
		BookDao bookdao = new BookDao();
		return bookdao.getBook(id);
	}
	

	public void deleteBook(int id){
		BookDao bookdao = new BookDao();
		bookdao.deleteBook(id);
	}
}

Action

public class BookAction extends ActionSupport {
	BookService bookservice = new BookService();
	private Book book;
	private int id;
	private List<Book> booklist;

	
	public List<Book> getBooklist() {
		return booklist;
	}

	public void setBooklist(List<Book> booklist) {
		this.booklist = booklist;
	}

	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	/**
	 * 
	 * @return
	 */
	public String prepareDeleteBook(){
		
		ActionContext.getContext().put("booklist", new BookService().listBooks());
		return "showDeleteBookList";
	}
	
	/**
	 * 
	 * @return
	 */
	public String prepareUpdateBook(){
		book = bookservice.getBook(id);
		//ActionContext.getContext().put("booklist",bookservice.getBook(id));
		return "showUpdateBookList";
	}
	
	
	public String addBook(){
		System.out.println(book.getBookauthor());

		bookservice.insertBook(book);
		ActionContext.getContext().put("booklist", new BookService().listBooks());
		System.out.println("添加成功");
		return "addbookOK";
	}
	
	public String deleteBook(){
		bookservice.deleteBook(id);
		System.out.println(id);
		return "deleteOK";
	}
	
	public String updateBook(){
		ActionContext ac = ActionContext.getContext();

		bookservice.updateBook(book);
		return "updateOK";
	}
	
	public String selectBook(){
		//bookservice.listBooks();
		ActionContext.getContext().put("booklist", new BookService().listBooks());
		return "selectOK";
	}
	
	
	
//	@Override
//	public Book getModel() {
//		return book;
//	}

}

struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
	
	<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
	

	


	<package name="struts2" extends="struts-default" namespace="/">
		<action name="*_*Action" class="action.{2}Action" method="{1}{2}">
		
		<!-- User result -->
			<param name="savepath">f:</param>
			<result name="index">/index.jsp</result>		
			<result name="welcome">/user/welcome.jsp</result>
			<result name="input">/regist.jsp</result>
			
		<!-- Book result -->
			<result name="selectOK">/user/selectBook.jsp</result>
			<result name="addbookOK" type="chain">select_BookAction</result>
			
			
			<result name="deleteOK" type="chain">select_BookAction</result>
			
			<result name="showUpdateBookList">/user/preUpdate.jsp</result>
			<result name="showUpdateBook">/user/updateBook.jsp</result>
			<result name="updateOK" type="chain">select_BookAction</result>
			
		</action>
	</package>
	


	
</struts>
最后显示层
welcome.jsp  作为用户登录后的页面
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome !!!</title>
</head>
<body>
	<a href="user/addBook.jsp" target="showFrame" >添加书籍</a>
	<a href="select_BookAction" target="showFrame" >查询书籍</a><br>
	<a href="user/updateBook.jsp" target="showFrame"></a>
	<iframe name="showFrame" frameborder="1" width="600px" height="300px"></iframe>	
</body>
</html>

addBook.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<s:form action="add_BookAction" method="post">

	<s:textfield name="book.bookname" label="书名"></s:textfield>
	<s:textfield name="book.bookprice" label="价格"></s:textfield>
		<s:textfield name="book.bookauthor" label="作者"></s:textfield>
		<s:textfield name="book.bookpublisher" label="出版社"></s:textfield>
		<s:submit value="添加"></s:submit>
	</s:form>
</body>
</html>

selectBook.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1px">

		<tr><th>ID</th><th>书名</th><th>价格</th><th>作者</th><th>出版社</th></tr>
		<c:forEach var="map" items="${booklist}" begin="0" varStatus="st">		
			<tr bgcolor="${st.index % 2 == 1 ? '#EFEFEF' : 'FFFFFF'}">
			<td>${map.id}</td>
			<td>${map.bookname}</td>
			<td>${map.bookprice}</td>
			<td>${map.bookauthor}</td>
			<td>${map.bookpublisher}</td>
			<td><a href="prepareUpdate_BookAction?id=${map.id}">修改</a></td>
			<td><a href="delete_BookAction?id=${map.id}">删除</a></td>
			</tr>
		</c:forEach>
	
	</table>
	<s:debug/>
</body>
</html>

preUpdate.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="update_BookAction" method="post">
<s:hidden name="book.id"></s:hidden>
<s:textfield name="book.bookname" label="书名"></s:textfield>
<s:textfield name="book.bookprice" label="价格"></s:textfield>
<s:textfield name="book.bookauthor" label="作者"></s:textfield>
<s:textfield name="book.bookpublisher" label="出版社"></s:textfield>
<s:submit value="更新"></s:submit>
</s:form>
<s:debug/>
</body>
</html>

updateBook.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
	<form action="update_BookAction">
		书名<input type="text" name="bookname" value="${book.bookname}" /><br>
		价格<input type="text" name="bookprice" value="${book.bookprice}" /><br>
		作者<input type="text" name="bookauthor" value="${book.bookauthor}" /><br>
		出版社<input type="text" name="bookpublisher" value="${book.bookpublisher}" /><br>
		<input type="submit" value="修改" />
	<s:debug/>
	</form>
</body>
</html>

要代码的同学把邮箱留在下面。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值