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;
}
}