文章目录
概要
基于Java连接数据库,三层分层,做个简单的图书管理系统,有增删改查功能,一看就会。这个是大学时的作业分享,涉及许多基础知识,希望对大家有用,话不多说看流程图。
准备:
推荐用idea开发,因为集成的maven自动帮你配置,不用手动弄,我是手动弄的,数据库软件Navicat。作者使用仅供参考。
流程图
整体架构流程
Java三层架构+MySQL+Swing
完成的功能
有可视化操作界面,可以注册用户,登录,然后对图书进行增删改查,注销账户
可以直接从这开始动手
基于三层架构由内往外写,直接看目录。
手搓这样的目录
在这多嘴一句,窗体和和view其实可以合并写,但是为了保证三层架构,所以多加个窗体,多做几个类似的项目的自己体会。
数据库创建
直接用Navicat点点点!!!
bean层
接下来的,都与上边的目录对应了。
注意对应数据库的字段,注意数据类型,Books实体类。
package com.sfk.bean;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 17:17
* @Description: com.sfk.bean
* @version: 1.0
*/
public class Books {
private String id;
private String isbn;
private String name;
private String author;
private String publisher;
private String price;
private String puDate;
private String deposit;
private int quantify;
private int lend;
public Books() {
}
public Books(String id, String isbn, String name, String author, String publisher, String price, String puDate, String deposit, int quantify, int lend) {
this.id = id;
this.isbn = isbn;
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
this.puDate = puDate;
this.deposit = deposit;
this.quantify = quantify;
this.lend = lend;
}
public Books(String isbn, String name, String author, String publisher, String price, String puDate, String deposit, int quantify) {
this.isbn = isbn;
this.name = name;
this.author = author;
this.publisher = publisher;
this.price = price;
this.puDate = puDate;
this.deposit = deposit;
this.quantify = quantify;
}
@Override
public String toString() {
return "Books{" +
"id='" + id + '\'' +
", isbn='" + isbn + '\'' +
", name='" + name + '\'' +
", author='" + author + '\'' +
", publisher='" + publisher + '\'' +
", price='" + price + '\'' +
", puDate='" + puDate + '\'' +
", deposit='" + deposit + '\'' +
", quantify=" + quantify +
", lend=" + lend +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getPuDate() {
return puDate;
}
public void setPuDate(String puDate) {
this.puDate = puDate;
}
public String getDeposit() {
return deposit;
}
public void setDeposit(String deposit) {
this.deposit = deposit;
}
public int getQuantify() {
return quantify;
}
public void setQuantify(int quantify) {
this.quantify = quantify;
}
public int getLend() {
return lend;
}
public void setLend(int lend) {
this.lend = lend;
}
}
User实体类
package com.sfk.bean;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 16:55
* @Description: com.sfk.bean
* @version: 1.0
*/
public class User {
private int id;
private Integer userName;
private String setPassword;
private String surePassword;
private String name;
private String sex;
private String birthday;
//空参构造器
public User(){
}
//有参构造器
public User(Integer userName, String setPassword, String surePassword, String name, String sex, String birthday) {
this.userName = userName;
this.setPassword = setPassword;
this.surePassword = surePassword;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
//toString
public User(int id, Integer userName, String setPassword, String surePassword, String name, String sex, String birthday) {
this.id = id;
this.userName = userName;
this.setPassword = setPassword;
this.surePassword = surePassword;
this.name = name;
this.sex = sex;
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName=" + userName +
", setPassword='" + setPassword + '\'' +
", surePassword='" + surePassword + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", brithday='" + birthday + '\'' +
'}';
}
//set & get
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getUserName() {
return userName;
}
public void setUserName(Integer userName) {
this.userName = userName;
}
public String getSetPassword() {
return setPassword;
}
public void setSetPassword(String setPassword) {
this.setPassword = setPassword;
}
public String getSurePassword() {
return surePassword;
}
public void setSurePassword(String surePassword) {
this.surePassword = surePassword;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBrithday() {
return birthday;
}
public void setBrithday(String brithday) {
this.birthday = brithday;
}
}
dao层
先写接口,再写实体类。
本来JDBC这个代码是在**imp中编写的,但是一个表就编写一次,提高代码利用率,直接抽出来写,然后直接继承即可提高复用性。一个是BaseDao,一个是DaoUtil,直接跟着操作就行。
dao层接口
IBooksDao
package com.sfk.dao;
import com.sfk.bean.Books;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 17:22
* @Description: com.sfk.dao
* @version: 1.0
*/
public interface IBooksDao {
//增加
public int addBooks(Books books);
//删除
public int deleteBooks(String id);
//修改
public int updateBooks(Books book);
public Books findBooksByIsbn(String isbn);
//查询
public List<Books> findBooksBySid(String name);
//全查
public List<Books> findAllBooks();
}
IUserDao
package com.sfk.dao;
import com.sfk.bean.User;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 17:22
* @Description: com.sfk.dao
* @version: 1.0
*/
public interface IUserDao {
//增加
public int addUser(User user);
//删除
public int deleteUser(User user);
//修改
public int updateUser(User user);
public User findUserByUserName(Integer userName);
//查询
public User findUserById(int id);
//全查
public List<User> findAllUser();
}
BaseDao
package com.sfk.dao.imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @Auther: shifk
* @Date: 2024/5/17 - 05 - 17 - 0:07
* @Description: com.sfk.dao
* @version: 1.0
*/
public class BaseDao {
protected Connection conn;
protected PreparedStatement prepstat;
protected ResultSet rs;
//增删改
protected int update(String sql,Object...args){
int ret = 0;
try {
conn = DaoUtil.getConn();
prepstat = conn.prepareStatement(sql);
if (args != null) {
for (int i = 0; i < args.length; i++) {
prepstat.setObject(i + 1,args[i]);
}
}
ret = prepstat.executeUpdate();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn, prepstat, rs);
}
return ret;
}
//查询
protected ResultSet query(String sql,Object...args){
ResultSet rs = null;
try {
conn = DaoUtil.getConn();
prepstat = conn.prepareStatement(sql);
if (args != null){
for (int i = 0; i < args.length; i++) {
prepstat.setObject(i + 1,args[i]);
}
}
rs = prepstat.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return rs;
}
}
DaoUtil
package com.sfk.dao.imp;
import java.sql.*;
/**
* @Auther: shifk
* @Date: 2024/5/17 - 05 - 17 - 0:06
* @Description: com.sfk.dao
* @version: 1.0
*/
public class DaoUtil {
//加载驱动
//驱动的加载只需加载一次,因此使用静态代码块,当加载此类时,会随着类的加载而加载一次
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConn(){
Connection conn = null;
try {
String url = "jdbc:mysql://localhost:3306/mybooks?charset=utf8mb4&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8&characterEncoding=utf-8&allowPublicKeyRetrieval=true";
String user = "root";
String pwd = "root";
conn = DriverManager.getConnection(url, user, pwd);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
//关闭连接
public static void closeResource(Connection conn, PreparedStatement prep, ResultSet res){
if (res != null){
try {
res.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (prep != null){
try {
prep.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
接下来是两个表的dao的**imp
BooksDaoImp
package com.sfk.dao.imp;
import com.sfk.bean.Books;
import com.sfk.dao.IBooksDao;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/17 - 05 - 17 - 0:09
* @Description: com.sfk.dao
* @version: 1.0
*/
public class BooksDaoImp extends BaseDao implements IBooksDao {
//增加记录
@Override
public int addBooks(Books books) {
String sql = "insert into books(isbn,name,author,publisher,price,puDate,deposit,quantify) values (?,?,?,?,?,?,?,?)";;
return update(sql,books.getIsbn(),books.getName(),books.getAuthor(),books.getPublisher(),books.getPrice(),books.getPuDate(),books.getDeposit(),books.getQuantify());
}
//删除记录
@Override
public int deleteBooks(String isbn) { //这里id是---String
String sql = "delete from books where isbn = ?";
return update(sql,isbn);
}
//修改记录
@Override
public int updateBooks(Books book) {
String sql = "update books set isbn = ?,name = ?,author = ?,publisher=?,price=?,puDate=?,deposit=?,quantify=?,lend=? where id = ?";
return update(sql,book.getIsbn(),book.getName(),book.getAuthor(),book.getPublisher(),book.getPrice(),book.getPuDate(),book.getDeposit(),book.getQuantify(),book.getLend(),book.getId());
}
//id查询
@Override
public List<Books> findBooksBySid(String name) {
List<Books> booksList = new ArrayList<>();
Books books = null;
//SimpleDateFormat format = new SimpleDateFormat(); 这里可以转时间格式,但不需要
try {
String sql = "select * from books where name = ?";
rs = query(sql, name);
while (rs.next()) {
books = new Books();
books.setId(rs.getString("id"));
books.setIsbn(rs.getString("isbn"));
books.setName(rs.getString("name"));
books.setAuthor(rs.getString("author"));
books.setPublisher(rs.getString("publisher"));
books.setPrice(rs.getString("price"));
books.setPuDate(rs.getString("puDate"));
books.setDeposit(rs.getString("deposit"));
books.setQuantify(rs.getInt("quantify"));
books.setLend(rs.getInt("lend"));
booksList.add(books);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn, prepstat, rs);
}
return booksList;
}
//isbn
@Override
public Books findBooksByIsbn(String isbn) {
Books book = null;
try {
String sql = "select * from books where isbn = ?";
book = new Books();
rs = query(sql, isbn);
while (rs.next()) {
book.setId(rs.getString("id"));
book.setIsbn(rs.getString("isbn"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPublisher(rs.getString("publisher"));
book.setPrice(rs.getString("price"));
book.setPuDate(rs.getString("puDate"));
book.setDeposit(rs.getString("deposit"));
book.setQuantify(rs.getInt("quantify"));
book.setLend(rs.getInt("lend"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn, prepstat, rs);
}
return book;
}
//全查
@Override
public List<Books> findAllBooks() {
Books books = null;
List list = new ArrayList();
//SimpleDateFormat format = new SimpleDateFormat();
try {
String sql = "select * from books";
rs = query(sql);
while (rs.next()) {
books = new Books();
books.setId(rs.getString("id"));
books.setIsbn(rs.getString("isbn"));
books.setName(rs.getString("name"));
books.setAuthor(rs.getString("author"));
books.setPublisher(rs.getString("publisher"));
books.setPrice(rs.getString("price"));
books.setPuDate(rs.getString("puDate"));
books.setDeposit(rs.getString("deposit"));
books.setQuantify(rs.getInt("quantify"));
books.setLend(rs.getInt("lend"));
list.add(books);
}
}
catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn, prepstat, rs);
}
return list;
}
}
UserDaoImp
package com.sfk.dao.imp;
import com.sfk.bean.User;
import com.sfk.dao.IUserDao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 17:24
* @Description: com.sfk.dao.imp
* @version: 1.0
*/
public class UserDaoImp extends BaseDao implements IUserDao {
//增加记录
@Override
public int addUser(User user) {
String sql = "insert into user(userName,setPassword,surePassword,name,sex,birthday) values (?,?,?,?,?,?)";
return update(sql,user.getUserName(),user.getSetPassword(),user.getSurePassword(),user.getName(),user.getSex(),user.getBrithday());
}
//删除记录
@Override
public int deleteUser(User user) {
String sql = "delete from user where userName = ?";
return update(sql,user.getUserName());
}
//修改记录 ---- 用户名修改
@Override
public int updateUser(User user) {
String sql = "update user set userName = ?,setPassword = ?,surePassword = ?,name=?,sex=?,birthday=? where userName = ?";
return update(sql,user.getUserName(),user.getSetPassword(),user.getSurePassword(),user.getName(),user.getSex(),user.getBrithday(),user.getUserName());
}
//用户名查询
@Override
public User findUserByUserName(Integer userName) {
User user = null;
try{
String sql = "select * from user where userName = ?";
rs = query(sql,userName); //rs(结果集)成员变量 是实现了BaseDao
//遍历结果集 ---- 封装对象
if(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getInt("userName"));
user.setSetPassword(rs.getString("setPassword"));
user.setSurePassword(rs.getString("surePassword"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setBrithday(rs.getString("birthday"));
}
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn, prepstat, rs);
}
return user;
}
//id查询
@Override
public User findUserById(int id) {
User user = null;
try{
String sql = "select * from user where id = ?";
rs = query(sql,id); //rs(结果集)成员变量 是实现了BaseDao
//遍历结果集
if(rs.next()){
user = new User();
user.setId(rs.getInt("id"));
user.setUserName(rs.getInt("userName"));
user.setSetPassword(rs.getString("setPassword"));
user.setSurePassword(rs.getString("surePassword"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setBrithday(rs.getString("birthday"));
}
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn, prepstat, rs);
}
return user;
}
//全查
@Override
public List<User> findAllUser() {
User user = null;
//用一个集合接收结果集
List list = new ArrayList();
try{
String sql = "select * from user";
rs = query(sql);
while (rs.next()){
user = new User();
user.setId(rs.getInt("id"));//小变大--自动装箱
user.setUserName(rs.getInt("userName"));//账号
user.setSetPassword(rs.getString("setPassword"));
user.setSurePassword(rs.getString("surePassword"));
user.setName(rs.getString("name"));
user.setSex(rs.getString("sex"));
user.setBrithday(rs.getString("birthday"));
list.add(user);
}
}catch (SQLException throwables){
throwables.printStackTrace();
}finally {
DaoUtil.closeResource(conn,prepstat,rs);//关闭连接
}
return list;
}
}
service层
service层接口
IBooksService
package com.sfk.service;
import com.sfk.bean.Books;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 17:25
* @Description: com.sfk.service
* @version: 1.0
*/
public interface IBooksService {
//增加
public boolean addBooks(Books books);
//删除
public boolean deleteBooks(String books);
//修改
public boolean modifyBooks(Books books);
//查询
public List<Books> findBooksBySid(String name);
//isbn
public Books findBooksByIsbn(String isbn);
//全查
public List<Books> findAllBooks();
}
IUserService
package com.sfk.service;
import com.sfk.bean.Books;
import com.sfk.bean.User;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 23:07
* @Description: com.sfk.service
* @version: 1.0
*/
public interface IUserService {
//增加
public boolean addUser(User u);
//删除
public boolean deleteUser(User user);
//修改
public boolean updateUser(User user);
public User findUserByUserName(Integer count);
//查询
public User findUserById(int id);
//全查
public List<User> findAllUser();
}
imp
package com.sfk.service.imp;
import com.sfk.bean.Books;
import com.sfk.dao.IBooksDao;
import com.sfk.dao.imp.BooksDaoImp;
import com.sfk.service.IBooksService;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 23:08
* @Description: com.sfk.service.imp
* @version: 1.0
*/
public class BooksServiceImp implements IBooksService {
//添加书
@Override
public boolean addBooks(Books books) {
boolean isok = false;
IBooksDao isd = new BooksDaoImp();
int ret = isd.addBooks(books);
if (ret > 0) {
isok = true;
}
return isok;
}
//删除书
@Override
public boolean deleteBooks(String ibsn) {
boolean isok = false;
IBooksDao isd = new BooksDaoImp();
int ret = isd.deleteBooks(ibsn);
if (ret > 0){
isok = true;
}
return isok;
}
//修改书
@Override
public boolean modifyBooks(Books book) {
boolean isok = false;
IBooksDao isd = new BooksDaoImp();
int i = isd.updateBooks(book);
if (i > 0){
isok = true;
}
return isok;
}
//id查询
@Override
public List<Books> findBooksBySid(String name) {
IBooksDao isd = new BooksDaoImp();
List<Books> books = isd.findBooksBySid(name);
return books;
}
//isbn查询
@Override
public Books findBooksByIsbn(String isbn) {
IBooksDao isd = new BooksDaoImp();
Books books = isd.findBooksByIsbn(isbn);
return books;
}
//全查
@Override
public List<Books> findAllBooks() {
IBooksDao isd = new BooksDaoImp();
List<Books> allBooks = isd.findAllBooks();
return allBooks;
}
}
package com.sfk.service.imp;
import com.sfk.bean.User;
import com.sfk.dao.IBooksDao;
import com.sfk.dao.IUserDao;
import com.sfk.dao.imp.UserDaoImp;
import com.sfk.service.IUserService;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 23:09
* @Description: com.sfk.service.imp
* @version: 1.0
*/
public class UserServiceImp implements IUserService {
//添加用户
@Override
public boolean addUser(User user) {
//判断逻辑处理是否成功------对数据库操作
boolean isok = false;
IUserDao iud = new UserDaoImp();
int ret = iud.addUser(user);
if(ret>0){
isok = true;
}
return isok;
}
//删除用户
@Override
public boolean deleteUser(User user) {
boolean isok = false;
IUserDao iud = new UserDaoImp();
int ret = iud.deleteUser(user);
if(ret>0){
isok = true;
}
return isok;
}
//修改用户
@Override
public boolean updateUser(User user) {
boolean isok = false;
IUserDao iud = new UserDaoImp();
int ret = iud.updateUser(user);
if(ret>0){
isok = true;
}
return isok;
}
@Override
public User findUserById(int id) {
IUserDao iud = new UserDaoImp();
User userById = iud.findUserById(id);
return userById;
}
@Override
public User findUserByUserName(Integer count) {
IUserDao iud = new UserDaoImp();
User user = iud.findUserByUserName(count);
return user;
}
@Override
public List<User> findAllUser() {
IUserDao iud = new UserDaoImp();
List<User> allUser = iud.findAllUser();
return allUser;
}
}
view层
view接口
IBooksView
package com.sfk.view;
import com.sfk.bean.Books;
import java.awt.print.Book;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 23:11
* @Description: com.sfk.view
* @version: 1.0
*/
public interface IBooksView {
//增加
public void showAddBooks(Books book);
//删除
public boolean showDeleteBooks(String ibsn);
//修改
public void showUpdateBooks(Books book);
//书本的isbn查询
public Books showBooksIsbn(String name);
//书本的id查询
public List<Books> showBooksId(String name);
//全查
public List<Books> showFindAllBooks();
}
IUserView
package com.sfk.view;
import com.sfk.bean.User;
import org.junit.Test;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 23:13
* @Description: com.sfk.view
* @version: 1.0
*/
public interface IUserView {
//增加
public void showAddUser(User u);
//删除
public void showDeleteUser(User user);
//修改
public void showUpdateUser(User user);
public User showFindUserByUserName(Integer userName);
//查询
public void showFindUserById();
//全查
public boolean showFindAllUser(String count,String passWord);
}
imp
package com.sfk.view.imp;
import com.sfk.bean.Books;
import com.sfk.service.IBooksService;
import com.sfk.service.imp.BooksServiceImp;
import com.sfk.view.IBooksView;
import org.junit.Test;
import java.awt.print.Book;
import java.util.List;
import java.util.Scanner;
/**
* @Auther: shifk
* @Date: 2024/5/13 - 05 - 13 - 23:13
* @Description: com.sfk.view.imp
* @version: 1.0
*/
public class IBooksViewImp implements IBooksView {
@Test
@Override
public void showAddBooks(Books b) {
System.out.println("添加书");
IBooksService iss = new BooksServiceImp();
//创建书本对象,先写死----------------------------后期改面板获取
//Books book = new Books("2","9787559637949","盗墓笔记","南派三叔","2024/05/31","139","2016/03/5","16-2-5",9,3);
Books book = b;
boolean isAddOk = iss.addBooks(book);
System.out.println("是否添加成功:"+isAddOk);
}
@Override
public boolean showDeleteBooks(String isbn) {
//老规矩----先写死
//1.调用逻辑服务
System.out.println("删除的isbn为"+isbn);
IBooksService ibs = new BooksServiceImp();//接口不能创建,只能通过类创建
//删除操作这里
boolean isDeleteBooks = ibs.deleteBooks(isbn);
System.out.println("是否删除成功:"+isDeleteBooks);
return isDeleteBooks;
}
//修改---更新
@Override
public void showUpdateBooks(Books book) {
//老规矩 --- 把id写死
System.out.println("修改的书:"+book.getName());
//1.调用逻辑对象
IBooksService ibs = new BooksServiceImp();//接口不能创建,只能通过类创建
boolean b = ibs.modifyBooks(book);
//3.进行修改
System.out.println("修改是否成功:"+b);
}
@Test
//展示查询
@Override
public List<Books> showBooksId(String name) {
//Scanner scan = new Scanner(System.in);
System.out.println("书名:");
String Name = name;
IBooksService iss = new BooksServiceImp();
List<Books> info = iss.findBooksBySid(Name);
System.out.println(info);
return info;
}
public Books showBooksIsbn(String isbn) {
String ISBN = isbn;
System.out.println("isbn:"+ISBN);
IBooksService iss = new BooksServiceImp();
Books info = iss.findBooksByIsbn(ISBN);
System.out.println(info);
return info;
}
@Test
@Override
public List<Books> showFindAllBooks() {
//查询所有的书
IBooksService ibs = new BooksServiceImp();
List<Books> allBooks = ibs.findAllBooks();
for (Books b:allBooks){
System.out.println(b);
}
return allBooks;
}
}
package com.sfk.view.imp;
import com.sfk.bean.User;
import com.sfk.service.IUserService;
import com.sfk.service.imp.UserServiceImp;
import com.sfk.view.IUserView;
import org.junit.Test;
import java.util.List;
public class IUserViewImp implements IUserView{
@Test
//增加
@Override
public void showAddUser(User u) {
System.out.println("添加用户");
IUserService ius = new UserServiceImp();
//创建书本对象,先写死----------------------------后期改面板获取
User user = u;
boolean isAddOk = ius.addUser(user);
System.out.println("注册:"+isAddOk);
}
@Test
//删除
@Override
public void showDeleteUser(User user) {
//老规矩----先写死
//1.调用逻辑服务
System.out.println("注销账号"+user.getUserName());
IUserService ius = new UserServiceImp();//接口不能创建,只能通过类创建
//删除操作这里
boolean isDeleteUser = ius.deleteUser(user);
System.out.println("是否删除成功:"+isDeleteUser);
}
@Test
//修改---更新
@Override
public void showUpdateUser(User user) {
//先写死
System.out.println("修改的用户名"+user.getUserName());
//1.调用逻辑对象
IUserService ius = new UserServiceImp();
boolean b = ius.updateUser(user);
System.out.println("修改是否成功:"+b);
}
@Test
//id查询
@Override
public void showFindUserById() {
System.out.println("用户的ID:");
int i=1;
IUserService ius = new UserServiceImp();
User info = ius.findUserById(i);
System.out.println(info);
}
@Override
//账号查 ---- Integer userName
public User showFindUserByUserName(Integer userName) {
System.out.println("UserName:"+userName);
IUserService ius = new UserServiceImp();
User user = ius.findUserByUserName(userName);
return user;
}
@Test
//全查
@Override
public boolean showFindAllUser(String count,String passWord) {
IUserService ius = new UserServiceImp();
List<User> allUser = ius.findAllUser();
boolean flag = false;
for(User u:allUser){
//用户名---学号
String s = String.valueOf(u.getUserName());
boolean isCountOk = s.equals(count);
//密码
String pd = String.valueOf(u.getSurePassword());
boolean isPasswordOk = pd.equals(passWord);
if(isCountOk && isPasswordOk){
System.out.println("登陆成功!");
flag = true;
}
}
return flag;
}
}
window
package com.sfk.window;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
public class AppStart {
public static void main(String[] args) throws IOException {
LoginFrame lg = new LoginFrame();
lg.start();
}
}
package com.sfk.window;
import com.sfk.bean.Books;
import com.sfk.view.IBooksView;
import com.sfk.view.IUserView;
import com.sfk.view.imp.IBooksViewImp;
import com.sfk.view.imp.IUserViewImp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BookAddForm {
private JFrame frame;
private JPanel panel;
//定义获取的变量----对应上字段
private static String ISBN;
private static String AUTHOR;
private static String PRICE;
private static String DEPOSIT;
private static String NAME;
private static String PUBLISHER;
private static String PUDATE;
private static int QUANTIFY;
//输入的字段框
static Books book = null;
static JTextField ibsn = new JTextField();
static JTextField author = new JTextField();
static JTextField price = new JTextField();
static JTextField deposit = new JTextField();
static JTextField name = new JTextField();
static JTextField publisher = new JTextField();
static JTextField puDate = new JTextField();
static JTextField quantify = new JTextField();
public BookAddForm() {
// 创建并设置JFrame
frame = new JFrame("录入图书");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
// 创建面板以容纳表单组件
//panel = new JPanel(new GridLayout(5, 2, 10, 10)); // 使用GridLayout布局
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.pink);
//返回按钮
JButton goback = new JButton("<---");
goback.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
goback.setBounds(50, 40, 50, 40);
panel.add(goback);
//标题
// 获取默认的字体,并基于它创建一个新的字体,大小增加50%
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("录入图书");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(325, 20, 100, 30);
panel.add(title);
// 创建并添加标签和文本字段
JLabel jLabel = new JLabel("书 号:");
jLabel.setBounds(50, 100, 50, 30);
ibsn.setBounds(100, 100, 200, 30);
panel.add(jLabel);
panel.add(ibsn);
JLabel jLabe2 = new JLabel("书 名:");
jLabe2.setBounds(50, 150, 50, 30);
author.setBounds(100, 150, 200, 30);
panel.add(jLabe2);
panel.add(author);
JLabel jLabe3 = new JLabel("作 者:");
jLabe3.setBounds(50, 200, 50, 30);
price.setBounds(100, 200, 200, 30);
panel.add(jLabe3);
panel.add(price);
JLabel jLabe4 = new JLabel("存放位置");
jLabe4.setBounds(50, 250, 70, 30);
deposit.setBounds(100, 250, 200, 30);
panel.add(jLabe4);
panel.add(deposit);
JLabel jLabe5 = new JLabel("价 格:");
jLabe5.setBounds(400, 100, 50, 30);
name.setBounds(450, 100, 200, 30);
panel.add(jLabe5);
panel.add(name);
JLabel jLabe6 = new JLabel("出版社:");
jLabe6.setBounds(400, 150, 50, 30);
publisher.setBounds(450, 150, 200, 30);
panel.add(jLabe6);
panel.add(publisher);
JLabel jLabe7 = new JLabel("出版时间");
jLabe7.setBounds(400, 200, 70, 30);
puDate.setBounds(450, 200, 200, 30);
panel.add(jLabe7);
panel.add(puDate);
JLabel jLabe8 = new JLabel("数 量:");
jLabe8.setBounds(400, 250, 50, 30);
quantify.setBounds(450, 250, 200, 30);
panel.add(jLabe8);
panel.add(quantify);
// 创建并提交和取消按钮
JButton submitButton = new JButton("提交");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isOk = submit();
if(isOk){
//成功
IBooksView ius = new IBooksViewImp();
ius.showAddBooks(book);
JOptionPane.showMessageDialog(null, "添加"+isOk+"!", "提示", JOptionPane.INFORMATION_MESSAGE);
}else {
//不成功
JOptionPane.showMessageDialog(null, "输入有误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
});
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
submitButton.setBounds(450, 350, 70, 40);
cancelButton.setBounds(230, 350, 70, 40);
panel.add(submitButton);
panel.add(cancelButton);
frame.add(panel);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
//frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗体关闭时就关闭
// 使窗体可见
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static boolean submit(){
try {
//输入不能为空
ISBN = ibsn.getText();
AUTHOR = author.getText();
PRICE = price.getText();
DEPOSIT = deposit.getText();
NAME = name.getText();
PUBLISHER = publisher.getText();
PUDATE = puDate.getText();
QUANTIFY = Integer.valueOf(quantify.getText());//这可能报错
book = new Books(ISBN,AUTHOR,PRICE,DEPOSIT,NAME,PUBLISHER,PUDATE,QUANTIFY);
//不能为空
if(ISBN.equals("")||AUTHOR.equals("")||PRICE.equals("")||DEPOSIT.equals("")||NAME.equals("")||PUBLISHER.equals("")||PUDATE.equals("")){
return false;
}else {
return true;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "数量为整型!", "提示", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
}
package com.sfk.window;
import com.sfk.bean.Books;
import com.sfk.view.IBooksView;
import com.sfk.view.imp.IBooksViewImp;
import org.junit.Test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class DeleForm extends JFrame{
private JFrame frame;
private JPanel panel = new JPanel();
private JPanel PANEL2 = new JPanel();//输出框
//把按钮和输入框提出来
static JLabel t = new JLabel("| id | isbn | 书 名 | 作 者 | 出 版 社 | 价 格 | 出 版 时 间 | 存 放 位 置 | 数 量 | 借 出 |");
static Font font = new Font("微软雅黑",Font.PLAIN,20);
static Font f2 = new Font("微软雅黑",Font.PLAIN,19);
public DeleForm(){
PANEL2.setLayout(new FlowLayout());
t.setFont(font);
t.setBounds(50,180,900,80);
PANEL2.setBounds(50,250,900,600);
panel.add(t);//标题栏
// 创建并设置JFrame
frame = new JFrame("查询图书");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 900); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
panel.setLayout(null);
panel.setBackground(Color.pink);
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("删除图书");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(450, 20, 100, 40);
panel.add(title);
//书名标签+输入框+查询按钮
JLabel isbn = new JLabel("ISBN 书号");
JTextField deleterText = new JTextField();
JButton delBtn = new JButton("删 除");
delBtn.setBounds(350,100,100,40);
isbn.setBounds(50,100,200,40);
deleterText.setBounds(120,100,200,40);
//全查
JLabel allB = new JLabel("点击左侧全查按钮即可查询所有书籍");//标签
allB.setBounds(50,150,400,40);
JButton allQ = new JButton("全 查");//全查按钮
allQ.setBounds(350,150,100,40);
allQ.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IBooksView iv = new IBooksViewImp();
java.util.List<Books> books = iv.showFindAllBooks();
//遍历结果集----生成标签
int y = 250;
for (Books b:books
) {
JLabel data = new JLabel("\t|\t"+b.getId()+"\t|\t"+b.getIsbn()+"\t|\t"+
b.getName()+"\t|\t"+b.getAuthor()+"\t|\t"+b.getPublisher()+"\t|\t"+b.getPrice()+
"\t|\t"+b.getPuDate()+"\t|\t"+b.getDeposit()+"\t|\t"+b.getQuantify()+"\t|\t"+b.getLend()+"\t|\t");
data.setFont(f2);
data.setBounds(0,y,900,80);
y += 80; // 递增y坐标以避免重叠
PANEL2.add(data);
PANEL2.revalidate();
PANEL2.repaint();
}
}
});
panel.add(allQ);
panel.add(allB);
//清屏
JButton clearBtn = new JButton("清 屏");
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PANEL2.removeAll();
PANEL2.revalidate();
PANEL2.repaint();
}
});
clearBtn.setBounds(800,100,100,40);
frame.add(clearBtn);
//查询---书名
delBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IBooksView ib = new IBooksViewImp();
String isbn = deleterText.getText();
boolean isOk = ib.showDeleteBooks(isbn);
int y = 250; // 初始y坐标
JLabel result = new JLabel("isbn为:"+isbn+"删除:"+isOk);
result.setFont(f2);
result.setBounds(0,y,900,80);
PANEL2.add(result);
PANEL2.revalidate();
PANEL2.repaint();
}
});
//全查----所有
//添加事件获取查询到的信息放到面板里
panel.add(PANEL2);
panel.add(delBtn);
panel.add(isbn);
panel.add(deleterText);
//返回按钮
JButton goback = new JButton("返回");
goback.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
goback.setBounds(50, 40, 70, 40);
panel.add(goback);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
}
}
package com.sfk.window;
import com.sfk.view.IUserView;
import com.sfk.view.imp.IUserViewImp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class LoginFrame extends JPanel {
private JLabel accountLabel;
private JLabel passwordLabel;
private JTextField accountField;
private JPasswordField passwordField;
private JButton loginButton;
private JButton cancelButton;
private static JFrame frame = new JFrame("登录页面");
public LoginFrame() throws IOException {
setLayout(null); // 使用null布局管理器
// 账号标签
accountLabel = new JLabel("账号");
accountLabel.setBounds(50, 80, 50, 30);
add(accountLabel);
// 账号输入框
accountField = new JTextField(10);
accountField.setBounds(100, 80, 200, 30);
add(accountField);
// 密码标签
passwordLabel = new JLabel("密码");
passwordLabel.setBounds(50, 130, 50, 30);
add(passwordLabel);
// 密码输入框
passwordField = new JPasswordField(10);
passwordField.setBounds(100, 130, 200, 30);
add(passwordField);
// 登录按钮
loginButton = new JButton("登录");
loginButton.setBounds(100, 200, 80, 30);
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 在这里处理登录逻辑
String count = accountField.getText();
String passWord = new String(passwordField.getPassword());
//逻辑判断
//1.从数据库遍历出来的用户对比账户---是否存在
IUserView user = new IUserViewImp();
boolean flag = user.showFindAllUser(count,passWord);
System.out.println("登录: " +count+ ", 密码: " +passWord);
if(flag){//登录成功---把窗口关掉,开启图书的增删改查窗口
//dispose(); // 关闭当前窗体
//打开新的窗体
new Menu(count,passWord);
frame.dispose();
}else {
System.out.println("用户名或密码错误!");
}
}
});
add(loginButton);
// 取消按钮
cancelButton = new JButton("注册");
cancelButton.setBounds(200, 200, 80, 30);
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 在这里处理取消逻辑
new RegisterForm();
}
});
add(cancelButton);
// 设置背景颜色(你可以替换为图片)
setBackground(Color.GRAY);
}
//启动 生成窗体
public static void start() throws IOException {
// 创建并设置JFrame
frame.setSize(400, 300); // 设置窗口大小
JPanel login = new LoginFrame();
frame.add(login); // 添加登录面板
frame.setVisible(true); // 显示窗口
//设置不可变
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗体关闭时就关闭
}
}
package com.sfk.window;
import com.sfk.bean.User;
import com.sfk.view.IBooksView;
import com.sfk.view.IUserView;
import com.sfk.view.imp.IBooksViewImp;
import com.sfk.view.imp.IUserViewImp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: shifk
* @Date: 2024/5/31 - 05 - 31 - 21:49
* @Description: com.sfk.window
* @version: 1.0
*/
public class Menu extends JFrame{
private JFrame frame;
private JPanel panel;
private Integer COUNT;
private String PASSWORD;
public Menu(String count,String passWord) {
//先记录下登录的账号
this.COUNT = Integer.valueOf(count);
this.PASSWORD = passWord;
this.setFocusable(true);
// 创建并设置JFrame
frame = new JFrame("录入图书");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
// 创建面板以容纳表单组件
//panel = new JPanel(new GridLayout(5, 2, 10, 10)); // 使用GridLayout布局
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.pink);
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("主页菜单");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(325, 20, 100, 30);
panel.add(title);
//主界面包括添加书籍、删除书籍、修改书籍、个人信息、查看书籍5个按钮。
// 创建并提交和取消按钮
JButton addBtn = new JButton("添加书籍");
addBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
BookAddForm bookAddForm = new BookAddForm();
}
});
frame.getContentPane().add(addBtn);
JButton delBtn = new JButton("删除书籍");
delBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DeleForm df = new DeleForm();
}
});
JButton modify = new JButton("修改书籍");
modify.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ModifyFrom();
}
});
JButton select = new JButton("查看书籍");
select.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new QueryFrom();
}
});
JButton person = new JButton("个人信息");
person.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//先查出这个对象,传给个人表单
IUserView iuv = new IUserViewImp();
User user = iuv.showFindUserByUserName(COUNT);
new PersonFrom(user);
}
});
JButton quit = new JButton("退出登录");
quit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
System.out.println("退出成功!");
}
});
addBtn.setBounds(100, 100, 200, 40);
delBtn.setBounds(100, 150, 200, 40);
modify.setBounds(100, 200, 200, 40);
select.setBounds(450, 100, 200, 40);
person.setBounds(450, 150, 200, 40);
quit.setBounds(450, 200, 200, 40);
panel.add(addBtn);
panel.add(delBtn);
panel.add(modify);
panel.add(select);
panel.add(person);
panel.add(quit);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
frame.add(panel);
frame.setResizable(false);
// 使窗体可见
frame.setVisible(true);
}
}
package com.sfk.window;
import com.sfk.bean.Books;
import com.sfk.bean.User;
import com.sfk.view.IBooksView;
import com.sfk.view.IUserView;
import com.sfk.view.imp.IBooksViewImp;
import com.sfk.view.imp.IUserViewImp;
import javax.lang.model.element.VariableElement;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/6/1 - 06 - 01 - 20:14
* @Description: com.sfk.window
* @version: 1.0
*/
public class ModifyFrom {
private JFrame frame;
private JPanel panel;
//定义获取的变量----对应上字段
private static String ISBN;
private static String AUTHOR;
private static String PRICE;
private static String DEPOSIT;
private static String NAME;
private static String PUBLISHER;
private static String PUDATE;
private static int QUANTIFY;
static JTextField isbnText = new JTextField();//查询的isbn输入框
//输入的字段框
static Books book = null;
static JTextField ibf = new JTextField();
static JTextField author = new JTextField();
static JTextField price = new JTextField();
static JTextField deposit = new JTextField();
static JTextField name = new JTextField();
static JTextField publisher = new JTextField();
static JTextField puDate = new JTextField();
static JTextField quantify = new JTextField();
public ModifyFrom() {
// 创建并设置JFrame
frame = new JFrame("修改图书");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
// 创建面板以容纳表单组件
//panel = new JPanel(new GridLayout(5, 2, 10, 10)); // 使用GridLayout布局
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.pink);
//查询出来后修改-------先查询
JLabel isbn = new JLabel("ISBN 书号");
JButton queryBtn = new JButton("查 询");
queryBtn.setBounds(350,60,100,40);
isbn.setBounds(50,60,200,40);
isbnText.setBounds(110,60,200,40);
panel.add(isbn);
panel.add(isbnText);
panel.add(queryBtn);
//给查询按钮添加事件
//查询---ISBN
queryBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//1.获取输入框的东西
String text = isbnText.getText();
//2.把isbn查询出来
IBooksView ibv = new IBooksViewImp();
//3.查询出来的赋值---呈现出来
book = ibv.showBooksIsbn(text);
//注:id不能丢----重新赋值
book.setId(book.getId());
//查到只能是一条数据,因为ISBN唯一的
ibf.setText(book.getIsbn());
author.setText(book.getAuthor());
price.setText(book.getPrice());
deposit.setText(book.getDeposit());
name.setText(book.getName());
publisher.setText(book.getPublisher());
puDate.setText(book.getPuDate());
quantify.setText(String.valueOf(book.getQuantify()));
}
});
//标题
// 获取默认的字体,并基于它创建一个新的字体,大小增加50%
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("先查后改");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(325, 20, 100, 30);
panel.add(title);
// 创建并添加标签和文本字段
JLabel jLabel = new JLabel("书 号:");
jLabel.setBounds(50, 120, 50, 30);
ibf.setBounds(110, 120, 200, 30);
panel.add(jLabel);
panel.add(ibf);
JLabel jLabe2 = new JLabel("书 名:");
jLabe2.setBounds(50, 170, 50, 30);
name.setBounds(110, 170, 200, 30);
panel.add(jLabe2);
panel.add(name);
JLabel jLabe3 = new JLabel("作 者:");
jLabe3.setBounds(50, 220, 50, 30);
author.setBounds(110, 220, 200, 30);
panel.add(jLabe3);
panel.add(author);
JLabel jLabe4 = new JLabel("存放位置");
jLabe4.setBounds(50, 270, 70, 30);
deposit.setBounds(110, 270, 200, 30);
panel.add(jLabe4);
panel.add(deposit);
JLabel jLabe5 = new JLabel("价 格:");
jLabe5.setBounds(400, 120, 50, 30);
price.setBounds(460, 120, 200, 30);
panel.add(jLabe5);
panel.add(price);
JLabel jLabe6 = new JLabel("出版社:");
jLabe6.setBounds(400, 170, 50, 30);
publisher.setBounds(460, 170, 200, 30);
panel.add(jLabe6);
panel.add(publisher);
JLabel jLabe7 = new JLabel("出版时间");
jLabe7.setBounds(400, 220, 70, 30);
puDate.setBounds(460, 220, 200, 30);
panel.add(jLabe7);
panel.add(puDate);
JLabel jLabe8 = new JLabel("数 量:");
jLabe8.setBounds(400, 270, 50, 30);
quantify.setBounds(460, 270, 200, 30);
panel.add(jLabe8);
panel.add(quantify);
// 创建并提交和取消按钮
JButton submitButton = new JButton("保存修改");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isOk = saveSubmit();
if(isOk){
//成功
IBooksView ius = new IBooksViewImp();
ius.showUpdateBooks(book);
System.out.println(book);
JOptionPane.showMessageDialog(null, "修改:"+isOk+"!", "提示", JOptionPane.INFORMATION_MESSAGE);
}else {
//不成功
JOptionPane.showMessageDialog(null, "输入有误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
});
JButton cancelButton = new JButton("取 消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
submitButton.setBounds(450, 370, 100, 40);
cancelButton.setBounds(230, 370, 100, 40);
panel.add(submitButton);
panel.add(cancelButton);
frame.add(panel);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
//frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗体关闭时就关闭
// 使窗体可见
frame.setVisible(true);
}
public static boolean saveSubmit(){
try {
//输入不能为空
ISBN = ibf.getText();
AUTHOR = author.getText();
PRICE = price.getText();
DEPOSIT = deposit.getText();
NAME = name.getText();
PUBLISHER = publisher.getText();
PUDATE = puDate.getText();
QUANTIFY = Integer.valueOf(quantify.getText());//这可能报错
book = new Books(book.getId(),ISBN,NAME,AUTHOR,PUBLISHER,PRICE,PUDATE,DEPOSIT,QUANTIFY,book.getLend());
//不能为空
if(ISBN.equals("")||AUTHOR.equals("")||PRICE.equals("")||DEPOSIT.equals("")||NAME.equals("")||PUBLISHER.equals("")||PUDATE.equals("")){
return false;
}else {
return true;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "数量为整型!", "提示", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
}
package com.sfk.window;
import com.sfk.bean.User;
import com.sfk.view.IUserView;
import com.sfk.view.imp.IUserViewImp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: shifk
* @Date: 2024/6/1 - 06 - 01 - 20:16
* @Description: com.sfk.window
* @version: 1.0
*
* 一但打开这个窗口就是之前登录的对象
*/
public class PersonFrom {
private JFrame frame;
private JPanel panel;
//定义获取的变量----对应上字段
private static int ID;
private static Integer USERNAME;
private static String SETPASSWORD;
private static String SUREPASSWORD;
private static String NAME;
private static String SEX;
private static String BIRTHDAY;
//输入的字段框
static User USER = null;
static JTextField userName = new JTextField();
static JTextField setPassword = new JTextField();
static JTextField surePassword = new JTextField();
static JTextField name = new JTextField();
static JTextField sex = new JTextField();
static JTextField birthday = new JTextField();
public PersonFrom(User user) {
//一旦被创建就获取对象
showMessage(user);
// 创建并设置JFrame
frame = new JFrame("个人信息");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 500); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
// 创建面板以容纳表单组件
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.pink);
//标题
// 获取默认的字体,并基于它创建一个新的字体,大小增加50%
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("编辑个人信息");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(300, 20, 200, 30);
panel.add(title);
// 创建并添加标签和文本字段
JLabel jLabel = new JLabel("用 户 名");
jLabel.setBounds(50, 120, 50, 30);
userName.setBounds(110, 120, 200, 30);
panel.add(jLabel);
panel.add(userName);
JLabel jLabe2 = new JLabel("密 码");
jLabe2.setBounds(50, 170, 50, 30);
setPassword.setBounds(110, 170, 200, 30);
panel.add(jLabe2);
panel.add(setPassword);
JLabel jLabe3 = new JLabel("确认密码");
jLabe3.setBounds(50, 220, 70, 30);
surePassword.setBounds(110, 220, 200, 30);
panel.add(jLabe3);
panel.add(surePassword);
JLabel jLabe5 = new JLabel("姓 名");
jLabe5.setBounds(400, 120, 50, 30);
name.setBounds(460, 120, 200, 30);
panel.add(jLabe5);
panel.add(name);
JLabel jLabe6 = new JLabel("性 别");
jLabe6.setBounds(400, 170, 50, 30);
sex.setBounds(460, 170, 200, 30);
panel.add(jLabe6);
panel.add(sex);
JLabel jLabe7 = new JLabel("生 日");
jLabe7.setBounds(400, 220, 70, 30);
birthday.setBounds(460, 220, 200, 30);
panel.add(jLabe7);
panel.add(birthday);
// 创建并提交和取消按钮
JButton submitButton = new JButton("保存编辑");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isOk = saveSubmit();
if(isOk){
//成功
IUserView iuv = new IUserViewImp();
iuv.showUpdateUser(USER);
System.out.println(USER);
JOptionPane.showMessageDialog(null, "修改:"+isOk+"!", "提示", JOptionPane.INFORMATION_MESSAGE);
}else {
//不成功
JOptionPane.showMessageDialog(null, "输入有误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
});
JButton cancelButton = new JButton("取 消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
//注销按钮
JButton logout = new JButton("注销账号");
logout.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
boolean isOk = saveSubmit();
IUserView iuv = new IUserViewImp();
iuv.showDeleteUser(USER);
System.out.println("注销用户:"+USER+"是否成功:"+isOk);
System.exit(0);
}
});
submitButton.setBounds(450, 300, 100, 40);
cancelButton.setBounds(220, 300, 100, 40);
logout.setBounds(460, 50, 100, 30);
panel.add(submitButton);
panel.add(cancelButton);
panel.add(logout);
frame.add(panel);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
frame.setVisible(true);
}
public static boolean saveSubmit(){
try {
//封装对象----提交----二次 确认才能进行修改
USERNAME = Integer.valueOf(userName.getText());
SETPASSWORD = setPassword.getText();
SUREPASSWORD = surePassword.getText();
NAME = name.getText();
SEX = sex.getText();
BIRTHDAY = birthday.getText();
USER = new User(ID, USERNAME, SETPASSWORD, SUREPASSWORD, NAME, SEX, BIRTHDAY);
//不能为空
if(USERNAME<=0||SETPASSWORD.equals("")||SUREPASSWORD.equals("")||NAME.equals("")||SEX.equals("")||BIRTHDAY.equals("")){
return false;
}else {
if (SETPASSWORD.equals(SUREPASSWORD)){
return true;
}else{
return false;
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "数量为整型!", "提示", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
/**
* 当用户登录后,对象会传到个人表里被展示-----最先
* @param user
*/
public static void showMessage(User user){
ID = user.getId();//系统给的不能动
userName.setText(String.valueOf(user.getUserName()));
setPassword.setText(user.getSetPassword());
surePassword.setText(user.getSurePassword());
name.setText(user.getName());
sex.setText(user.getSex());
birthday.setText(user.getBrithday());
}
}
package com.sfk.window;
import com.sfk.bean.Books;
import com.sfk.view.IBooksView;
import com.sfk.view.imp.IBooksViewImp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
/**
* @Auther: shifk
* @Date: 2024/6/1 - 06 - 01 - 20:16
* @Description: com.sfk.window
* @version: 1.0
*/
public class QueryFrom {
private JFrame frame;
private JPanel panel = new JPanel();
private JPanel PANEL2 = new JPanel();//输出框
//把按钮和输入框提出来
static JLabel t = new JLabel("| id | isbn | 书 名 | 作 者 | 出 版 社 | 价 格 | 出 版 时 间 | 存 放 位 置 | 数 量 | 借 出 |");
static Font font = new Font("微软雅黑",Font.PLAIN,20);
static Font f2 = new Font("微软雅黑",Font.PLAIN,19);
public QueryFrom(){
PANEL2.setLayout(new FlowLayout());
t.setFont(font);
t.setBounds(50,180,900,80);
PANEL2.setBounds(50,250,900,600);
panel.add(t);//标题栏
// 创建并设置JFrame
frame = new JFrame("查询图书");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000, 900); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
panel.setLayout(null);
panel.setBackground(Color.pink);
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("查询图书");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(450, 20, 100, 40);
panel.add(title);
//书名标签+输入框+查询按钮
JLabel name = new JLabel("书 名");
JTextField nameText = new JTextField();
JButton queryBtn = new JButton("查 询");
queryBtn.setBounds(350,100,100,40);
name.setBounds(50,100,200,40);
nameText.setBounds(100,100,200,40);
//全查
JLabel allB = new JLabel("点击左侧全查按钮即可查询所有书籍");
allB.setBounds(50,150,400,40);
JButton allQ = new JButton("全 查");
allQ.setBounds(350,150,100,40);
allQ.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IBooksView iv = new IBooksViewImp();
List<Books> books = iv.showFindAllBooks();
//遍历结果集----生成标签
int y = 250;
for (Books b:books
) {
JLabel data = new JLabel("\t|\t"+b.getId()+"\t|\t"+b.getIsbn()+"\t|\t"+
b.getName()+"\t|\t"+b.getAuthor()+"\t|\t"+b.getPublisher()+"\t|\t"+b.getPrice()+
"\t|\t"+b.getPuDate()+"\t|\t"+b.getDeposit()+"\t|\t"+b.getQuantify()+"\t|\t"+b.getLend()+"\t|\t");
data.setFont(f2);
data.setBounds(0,y,900,80);
y += 80; // 递增y坐标以避免重叠
PANEL2.add(data);
PANEL2.revalidate();
PANEL2.repaint();
}
}
});
panel.add(allQ);
panel.add(allB);
//清屏
JButton clearBtn = new JButton("清 屏");
clearBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
PANEL2.removeAll();
PANEL2.revalidate();
PANEL2.repaint();
}
});
clearBtn.setBounds(800,100,100,40);
frame.add(clearBtn);
//查询---书名
queryBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IBooksView ib = new IBooksViewImp();
String name = nameText.getText();
List<Books> books = ib.showBooksId(name);
int y = 250; // 初始y坐标
for(Books b:books){
JLabel data = new JLabel("\t|\t"+b.getId()+"\t|\t"+b.getIsbn()+"\t|\t"+
b.getName()+"\t|\t"+b.getAuthor()+"\t|\t"+b.getPublisher()+"\t|\t"+b.getPrice()+
"\t|\t"+b.getPuDate()+"\t|\t"+b.getDeposit()+"\t|\t"+b.getQuantify()+"\t|\t"+b.getLend()+"\t|\t");
data.setFont(f2);
data.setBounds(0,y,900,80);
y += 80; // 递增y坐标以避免重叠
PANEL2.add(data);
PANEL2.revalidate();
PANEL2.repaint();
}
}
});
//全查----所有
//添加事件获取查询到的信息放到面板里
panel.add(PANEL2);
panel.add(queryBtn);
panel.add(name);
panel.add(nameText);
//返回按钮
JButton goback = new JButton("返回");
goback.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
goback.setBounds(50, 40, 70, 40);
panel.add(goback);
frame.add(panel);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
}
}
package com.sfk.window;
import com.sfk.bean.User;
import com.sfk.service.IUserService;
import com.sfk.service.imp.UserServiceImp;
import com.sfk.view.IUserView;
import com.sfk.view.imp.IUserViewImp;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* @Auther: shifk
* @Date: 2024/5/30 - 05 - 30 - 9:58
* @Description: com.sfk.window
* @version: 1.0
*/
public class RegisterForm extends JFrame {
//通过表单获取属性
//学号、密码、姓名、性别、出生日期
//private int id;//系统id自动给
private static Integer userName;//用户名
private static String setPassword;//密码
private static String surePassword;//确认密码
private static String name;//姓名
private static String sex;//性别
private static String birthday;//生日
//表单框
static JTextField UName = new JTextField();
static JTextField setPassWord = new JTextField();
static JTextField sureSetPassWord = new JTextField();
static JTextField Name = new JTextField();
static JTextField Sex = new JTextField();
static JTextField Birthday = new JTextField();
static User user = null;
//窗体表单
private JPanel panel;
private static JFrame frame = new JFrame("注册页面");
//构造方法
public RegisterForm(){
// 创建并设置JFrame
frame = new JFrame("填写注册信息");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 400); // 根据需要调整大小
frame.setLayout(new BorderLayout()); // 使用BorderLayout
// 创建面板以容纳表单组件
panel = new JPanel();
panel.setLayout(null);
panel.setBackground(Color.pink);
//返回按钮
JButton goback = new JButton("<---");
goback.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
goback.setBounds(50, 40, 70, 40);
panel.add(goback);
//标题
// 获取默认的字体,并基于它创建一个新的字体,大小增加50%
Font defaultFont = UIManager.getFont("Label.font");
float newSize = defaultFont.getSize2D() * 2.0f; // 增大50%
Font newFont = defaultFont.deriveFont(newSize);
//蓝色标题
JLabel title = new JLabel("填写注册信息");
title.setFont(newFont);
title.setForeground(Color.blue);
title.setBounds(325, 20, 150, 30);
panel.add(title);
// 创建并添加标签和文本字段
JLabel jLabel = new JLabel("用 户 名:");
jLabel.setBounds(50, 100, 80, 30);
UName.setBounds(120, 100, 200, 30);
panel.add(jLabel);
panel.add(UName);
JLabel jLabe2 = new JLabel("设置密码:");
jLabe2.setBounds(50, 150, 80, 30);
setPassWord.setBounds(120, 150, 200, 30);
panel.add(jLabe2);
panel.add(setPassWord);
JLabel jLabe3 = new JLabel("确认密码:");
jLabe3.setBounds(50, 200, 80, 30);
sureSetPassWord.setBounds(120, 200, 200, 30);
panel.add(jLabe3);
panel.add(sureSetPassWord);
JLabel jLabe4 = new JLabel("姓 名");
jLabe4.setBounds(400, 100, 80, 30);
Name.setBounds(450, 100, 200, 30);
panel.add(jLabe4);
panel.add(Name);
JLabel jLabe5 = new JLabel("性 别:");
jLabe5.setBounds(400, 150, 80, 30);
Sex.setBounds(450, 150, 200, 30);
panel.add(jLabe5);
panel.add(Sex);
JLabel jLabe6 = new JLabel("生 日");
jLabe6.setBounds(400, 200, 80, 30);
Birthday.setBounds(450, 200, 200, 30);
panel.add(jLabe6);
panel.add(Birthday);
// 创建并提交和取消按钮
JButton submitButton = new JButton("确认");
submitButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//1.表单是否为空----空--》弹窗 /成功---》成功
boolean isOk = submit();
if(isOk){
//成功
IUserView ius = new IUserViewImp();
ius.showAddUser(user);
JOptionPane.showMessageDialog(null, "注册"+isOk+"!", "提示", JOptionPane.INFORMATION_MESSAGE);
}else {
//不成功
JOptionPane.showMessageDialog(null, "输入有误!", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
});
JButton cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.dispose();
}
});
submitButton.setBounds(510, 250, 70, 40);
cancelButton.setBounds(180, 250, 70, 40);
panel.add(submitButton);
panel.add(cancelButton);
frame.add(panel);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭仅关闭自身
// 使窗体可见
frame.setVisible(true);
}
public static boolean submit(){
//boolean success = false;//先为失败,操作成功后再改为成功
//调用成员变量看是否为空
try {
userName = Integer.valueOf(UName.getText());
setPassword = setPassWord.getText();
surePassword = sureSetPassWord.getText();
name = Name.getText();
sex = Sex.getText();
birthday = Birthday.getText();
user = new User(userName,setPassword,surePassword,name,sex,birthday);
//输入的字符为""但不是null所以这样比较
if(setPassword.equals("")||surePassword.equals("")||name.equals("")||sex.equals("")||birthday.equals("")){
return false;//都不能为空
}else{
if(setPassword.equals(surePassword)){
return true;//二次确认
}else {
return false;
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "用户名为整型!", "提示", JOptionPane.INFORMATION_MESSAGE);
return false;
}
}
}
运行的结果图
紧展示部分图片,所以功能都可以使用,大家还是自己上手来弄,这不再演示。跟着我的节奏绝对没问题的。
结束的废话
到这就结束啦,相信你做出来了,这是练手的小作业,主要是分享一下,三层架构,和一些基本知识点,第一次写文章勿喷。