javaweb 项目school的增删改查(共用层)

school项目

项目school的增删改查内容较多,在这里只展示核心代码。

项目连接数据库通过JDBC,然后在网页上进行一些列操作(增删改查),实现具体的功能。

下面是项目的要实现的界面:
在这里插入图片描述
在这里插入图片描述
建立公共层:com.dao,com.bean,com.entity,com.filter,com.service,com.serviceimpl com.util

在com.entity上是一些get/set方法,在这里就不演示了。

公共层代码com.service以及com.serviceimpl:

BookService上的代码

package com.service;

import java.util.List;

import com.entity.Book;

public interface BookService {

   public List<Book> getBookList();
   public int deleteBook(String id);
   public int insertBook(Book book);
   public Book getBook(String id); 
   public int updateBook(Book book);
}

UserService上的代码:

package com.service;

import com.entity.User;

public interface UserService {

	  public User getLogin(String username,String password);
}

BookServiceImpl:

package com.serviceimpl;

import java.util.ArrayList;
import java.util.List;

import com.dao.BookDao;
import com.entity.Book;
import com.service.BookService;


public class BookServiceImpl implements BookService {

	BookDao bookDao = new BookDao();
	
	@Override
	public List<Book> getBookList() {
		String sql ="select * from t_book"; 
		List<Book> bookList = new ArrayList<>();
		
		try {
			bookList = bookDao.queryList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return bookList;
	}
	@Override
	public int deleteBook(String id){
   	 String sql = "delete from t_book where id = ?";
   	int book = 0;
   	 
   	 try {
		book = bookDao.update(sql, id);
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   	 return book;
    }
	public int insertBook(Book book){
		String sql = "insert into t_book(bookname,bookauthor,bookpublic) values(?,?,?)";
	    int update = 0;
	    
	    try {
			update = bookDao.update(sql,
					book.getBookname(),
					book.getBookauthor(),
					book.getBookpublic());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return update;
	}
	@Override
	public Book getBook(String id) {
		String sql = "select * from t_book where id = ?";
		Book book = null;
		try {
			book = bookDao.querySingle(sql, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return book;
	}
	@Override
	public int updateBook(Book book) {
		String sql = "update t_book set bookname = ? ,bookauthor = ? ,bookpublic = ? where id = ?";
		int update = 0;
		try {
			 update = bookDao.update(sql, 
					book.getBookname(),
					book.getBookauthor(),
					book.getBookpublic(),
					book.getId());
		} catch (Exception e) { 
			e.printStackTrace();
		}
		return update;
	}
	
}

UserServiceImpl:

package com.serviceimpl;

import com.dao.UserDao;
import com.entity.User;
import com.service.UserService;

public class UserServiceImpl implements UserService {

	UserDao userDao = new UserDao();

	@Override
	public User getLogin(String username, String password) {

		String sql = "select * from t_user where username = ? and password = ? ";
		User user = new User();
		try {
			// public Object scalar(String sql, Object... objects)
			user = userDao.querySingle(sql, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值