窗口类:
package com.bookstore.views;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.bookstore.tools.GUTools;
/**
* 主窗口类
*/
@SuppressWarnings("serial")
public abstract class BookMainFrame extends JFrame {
//组件
private JLabel titleLabel1 = new JLabel(new ImageIcon("bookstore.jpg"));//标题图片
private JButton btn = new JButton("进入系统");//顾客按钮
//构造函数
public BookMainFrame() {
this.init();// 初始化操作
this.addComponent();// 添加组件
this.addListener();// 添加监听器
}
//初始化操作
private void init() {
this.setTitle("新华书店欢迎您!");// 标题
this.setSize(600, 400);// 窗体大小与位置
GUTools.center(this);//设置窗口在屏幕上的位置
GUTools.setTitleImage(this, "bookstore2.jpg");
this.setResizable(false);// 窗体大小固定
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗口默认操作
}
//添加组件
private void addComponent() {
//窗体使用默认的边界布局,北区放入图片
this.add(this.titleLabel1 , BorderLayout.NORTH);
//创建JPanel对象
JPanel btnPanel = new JPanel();
//清除布局,使JPanel中的组件可以自定义位置
btnPanel.setLayout(null);
//将JPanel对象添加到窗体中
this.add(btnPanel);
//定义边界位置
btn.setBounds(240, 20, 120, 50);
//将按钮添加到JPanel对象中
btnPanel.add(btn);
}
//添加监听器
private void addListener() {
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
showRootDialog();
}
});
}
//展示管理员界面方法
public abstract void showRootDialog();
}
package com.bookstore.views;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import com.bookstore.tools.GUTools;
/**
* 管理窗口类
*/
@SuppressWarnings("serial")
public abstract class RootDialog extends JDialog{
//定义界面使用到的组件作为成员变量
private JLabel tableLabel = new JLabel("图书列表");//列表标题
private JScrollPane tablePane = new JScrollPane();//滚动视口
protected JTable table = new JTable(); //列表
private JLabel numberLabel = new JLabel("图书编号");//编号标题
private JLabel nameLabel = new JLabel("图书名称");//名称标题
private JLabel priceLabel = new JLabel("图书单价");//单价标题
//添加功能组件
protected JTextField addNumberText = new JTextField(6);//添加编号文本框
protected JTextField addNameText = new JTextField(6);//添加名称文本框
protected JTextField addPriceText = new JTextField(6);//添加单价文本框
protected JTextField addUnitText = new JTextField(6);//添加计价单位文本框
private JButton addBtn = new JButton("添加图书");//添加按钮
//修改功能组件
protected JTextField updateNumberText = new JTextField(6);//修改编号文本框
protected JTextField updateNameText = new JTextField(6);//修改名称文本框
protected JTextField updatePriceText = new JTextField(6);//修改单价文本框
protected JTextField updateUnitText = new JTextField(6);//修改计价单位文本框
private JButton updateBtn = new JButton("修改图书");//修改按钮
//删除功能组件
protected JTextField delNumberText = new JTextField(6);//添加编号文本
private JButton delBtn = new JButton("删除图书");//删除按钮
//构造方法
public RootDialog() {
this(null,true);
}
public RootDialog(Frame owner, boolean modal) {
super(owner, modal);
this.init();// 初始化操作
this.addComponent();// 添加组件
this.addListener();// 添加监听器
}
// 初始化操作
private void init() {
this.setTitle("图书管理!");// 标题
this.setSize(600, 400);// 窗体大小与位置
GUTools.center(this);//设置窗口在屏幕上的位置
this.setResizable(false);// 窗体大小固定
}
// 添加组件
private void addComponent() {
//取消布局
this.setLayout(null);
//表格标题
tableLabel.setBounds(265, 20, 70, 25);
this.add(tableLabel);
//表格
table.getTableHeader().setReorderingAllowed(false); //列不能移动
table.getTableHeader().setResizingAllowed(false); //不可拉动表格
table.setEnabled(false); //不可更改数据
tablePane.setBounds(50, 50, 500, 200);
tablePane.setViewportView(table); //视口装入表格
this.add(tablePane);
//字段标题
numberLabel.setBounds(50, 250, 70, 25);
nameLabel.setBounds(150, 250, 70, 25);
priceLabel.setBounds(250, 250, 70, 25);
this.add(numberLabel);
this.add(nameLabel);
this.add(priceLabel);
//增加组件
addNumberText.setBounds(50, 280, 80, 25);
addNameText.setBounds(150, 280, 80, 25);
addPriceText.setBounds(250, 280, 80, 25);
this.add(addNumberText);
this.add(addNameText);
this.add(addPriceText);
addBtn.setBounds(460, 280, 90, 25);
this.add(addBtn);
//修改组件
updateNumberText.setBounds(50, 310, 80, 25);
updateNameText.setBounds(150, 310, 80, 25);
updatePriceText.setBounds(250, 310, 80, 25);
this.add(updateNumberText);
this.add(updateNameText);
this.add(updatePriceText);
updateBtn.setBounds(460, 310, 90, 25);
this.add(updateBtn);
//删除组件
delNumberText.setBounds(50, 340, 80, 25);
this.add(delNumberText);
delBtn.setBounds(460, 340, 90, 25);
this.add(delBtn);
}
// 添加监听器
private void addListener() {
//添加按钮监听
addBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//调用添加方法
addBookItem();
}
});
//修改按钮监听
updateBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//调用修改方法
updateBookItem();
}
});
//删除按钮监听
delBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//调用删除方法
delBookItem();
}
});
}
//查询方法
public abstract void queryBookItem();
//添加方法
public abstract void addBookItem();
//修改方法
public abstract void updateBookItem();
//删除方法
public abstract void delBookItem();
}
工具类:
package com.bookstore.tools;
import java.awt.Component;
import java.awt.Toolkit;
import javax.swing.JFrame;
/*
* 工具类
*/
public class GUTools {
//JAVA提供的GUI默认工具类对象
static Toolkit kit = Toolkit.getDefaultToolkit();
//将指定组件屏幕居中
public static void center(Component c) {
int x = (kit.getScreenSize().width - c.getWidth()) / 2;
int y = (kit.getScreenSize().height - c.getHeight()) / 2;
c.setLocation(x, y);
}
//为指定窗口设置图标标题
public static void setTitleImage(JFrame frame,String titleIconPath) {
frame.setIconImage(kit.createImage(titleIconPath));
}
}
package com.bookstore.tools;
import java.sql.Connection;
import java.sql.*;
public class JDBCLJ {
//把用户名、密码、URL、驱动类 这几个字符串定义为常量
//得到数据库的连接
public static Connection getConnection() throws SQLException,
ClassNotFoundException{
Class.forName("com.mysql.cj.jdbc.Driver");
String USER = "root";
String PWD = "123456";
String URL = "jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC";
Connection conn = DriverManager.getConnection(URL,USER,PWD);
return conn;
}
//关闭所有打开的资源
public static void release(Statement stmt ,Connection conn ){
if (stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null ;
}
if (conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
//关闭所有打开的资源
public static void release(ResultSet rs,Statement stmt,Connection conn){
if(rs!=null){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
rs=null;
}
release(stmt,conn);
}
}
服务类:
package com.bookstore.service;
import java.util.ArrayList;
import com.bookstore.dao.RootDao;
import com.bookstore.domain.BookItem;
/*
* 管理员服务类
*/
public class RootService {
private RootDao adminDao = new RootDao();
//查询服务
public ArrayList<BookItem> queryBookItem() {
//调用Dao层的获取所有数据方法获取所有数据
ArrayList<BookItem> data = adminDao.queryAllData();
//返回数据
return data;
}
//添加服务
public boolean addBookItem(String number, String name, String price) {
//调用Dao层的获取所有数据方法获取所有数据
ArrayList<BookItem> data = queryBookItem();
//使用输入的编号与所有数据对比
for (int i = 0; i < data.size(); i++) {
BookItem BookItem = data.get(i);
//如果存在重复编号数据,则添加不成功
if(number.equals(BookItem.getNumber())) {
return false;
}
}
//如果没有重复编号,将数据封装为FruitItem对象
BookItem thisBookItem= new BookItem(number, name,
Double.parseDouble(price));
//调用Dao层的添加数据方法
adminDao.addBookItem(thisBookItem);
//在添加数据后,返回添加成功
return true;
}
//修改服务
public boolean updateBookItem(String number, String name,
String price) {
//调用Dao层的获取所有数据方法获取所有数据
ArrayList<BookItem> data = queryBookItem();
//使用输入的编号与所有数据对比
for (int i = 0; i < data.size(); i++) {
BookItem BookItem = data.get(i);
//如果存在相同编号数据,则可以更新
if(number.equals(BookItem.getNumber())) {
//调用Dao层的删除指定编号数据方法
adminDao.delBookItem(number);
//如果没有重复编号,将数据封装为FruitItem对象
BookItem thisBookItem = new BookItem(number, name,
Double.parseDouble(price));
//调用Dao层的添加数据方法
adminDao.addBookItem(thisBookItem);
//在修改数据后,返回添加成功
return true;
}
}
//如果不存在相同编号数据,则不可以更新
return false;
}
//删除服务
public boolean delBookItem(String delNumber) {
//调用Dao层的获取所有数据方法获取所有数据
ArrayList<BookItem> data = queryBookItem();
//使用输入的编号与所有数据对比
for (int i = 0; i < data.size(); i++) {
BookItem BookItem = data.get(i);
//如果存在相同编号数据,则可以删除
if(delNumber.equals(BookItem.getNumber())) {
//调用Dao层的删除指定编号数据方法
adminDao.delBookItem(delNumber);
//在删除数据后,返回添加成功
return true;
}
}
//如果不存在相同编号数据,则不可以删除
return false;
}
}
domain 类
package com.bookstore.domain;
public class BookItem {
//属性
private String number;//编号
private String name; //名称
private double price; //价格
//构造方法
public BookItem() {
}
public BookItem(String number, String name, double price) {
super();
this.number = number;
this.name = name;
this.price = price;
}
//get/set方法
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
DAO类;`package com.bookstore.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
//import cn.itcast.fruitstore.data.DataBase;
import com.bookstore.domain.BookItem;
import com.bookstore.tools.JDBCLJ;
/*
* 管理员数据访问类
*/
public class RootDao {
public ArrayList<BookItem> queryAllData() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<BookItem> list = new ArrayList<BookItem>();
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "SELECT * FROM bookprize";
rs = stmt.executeQuery(sql);
// 处理结果集
while (rs.next()) {
BookItem BookItem= new BookItem();
BookItem.setNumber(rs.getString("number"));
BookItem.setName(rs.getString("name"));
BookItem.setPrice(rs.getDouble("price"));
list.add(BookItem);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
return null;
}
//添加数据
public void addBookItem(BookItem BookItem) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "INSERT INTO bookprize(number,name,price)"
+ "VALUES(" + BookItem.getNumber() + ",'" + BookItem.getName()
+ "','" + BookItem.getPrice() + "','" + "')";
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("插入数据成功!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
}
//删除数据
public void delBookItem(String delNumber) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "DELETE FROM bookprize WHERE number=" + delNumber;
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("删除数据成功!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
}
//删除数据
public void updateBookItem(String number) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "update FROM bookprize WHERE number=" + number;
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("删除数据成功!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
}
}
Controller类:
package com.bookstore.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
//import cn.itcast.fruitstore.data.DataBase;
import com.bookstore.domain.BookItem;
import com.bookstore.tools.JDBCLJ;
/*
* 管理员数据访问类
*/
public class RootDao {
public ArrayList<BookItem> queryAllData() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<BookItem> list = new ArrayList<BookItem>();
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "SELECT * FROM bookprize";
rs = stmt.executeQuery(sql);
// 处理结果集
while (rs.next()) {
BookItem BookItem= new BookItem();
BookItem.setNumber(rs.getString("number"));
BookItem.setName(rs.getString("name"));
BookItem.setPrice(rs.getDouble("price"));
list.add(BookItem);
}
return list;
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
return null;
}
//添加数据
public void addBookItem(BookItem BookItem) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "INSERT INTO bookprize(number,name,price)"
+ "VALUES(" + BookItem.getNumber() + ",'" + BookItem.getName()
+ "','" + BookItem.getPrice() + "','" + "')";
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("插入数据成功!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
}
//删除数据
public void delBookItem(String delNumber) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "DELETE FROM bookprize WHERE number=" + delNumber;
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("删除数据成功!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
}
//删除数据
public void updateBookItem(String number) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获得数据的连接
conn = JDBCLJ.getConnection();
// 获得Statement对象
stmt = conn.createStatement();
// 发送SQL语句
String sql = "update FROM bookprize WHERE number=" + number;
int num = stmt.executeUpdate(sql);
if (num > 0) {
System.out.println("删除数据成功!");
}
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCLJ.release(rs, stmt, conn);
}
}
}
package com.bookstore.controller;
import com.bookstore.controller.RootDialogController;
import com.bookstore.views.BookMainFrame;
/**
* 主界面操作类
*/
@SuppressWarnings("serial")
public class FrameController extends BookMainFrame {
@Override
public void showRootDialog() {
//在该方法中创建管理员界面并显示
//this为父窗口(主界面) true:设置为模态窗口展示
new RootDialogController(this, true).setVisible(true);
}
}
APP类:package com.bookstore.app;
import com.bookstore.controller.FrameController;
public class APP {
public static void main(String[] args) {
new FrameController().setVisible(true);
}
}