- 浏览: 15900 次
- 性别:
最新评论
是一个图书的增删改查
JDBC的连接
package com.zhouxiaobo.demo.jdbc.util;
import java.sql.Connection;
public interface DBUtil {
public Connection openConn();
public void closeConn();
}
重写
package com.zhouxiaobo.demo.jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DefaultDBUtil implements DBUtil{
public static String url="jdbc:mysql://localhost:3306/d";
public static String username="root";
public static String password="root";
public static String drive="com.mysql.jdbc.Driver";
public Connection conn=null;
@Override
public Connection openConn() /*throws RuntimeException */ {
try{
Class.forName(drive);
conn = DriverManager.getConnection(url,username,password);
return conn;
}catch(Exception e){
//throw new RuntimeException(e);
e.getMessage();
return null;
}
}
@Override
public void closeConn()throws RuntimeException {
try{
conn.close();
}catch(Exception s){
throw new RuntimeException(s);
}
}
}
Bean层
package com.zhouxiaobo.demo.jdbc.bean;
public class Book {
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String sn;
private String name;
}
BookLend 层
package com.zhouxiaobo.demo.jdbc.bean;
public class BookLend {
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
private Book book;
private Student student;
private int number;
}
BookStore 层
package com.zhouxiaobo.demo.jdbc.bean;
public class BookStore {
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
}
private Book book;
private int totalNumber;
}
Student 层
package com.zhouxiaobo.demo.jdbc.bean;
public class Student {
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;
}
private String id;
private String name;
}
DAO层
package com.zhouxiaobo.demo.jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zhouxiaobo.demo.jdbc.bean.Book;
public class BookDao {
public List<Book> findbysn(String sn,Connection conn){
String sql="select * from book where sn=?";
List<Book> books=new ArrayList<Book>();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book book=new Book();
book.setSn(rs.getString(1));
book.setName(rs.getString(2));
books.add(book);
}
return books;
}catch(SQLException s){
s.printStackTrace();
return null;
}
}
public void insert(String sn,String name,Connection conn){
String sql="insert book values(? ,?)";
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
ps.setString(2, name);
row=ps.executeUpdate();
if(row>0){
System.out.println("success");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public void update(String sn,String name,Connection conn){
String sql="update book set name=? where sn=?";
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2, sn);
row=ps.executeUpdate();
if(row>0){
System.out.println("Success!");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public List<Book> getquery(Connection conn){
String sql="select * from book";
List<Book> books=new ArrayList<Book>();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book book=new Book();
book.setSn(rs.getString(1));
book.setName(rs.getString(2));
books.add(book);
}
return books;
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
public void delete(String sn,Connection conn){
String sql="delete from book where sn=?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
int row=0;
row=ps.executeUpdate();
if(row>0){
System.out.println("Success!");
}
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
package com.zhouxiaobo.demo.jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BookLendDao {
public void insert(String sn, String id, int number, Connection conn) {
String sql = "insert booklend values(?,?,?)";
try {
int row = 0;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, sn);
ps.setString(2, id);
ps.setInt(3, number);
row = ps.executeUpdate();
if (row > 0) {
System.out.println("success");
}
} catch (SQLException s) {
s.printStackTrace();
}
}
public void delete(String sn,Connection conn){
String sql="delete from booklend where sn=?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
int row=0;
row=ps.executeUpdate();
if(row>0){
System.out.println("success");
}
}catch(SQLException s){
s.printStackTrace();
}
}
}
package com.zhouxiaobo.demo.jdbc.dao;
import java.awt.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.bean.BookStore;
public class BookStoreDao {
public void update(String sn, int totalNumber, Connection conn){
String sql="update bookstore set totalNumber=? where sn=?";
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,totalNumber);
ps.setString(2, sn);
row=ps.executeUpdate();
if(row>0){
System.out.println("���³ɹ���");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public int getbooknumberbysn(String sn,Connection conn){
String sql="select * from bookstore where sn=?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
if(rs.next()){
String s1=rs.getNString(3);
System.out.println("�Ȿ��һ����"+s1+"��");
int a=Integer.parseInt(s1);
return a;
}
}catch(SQLException s){
s.printStackTrace();
}
return 0;
}
public void insert(Book book, int totalNumber, Connection conn ){
String sql="insert bookstore value(?,?,?)";
BookStore bookStore=new BookStore();
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, bookStore.getBook().getSn());
ps.setString(2, bookStore.getBook().getName());
ps.setInt(3, bookStore.getTotalNumber());
row=ps.executeUpdate();
if(row>1){
System.out.println("����ɹ���");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public void delete(String sn,Connection conn){
String sql="delete from bookstore where sn=?";
BookStore bookStore=new BookStore();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,bookStore.getBook().getSn());
int row=0;
row=ps.executeUpdate();
if(row>0){
System.out.println("ɾ��ɹ���");
}
}catch(SQLException s){
s.printStackTrace();
}finally{
}
}
public void findbysn(String sn,Connection conn){
String sql="select *from bookstore where sn=?";
BookStore bookStore=new BookStore();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, bookStore.getBook().getSn());
ResultSet rs=ps.executeQuery();
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
System.out.println(s1+"\t"+s2+"\t"+s3);
}catch(SQLException a){
a.printStackTrace();
}
}
/*public List<BookStore> getquery(Connection conn){
String sql="select * from bookstore";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
List<BookStore> bookList =new ArrayList<BookStore>();
while(rs.next()){
BookStore bookStore=new BookStore();
//bookStore.setBook()
}
}catch(SQLException s){
s.printStackTrace();
}
}*/
}
三个有点相似 看一个就好
Service层
接口
package com.zhouxiaobo.demo.jdbc.service;
import java.sql.Connection;
import java.util.List;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.bean.BookLend;
import com.zhouxiaobo.demo.jdbc.bean.Student;
public interface BussinessService {
public void lendBook(Student student, Book book, int number);
public void returnbook(BookLend bookLend);
public List<Book> showallbooks();
public void setConn(Connection conn);
public void updatebook(String sn,String name,Connection conn);
public void deletebook(String sn,Connection conn);
public String insertbook(String sn,String name,Connection conn);
public List<Book> findbysn(String sn,Connection conn);
}
重写
package com.zhouxiaobo.demo.jdbc.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.bean.BookLend;
import com.zhouxiaobo.demo.jdbc.bean.Student;
import com.zhouxiaobo.demo.jdbc.dao.BookDao;
import com.zhouxiaobo.demo.jdbc.dao.BookLendDao;
import com.zhouxiaobo.demo.jdbc.dao.BookStoreDao;
import com.zhouxiaobo.demo.jdbc.exception.DemoJDBCException;
public class BussinessServiceImpl implements BussinessService {
private BookLendDao booklendDao;
private BookStoreDao bookStoreDao;
private Connection conn;
public BussinessServiceImpl(){
}
public BussinessServiceImpl(Connection conn){
this.conn = conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
@Override
public void lendBook(Student student, Book book, int number) {
booklendDao = new BookLendDao();
bookStoreDao = new BookStoreDao();
try{
conn.setAutoCommit(false);
//TODO: ��lendBookȥ�����������
booklendDao.insert(book.getSn(), student.getId(), number, conn);
//��ȡ�����ж��ٱ������
int total = bookStoreDao.getbooknumberbysn(book.getSn(),conn);
int currentNumber = total - number;
bookStoreDao.update(book.getSn(), currentNumber, conn);
//TODO: ��bookStoreDaoȥ�۳�ͼ��
conn.commit();
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
throw new DemoJDBCException("xzzxz",e);
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void returnbook(BookLend bookLend){
//ɾ����ļ�¼
booklendDao=new BookLendDao();
bookStoreDao=new BookStoreDao();
try{
conn.setAutoCommit(false);
booklendDao.delete(bookLend.getBook().getSn(), conn);
//ɾ����ļ�¼
int number=bookStoreDao.getbooknumberbysn(bookLend.getBook().getSn(),conn);
int totalNumber=number+bookLend.getNumber();
bookStoreDao.update(bookLend.getBook().getSn(), totalNumber, conn);
conn.commit();
}catch(Exception x){
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Book> showallbooks(){
BookDao bookDao=new BookDao();
List<Book> result = null;
try{
conn.setAutoCommit(false);
return bookDao.getquery(conn);
}catch (SQLException e) {
// TODO: handle exception
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return null;
}
public void updatebook(String sn,String name,Connection conn){
BookDao bookDao=new BookDao();
bookDao.update(sn, name, conn);
}
public void deletebook(String sn,Connection conn){
BookDao bookDao=new BookDao();
try{
conn.setAutoCommit(false);
bookDao.delete(sn, conn);
conn.commit();
}catch(SQLException x){
x.printStackTrace();
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String insertbook(String sn,String name,Connection conn){
BookDao bookDao=new BookDao();
List<Book> books=new ArrayList<Book>();
books=bookDao.findbysn(sn, conn);
if( books.size()==0){
try{
conn.setAutoCommit(false);
bookDao.insert(sn, name, conn);
conn.commit();
return "1";
}catch(SQLException s){
s.printStackTrace();
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "1";
}
}else{
return sn;
}
}
public List<Book> findbysn(String sn,Connection conn){
BookDao bookDao=new BookDao();
List<Book> result = null;
try{
conn.setAutoCommit(false);
return bookDao.findbysn(sn, conn);
}catch (Exception e) {
// TODO: handle exception
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
return null;
}
}
}
Action层
package com.zhouxiaobo.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.service.BussinessService;
import com.zhouxiaobo.demo.jdbc.service.BussinessServiceImpl;
import com.zhouxiaobo.demo.jdbc.util.DBUtil;
import com.zhouxiaobo.demo.jdbc.util.DefaultDBUtil;
/**
* Servlet implementation class FindAllBook
*/
public class FindAllBook extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public FindAllBook() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DBUtil dbUtil=new DefaultDBUtil();
try{
Connection conn = dbUtil.openConn();
BussinessService businessService = new BussinessServiceImpl();
businessService.setConn(conn);
List<Book> BookStore = businessService.showallbooks();
request.setAttribute("book",BookStore);
request.getRequestDispatcher("/booklist.jsp").forward(request, response);
}finally{
dbUtil.closeConn();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
JSP
界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<h1>图书管理系统</h1>
<p align="center">
</p>
<td><a href="/LIB/Addbook.jsp">添加图书</a></td>
<td><a href="/LIB/findAllBook">查看所有图书</a></td>
<td><a href="/LIB/editBookServlet">更改图书</a></td>
<td><a href="/LIB/Find.jsp">按条件查找</a></td>
</body>
</html>
显示所有图书
<%@page import="java.util.List"%>
<%@page import="com.zhouxiaobo.demo.jdbc.bean.Book"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<table border="1" cellspacing="0" cellpadding="0 width="100%" style="align:center;">
<tr bgcolor="AAAAAA" style="font-weight:bold;">
<th>编号</th><th>书名</th><th><th width="0" colspan="2"></th>
<c:forEach var="bk" items="${book}" varStatus="status">
<tr>
<c:if test="${status.count%2==0}" >
<tr bgcolor="CCC000" style="font-weight:bold;">
</c:if>
<c:if test="${status.count%2==1}">
<tr bgcolor="FFFFOO" style="font-weight:bold;">
</c:if>
<td>${bk.sn}</td>
<td>${bk.name}</td>
<td><a href="/LIB/deleteBookServlet?sn=${bk.sn}">删除</a>|<a href="/LIB/editBookServlet?sn=${bk.sn}">更改</a></td>
</c:forEach>
<c:if test="${!empty flag}">
ISBN ${flag}is deleted!
</c:if>
<c:if test="${!empty flag1}">
ISBN ${flag1} is haved !
</c:if>
</tr>
</table>
JDBC的连接
package com.zhouxiaobo.demo.jdbc.util;
import java.sql.Connection;
public interface DBUtil {
public Connection openConn();
public void closeConn();
}
重写
package com.zhouxiaobo.demo.jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
public class DefaultDBUtil implements DBUtil{
public static String url="jdbc:mysql://localhost:3306/d";
public static String username="root";
public static String password="root";
public static String drive="com.mysql.jdbc.Driver";
public Connection conn=null;
@Override
public Connection openConn() /*throws RuntimeException */ {
try{
Class.forName(drive);
conn = DriverManager.getConnection(url,username,password);
return conn;
}catch(Exception e){
//throw new RuntimeException(e);
e.getMessage();
return null;
}
}
@Override
public void closeConn()throws RuntimeException {
try{
conn.close();
}catch(Exception s){
throw new RuntimeException(s);
}
}
}
Bean层
package com.zhouxiaobo.demo.jdbc.bean;
public class Book {
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String sn;
private String name;
}
BookLend 层
package com.zhouxiaobo.demo.jdbc.bean;
public class BookLend {
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
private Book book;
private Student student;
private int number;
}
BookStore 层
package com.zhouxiaobo.demo.jdbc.bean;
public class BookStore {
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
public int getTotalNumber() {
return totalNumber;
}
public void setTotalNumber(int totalNumber) {
this.totalNumber = totalNumber;
}
private Book book;
private int totalNumber;
}
Student 层
package com.zhouxiaobo.demo.jdbc.bean;
public class Student {
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;
}
private String id;
private String name;
}
DAO层
package com.zhouxiaobo.demo.jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zhouxiaobo.demo.jdbc.bean.Book;
public class BookDao {
public List<Book> findbysn(String sn,Connection conn){
String sql="select * from book where sn=?";
List<Book> books=new ArrayList<Book>();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book book=new Book();
book.setSn(rs.getString(1));
book.setName(rs.getString(2));
books.add(book);
}
return books;
}catch(SQLException s){
s.printStackTrace();
return null;
}
}
public void insert(String sn,String name,Connection conn){
String sql="insert book values(? ,?)";
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
ps.setString(2, name);
row=ps.executeUpdate();
if(row>0){
System.out.println("success");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public void update(String sn,String name,Connection conn){
String sql="update book set name=? where sn=?";
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2, sn);
row=ps.executeUpdate();
if(row>0){
System.out.println("Success!");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public List<Book> getquery(Connection conn){
String sql="select * from book";
List<Book> books=new ArrayList<Book>();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
Book book=new Book();
book.setSn(rs.getString(1));
book.setName(rs.getString(2));
books.add(book);
}
return books;
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
public void delete(String sn,Connection conn){
String sql="delete from book where sn=?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
int row=0;
row=ps.executeUpdate();
if(row>0){
System.out.println("Success!");
}
}catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
package com.zhouxiaobo.demo.jdbc.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class BookLendDao {
public void insert(String sn, String id, int number, Connection conn) {
String sql = "insert booklend values(?,?,?)";
try {
int row = 0;
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, sn);
ps.setString(2, id);
ps.setInt(3, number);
row = ps.executeUpdate();
if (row > 0) {
System.out.println("success");
}
} catch (SQLException s) {
s.printStackTrace();
}
}
public void delete(String sn,Connection conn){
String sql="delete from booklend where sn=?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, sn);
int row=0;
row=ps.executeUpdate();
if(row>0){
System.out.println("success");
}
}catch(SQLException s){
s.printStackTrace();
}
}
}
package com.zhouxiaobo.demo.jdbc.dao;
import java.awt.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.bean.BookStore;
public class BookStoreDao {
public void update(String sn, int totalNumber, Connection conn){
String sql="update bookstore set totalNumber=? where sn=?";
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setInt(1,totalNumber);
ps.setString(2, sn);
row=ps.executeUpdate();
if(row>0){
System.out.println("���³ɹ���");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public int getbooknumberbysn(String sn,Connection conn){
String sql="select * from bookstore where sn=?";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
if(rs.next()){
String s1=rs.getNString(3);
System.out.println("�Ȿ��һ����"+s1+"��");
int a=Integer.parseInt(s1);
return a;
}
}catch(SQLException s){
s.printStackTrace();
}
return 0;
}
public void insert(Book book, int totalNumber, Connection conn ){
String sql="insert bookstore value(?,?,?)";
BookStore bookStore=new BookStore();
try{
int row=0;
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, bookStore.getBook().getSn());
ps.setString(2, bookStore.getBook().getName());
ps.setInt(3, bookStore.getTotalNumber());
row=ps.executeUpdate();
if(row>1){
System.out.println("����ɹ���");
}
}catch(SQLException s){
s.printStackTrace();
}
}
public void delete(String sn,Connection conn){
String sql="delete from bookstore where sn=?";
BookStore bookStore=new BookStore();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,bookStore.getBook().getSn());
int row=0;
row=ps.executeUpdate();
if(row>0){
System.out.println("ɾ��ɹ���");
}
}catch(SQLException s){
s.printStackTrace();
}finally{
}
}
public void findbysn(String sn,Connection conn){
String sql="select *from bookstore where sn=?";
BookStore bookStore=new BookStore();
try{
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1, bookStore.getBook().getSn());
ResultSet rs=ps.executeQuery();
String s1=rs.getString(1);
String s2=rs.getString(2);
String s3=rs.getString(3);
System.out.println(s1+"\t"+s2+"\t"+s3);
}catch(SQLException a){
a.printStackTrace();
}
}
/*public List<BookStore> getquery(Connection conn){
String sql="select * from bookstore";
try{
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
List<BookStore> bookList =new ArrayList<BookStore>();
while(rs.next()){
BookStore bookStore=new BookStore();
//bookStore.setBook()
}
}catch(SQLException s){
s.printStackTrace();
}
}*/
}
三个有点相似 看一个就好
Service层
接口
package com.zhouxiaobo.demo.jdbc.service;
import java.sql.Connection;
import java.util.List;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.bean.BookLend;
import com.zhouxiaobo.demo.jdbc.bean.Student;
public interface BussinessService {
public void lendBook(Student student, Book book, int number);
public void returnbook(BookLend bookLend);
public List<Book> showallbooks();
public void setConn(Connection conn);
public void updatebook(String sn,String name,Connection conn);
public void deletebook(String sn,Connection conn);
public String insertbook(String sn,String name,Connection conn);
public List<Book> findbysn(String sn,Connection conn);
}
重写
package com.zhouxiaobo.demo.jdbc.service;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.bean.BookLend;
import com.zhouxiaobo.demo.jdbc.bean.Student;
import com.zhouxiaobo.demo.jdbc.dao.BookDao;
import com.zhouxiaobo.demo.jdbc.dao.BookLendDao;
import com.zhouxiaobo.demo.jdbc.dao.BookStoreDao;
import com.zhouxiaobo.demo.jdbc.exception.DemoJDBCException;
public class BussinessServiceImpl implements BussinessService {
private BookLendDao booklendDao;
private BookStoreDao bookStoreDao;
private Connection conn;
public BussinessServiceImpl(){
}
public BussinessServiceImpl(Connection conn){
this.conn = conn;
}
public void setConn(Connection conn) {
this.conn = conn;
}
@Override
public void lendBook(Student student, Book book, int number) {
booklendDao = new BookLendDao();
bookStoreDao = new BookStoreDao();
try{
conn.setAutoCommit(false);
//TODO: ��lendBookȥ�����������
booklendDao.insert(book.getSn(), student.getId(), number, conn);
//��ȡ�����ж��ٱ������
int total = bookStoreDao.getbooknumberbysn(book.getSn(),conn);
int currentNumber = total - number;
bookStoreDao.update(book.getSn(), currentNumber, conn);
//TODO: ��bookStoreDaoȥ�۳�ͼ��
conn.commit();
}catch(Exception e){
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
throw new DemoJDBCException("xzzxz",e);
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void returnbook(BookLend bookLend){
//ɾ����ļ�¼
booklendDao=new BookLendDao();
bookStoreDao=new BookStoreDao();
try{
conn.setAutoCommit(false);
booklendDao.delete(bookLend.getBook().getSn(), conn);
//ɾ����ļ�¼
int number=bookStoreDao.getbooknumberbysn(bookLend.getBook().getSn(),conn);
int totalNumber=number+bookLend.getNumber();
bookStoreDao.update(bookLend.getBook().getSn(), totalNumber, conn);
conn.commit();
}catch(Exception x){
try {
conn.rollback();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Book> showallbooks(){
BookDao bookDao=new BookDao();
List<Book> result = null;
try{
conn.setAutoCommit(false);
return bookDao.getquery(conn);
}catch (SQLException e) {
// TODO: handle exception
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
return null;
}
public void updatebook(String sn,String name,Connection conn){
BookDao bookDao=new BookDao();
bookDao.update(sn, name, conn);
}
public void deletebook(String sn,Connection conn){
BookDao bookDao=new BookDao();
try{
conn.setAutoCommit(false);
bookDao.delete(sn, conn);
conn.commit();
}catch(SQLException x){
x.printStackTrace();
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public String insertbook(String sn,String name,Connection conn){
BookDao bookDao=new BookDao();
List<Book> books=new ArrayList<Book>();
books=bookDao.findbysn(sn, conn);
if( books.size()==0){
try{
conn.setAutoCommit(false);
bookDao.insert(sn, name, conn);
conn.commit();
return "1";
}catch(SQLException s){
s.printStackTrace();
try {
conn.rollback();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "1";
}
}else{
return sn;
}
}
public List<Book> findbysn(String sn,Connection conn){
BookDao bookDao=new BookDao();
List<Book> result = null;
try{
conn.setAutoCommit(false);
return bookDao.findbysn(sn, conn);
}catch (Exception e) {
// TODO: handle exception
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
return null;
}
}
}
Action层
package com.zhouxiaobo.demo;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Collection;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhouxiaobo.demo.jdbc.bean.Book;
import com.zhouxiaobo.demo.jdbc.service.BussinessService;
import com.zhouxiaobo.demo.jdbc.service.BussinessServiceImpl;
import com.zhouxiaobo.demo.jdbc.util.DBUtil;
import com.zhouxiaobo.demo.jdbc.util.DefaultDBUtil;
/**
* Servlet implementation class FindAllBook
*/
public class FindAllBook extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public FindAllBook() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
DBUtil dbUtil=new DefaultDBUtil();
try{
Connection conn = dbUtil.openConn();
BussinessService businessService = new BussinessServiceImpl();
businessService.setConn(conn);
List<Book> BookStore = businessService.showallbooks();
request.setAttribute("book",BookStore);
request.getRequestDispatcher("/booklist.jsp").forward(request, response);
}finally{
dbUtil.closeConn();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
JSP
界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<h1>图书管理系统</h1>
<p align="center">
</p>
<td><a href="/LIB/Addbook.jsp">添加图书</a></td>
<td><a href="/LIB/findAllBook">查看所有图书</a></td>
<td><a href="/LIB/editBookServlet">更改图书</a></td>
<td><a href="/LIB/Find.jsp">按条件查找</a></td>
</body>
</html>
显示所有图书
<%@page import="java.util.List"%>
<%@page import="com.zhouxiaobo.demo.jdbc.bean.Book"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<table border="1" cellspacing="0" cellpadding="0 width="100%" style="align:center;">
<tr bgcolor="AAAAAA" style="font-weight:bold;">
<th>编号</th><th>书名</th><th><th width="0" colspan="2"></th>
<c:forEach var="bk" items="${book}" varStatus="status">
<tr>
<c:if test="${status.count%2==0}" >
<tr bgcolor="CCC000" style="font-weight:bold;">
</c:if>
<c:if test="${status.count%2==1}">
<tr bgcolor="FFFFOO" style="font-weight:bold;">
</c:if>
<td>${bk.sn}</td>
<td>${bk.name}</td>
<td><a href="/LIB/deleteBookServlet?sn=${bk.sn}">删除</a>|<a href="/LIB/editBookServlet?sn=${bk.sn}">更改</a></td>
</c:forEach>
<c:if test="${!empty flag}">
ISBN ${flag}is deleted!
</c:if>
<c:if test="${!empty flag1}">
ISBN ${flag1} is haved !
</c:if>
</tr>
</table>
发表评论
-
删除<作废>
2015-05-03 15:54 485<table class="tbl-box& ... -
excel转换成JAVA到Mysql
2015-04-29 13:43 619excel转换成JAVA到Mysql -
第一个项目的jsp<不会的很多啊!!!>
2015-04-27 18:27 807<%@ page language="java ... -
/*发送短信*/
2015-04-27 18:23 351/*发送邮件*/ $("#sendEma ... -
jquery 删除
2015-04-26 18:32 415$("#btn_clear").click ... -
省市区
2015-04-27 18:23 541<script> $(function(){ ... -
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
2015-04-26 13:16 896<%@ taglib uri="http:// ... -
sql
2015-04-24 20:24 759<?xml version="1.0" ... -
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
2015-04-24 14:43 1649<%@ taglib prefix="spri ... -
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
2015-04-24 14:44 960<%@ taglib prefix="c&qu ... -
<%@ taglib prefix="code" uri="/WEB-INF/tld/code4dropdown.tld"%>
2015-04-24 14:44 640<%@ taglib prefix="code ... -
Android工具下载
2015-04-24 14:44 421原文:http://blog.csdn.net/shulian ... -
mybatis的配置
2015-04-08 16:49 532jdbc.properties driver=com.my ... -
mybatis
2015-04-08 16:45 447pom <project xmlns="htt ... -
JAVA 入门简单的增删改查(Servlet+JSP)二
2015-04-08 16:42 931接着上次的 DAO层 删除操作 package com.zho ...
相关推荐
技术栈 Servlet + Jsp + Tomcat , 是Java Web入门非常好的练手项目.zip 技术栈 Servlet + Jsp + Tomcat , 是Java Web入门非常好的练手项目.zip 技术栈 Servlet + Jsp + Tomcat , 是Java Web入门非常好的练手项目.zip...
在IT行业中,Web开发是一项核心技能,而"jsp+servlet+mysql+tomcat实现增删改查"是初学者常见的入门实践项目。这个项目旨在帮助新手掌握基于Java Web的动态网站开发技术,通过Eclipse这样的集成开发环境(IDE)进行...
Java入门练手项目, 基于 Servlet + Jsp + Tomcat
总的来说,Java Servlet+JDBC的增删改查涵盖了Web开发的基本流程,是学习Java Web的入门基础。理解并熟练掌握这些知识点,可以为后续的Web框架学习(如Spring Boot、Struts、JSF等)打下坚实的基础。在实际开发中,...
总之,"Servlet+jsp注册登录增删改查万能模板"是一个全面的Java Web学习资源,涵盖了Web开发的基本元素,适合初学者入门和实践,同时也为更高级的项目开发奠定了基础。通过深入学习和实践,可以掌握Java Web开发的...
Java入门教程:基于JDBC、Servlet和JSP的增删改查操作 在Java开发领域,Web应用程序的构建是常用的任务之一,而增删改查(CRUD)操作是任何数据库驱动应用的基础。本教程将重点讲解如何使用Java标准库JDBC、Servlet...
项目名称:基于JSP+Servlet+Mysql的宠物管理系统(简单增删改查) 项目架构:B/S架构 开发语言:Java语言 后端技术:JSP、Servlet、JDBC等技术 运行环境:Win10、JDK1.8 数 据 库:MySQL5.7及以上 运行服务器:...
本项目是一个基于JSP、Servlet和JDBC技术的简单注册登录系统,旨在帮助初学者快速理解和掌握这些核心技术在实际开发中的应用。在这个项目中,开发者将使用MySQL作为后端数据库,创建一个名为`test`的数据库,并在...
项目名称:基于JSP+Servlet+Mysql的学生宿舍管理系统(简单的增删改查) 项目架构:B/S架构 开发语言:Java语言 ...宿舍的增删改查,使之成为了一个完成的增删改查模块,功能比较简单,适合小白入门。
一个使用jsp+servlet+mysql开发简单宠物管理系统,功能如下: 包含宠物的按分类查询,添加宠物、编辑...原来没有编辑、删除功能,在基础上做了升级,使之成为了一个完成的增删改查模块,功能比较简单,适合小白入门。
基于javaBean+jsp+servlet+mysql开发的模拟百度网盘的个人网盘系统源码(课程作业).zip基于javaBean+jsp+servlet+mysql开发的模拟百度网盘的个人网盘系统源码(课程作业).zip基于javaBean+jsp+servlet+mysql开发的模拟...
宿舍管理系统-jsp+servlet+mysql宿舍管理系统-jsp+servlet+mysql宿舍管理系统-jsp+servlet+mysql宿舍管理系统-jsp+servlet+mysql宿舍管理系统-jsp+servlet+mysql宿舍管理系统-jsp+servlet+mysql宿舍管理系统-jsp+...
本项目是一个SSM入门实例,主要展示了如何使用这三个框架进行基本的增删改查操作,并结合数据库进行数据操作。 1. **Spring框架**:Spring是Java企业级应用的核心,提供了一个容器来管理对象(如Bean)的生命周期和...
以前不知道哪里找到的一个系统,感觉很不错,使用文档,数据库文件,项目截图全都包括,后台使用Servlet+Jsp,前台用的H-ui+EasyUI+jQuery,很适合新手入门的学习,特别是代码里面许许多多的注释,让我受益匪浅,...
### JAVA+JSP+Servlet+Android+SSH视频教程全集+项目视频知识点解析 #### Java基础知识及应用 Java作为一门跨平台的高级编程语言,在软件开发领域占据着重要的地位。本教程将涵盖Java基础语法、面向对象编程(OOP)...
基于servlet、jsp简单的增删改查询,运用反射创建了一个微型的框架,页面借助了bootstrap、jquery这些成熟的前端框架,本来是帮同学写的作业,快一年没接触Java了,在同学的请求下练练了手,可以做Java入门参考,...
【标题】"jsp+JavaBean+Servlet+MySQL宿舍管理系统(初学者)"是一个针对初学者设计的项目,旨在帮助他们理解并实践Web应用开发的基本技术。这个系统的核心是使用JSP(JavaServer Pages)来展示界面,JavaBean作为业务...
项目名称:基于JSP+Servlet+Mysql的宠物管理系统(简单增删改查) 项目架构:B/S架构 开发语言:Java语言 后端技术:JSP、Servlet、JDBC等技术 运行环境:Win10、JDK1.8 数 据 库:MySQL5.7及以上 运行服务器:...
适合刚刚入门javaweb学习的菜鸟 1.jdbc(baseDao+UserDao) 2.servlet 3.entity 注明: 1.jdbc:jdbc很详细(从头到尾的步骤)(适合jdbc基础不是很好的人群参考) 2.所用到的jar包(jstl+standard+mysql-connector-java-...
这个“JSP增删改查案例”是一个非常适合初学者的学习资源,旨在教授如何在Java Web环境中进行基本的数据操作,即CRUD(Create, Read, Update, Delete)操作。以下将详细介绍这些知识点。 首先,CRUD是数据库管理中...