这个学期学习了java语言程序设计和数据库原理与应用,期末就有一个大作业是通过Java实现药房库存管理系统,于是就使用系统采用Java编程语言,结合JDBC进行数据库操作,并使用Swing库构建用户交互界面。
一、项目概述
本项目开发了一个药房库存管理系统,主要面向管理员用户,提供药品信息的增删改查(CRUD)等基本功能。系统采用Java编程语言,结合JDBC进行数据库操作,并利用Swing库构建用户交互界面。该系统模拟了一个药房的库存管理流程,包括药品的查询、增添、修改和删除等操作。
二、开发环境及依赖
- 开发环境:JDK 1.8 或更高版本
- IDE: Eclipse
- 数据库:SQL Server
- GUI库:Swing
- 其他:JDBC驱动
三、数据库设计
系统数据库包含多个表,以下是部分关键表的设计:
- 药品表 (药品表)
- 药品ID (int, 主键, 自增)
- 药品名称 (varchar)
- 分类ID (int)
- 剂型 (varchar)
- 用法 (text)
- 存量 (int)
- 生产厂家 (varchar)
- 进价 (decimal)
- 售价 (decimal)
- 进货日期 (date)
- 有效期 (date)
- 特殊禁忌 (text)
- 销售记录表 (销售记录表)
- 销售记录ID (int, 主键, 自增)
- 药品ID (int)
- 销售数量 (int)
- 销售日期 (date)
- 销售总价 (decimal)
- 进货记录表 (进货记录表)
- 进货记录ID (int, 主键, 自增)
- 药品ID (int)
- 进货数量 (int)
- 进货日期 (date)
- 进货总价 (decimal)
四、核心功能实现
1. 登录界面
系统启动时首先展示登录界面,用户输入用户名和密码进行验证。如果输入正确,则进入药房库存管理系统主界面。
class My_object1 extends JFrame {
public JTextField j3;
public JTextField j4;
ImageIcon img = null;
JPanel p;
public My_object1() {
// img = new ImageIcon("D:\\21612\\Pictures\\Saved Pictures\\3.jpg");
setTitle("登录");
setSize(200, 150);
// JFrame j=new JFrame();
// JPanel p = new JPanel();
// LayoutManager icon = (LayoutManager) new ImageIcon("D:\\21612\\Pictures\\Saved Pictures\\3.jpg");
// 自定义的JPanel,用于设置背景图片
JPanel p = new JPanel() {
private Image bgImage;
{
// 加载图片并存储为Image对象
bgImage = new ImageIcon("D:\\21612\\Pictures\\Saved Pictures\\3.jpg").getImage();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // 调用父类的paintComponent方法
if (bgImage != null) {
// 绘制背景图片
g.drawImage(bgImage, 0, 0, getWidth(), getHeight(), this);
}
}
};
getContentPane().add(p);
JLabel j1 = new JLabel("密码 ");
JLabel j2 = new JLabel("用户名");
JLabel j7 = new JLabel(img);
j3 = new JTextField(10);
j4 = new JTextField(10);
JButton j5 = new JButton("确定");
JButton j6 = new JButton("取消");
p.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
// p.add(j7);
p.add(j2);
p.add(j3);
p.add(j1);
p.add(j4);
p.add(j5);
p.add(j6);
j6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getActionCommand().equals("取消")) {
System.exit(0);
}
}
});
// final String userName = "abc";
// final String passwrod = "123";
j5.addMouseListener(new Mouselen());
setLocationRelativeTo(null);
setVisible(true);
}
class Mouselen implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if (!j3.getText().equals("123456"))
// System.out.println("用户名错误");
JOptionPane.showMessageDialog(null, "账号错误");
if (!j4.getText().equals("789"))
// System.out.println("密码错误");
JOptionPane.showMessageDialog(null, "密码错误");
else // System.out.println("输入正确");
// JOptionPane.showMessageDialog(null, "登录成功");
{
// System.exit(0);
new guanli();
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
}
}
2. 主界面
主界面包含多个按钮,分别对应不同的操作:查询、增添、修改、删除等。每个按钮点击后,会弹出相应的子窗口进行具体操作。
class guanli extends JFrame {
JButton b1;
JButton b2;
public guanli() {
setSize(550, 370);
Toolkit tk = getToolkit();
Dimension d1 = tk.getScreenSize();
int w = (int) d1.getWidth();
int h = (int) d1.getHeight();
setLocation(w / 2 - 550 / 2, h / 2 - 370 / 2);
setVisible(true);
setTitle("药房库存管理系统");
// Image img1=tk.getImage("./src/jame/1-140326154313.jpg");
// setIconImage(img1);
// ImageIcon img2 = new ImageIcon("D:\\21612\\Pictures\\Saved Pictures\\3.jpg"); // 使用类加载器获取资源
// if (img2 != null) {
// JLabel imgLabel = new JLabel(img2);
// imgLabel.setBounds(0, 0, getWidth(), getHeight()); // 设置与窗口相同的大小
// getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
// }
//
// ImageIcon img2 = new ImageIcon("./src/jame/13.jpg"); // 相对路径获取图片
// JLabel imgLabel = new JLabel(img2); // 创建图片标签
// this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
// // 为标签设置为容器最底层;
// //getLayeredPane()作用是为容器添加深度,允许组件互相重叠;
// //Integer.MIN_VALUE最底层
// imgLabel.setBounds(0, 0, img2.getIconWidth(), img2.getIconHeight());
Container cp = getContentPane(); // 获取顶级容器
((JPanel) cp).setOpaque(false); // 设置透明以使底层背景图片显示
cp.setLayout(null);
JLabel imgLable = new JLabel();
JLabel j2 = new JLabel("***欢迎登陆药房库存管理系统***");
JLabel j1 = new JLabel("~~请在左侧选择所做的操作~~");
JButton b1 = new JButton("查询");
JButton b2 = new JButton("取消");
JButton b3 = new JButton("修改");
JButton b4 = new JButton("增添");
JButton b5 = new JButton("删除");
JTextArea t3 = new JTextArea(150, 150);
cp.add(j2);
j2.setBounds(220, 110, 190, 25);
cp.add(j1);
j1.setBounds(220, 150, 190, 25);
cp.add(b1);
b1.setBounds(0, 70, 90, 25);
cp.add(b2);
b2.setBounds(250, 250, 70, 25);
cp.add(b4);
b4.setBounds(0, 110, 90, 25);
cp.add(b3);
b3.setBounds(0, 150, 90, 25);
cp.add(b5);
b5.setBounds(0, 190, 90, 25);
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getActionCommand().equals("取消")) {
System.exit(0);
}
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
new chaxun();
}
});
b3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
new xiugai();
}
});
b4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
new zengtian();
}
});
b5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
new shanchu();
}
});
}
}
3. 查询功能
用户可以通过查询界面输入药品ID、销售记录ID或进货记录ID进行查询。系统根据用户输入,执行相应的SQL查询语句,并将结果显示在界面上。
class chaxun extends JFrame {
TextField t1;
TextField t2;
public chaxun() {
setTitle("查询");
setSize(550, 370);
JPanel p1 = new JPanel();
// getContentPane().add(p1);
JLabel j1 = new JLabel("药品ID: ");
JLabel j2 = new JLabel("销售记录ID: ");
JLabel j3 = new JLabel("进货记录ID: ");
TextField t1 = new TextField(70);// 药品ID
TextField t2 = new TextField(70);// 销售记录ID
TextField t3 = new TextField(70);// 进货记录ID
JButton b = new JButton("查询");
JButton b1 = new JButton("取消");
// ImageIcon img2 = new ImageIcon("./src/1-140326154313.jpg"); // 相对路径获取图片
ImageIcon img2 = new ImageIcon("D:\\21612\\Pictures\\Saved Pictures\\2.jpg");
// JLabel imgLabel = new JLabel(img2); // 创建图片标签
// imgLabel.setBounds(0, 0, img2.getIconWidth(), img2.getIconHeight());
// this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
// 为标签设置为容器最底层;getLayeredPane()作用是为容器添加深度,允许组件互相重叠;Integer.MIN_VALUE最底层
Container p11 = getContentPane(); // 获取顶级容器
((JPanel) p11).setOpaque(false); // 设置透明以使底层背景图片显示
p11.setLayout(null);
p11.add(j1);
j1.setBounds(50, 70, 70, 25);
p11.add(t1);
t1.setBounds(150, 70, 100, 20);
p11.add(j2);
j2.setBounds(50, 150, 70, 25);
p11.add(t2);
t2.setBounds(150, 150, 100, 20);
p11.add(j3);
j3.setBounds(50, 230, 70, 25);
p11.add(t3);
t3.setBounds(150, 230, 100, 20);
p11.add(b);
b.setBounds(400, 140, 90, 25);
p11.add(b1);
b1.setBounds(400, 170, 90, 25);
setLocationRelativeTo(null);
setVisible(true);
final String url = "jdbc:sqlserver://localhost:1433";
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO 自动生成的方法存根
if (e.getActionCommand().equals("取消")) {
System.exit(0);
}
}
});
// b.addActionListener(new ActionListener() {
//
// @Override
// public void actionPerformed(ActionEvent arg0) {
// // TODO Auto-generated method stub
//
// try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码")) {
// Statement state = conn.createStatement();
// ResultSet result = state.executeQuery("SELECT * FROM 药房库存管理系统.药品表");
//
// } catch (SQLException ex) {
//
// ex.getSQLState();
// }
// }
// });
// 为"查询"按钮添加ActionListener
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 根据用户输入构建查询语句并执行查询
if (t1.getText() != null && !t1.getText().isEmpty()) {
String query = "SELECT * FROM 药房库存管理系统.dbo.药品表 WHERE 药品ID = '" + t1.getText() + "'"; // 假设根据药品ID查询
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
Statement state = conn.createStatement();
ResultSet result = state.executeQuery(query)) {
while (result.next()) {
System.out.println("以下是药品ID为" + t1.getText() + "相关信息");
System.out.println("药品ID:" + result.getInt("药品ID") + "\n药品名称:" + result.getString("药品名称")
+ "\n分类ID:" + result.getInt("分类ID"));
System.out.println("剂型:" + result.getString("剂型") + "\n用法:" + result.getString("用法")
+ "\n存量:" + result.getInt("存量"));
System.out.println("生产厂家:" + result.getString("生产厂家") + "\n进价:" + result.getInt("进价")
+ "\n售价:" + result.getInt("售价"));
System.out.println("进货日期:" + result.getDate("进货日期") + "\n有效期:" + result.getDate("有效期")
+ "\n特殊禁忌:" + result.getString("特殊禁忌"));
// [药品ID] [int] IDENTITY(1,1) NOT NULL,
// [药品名称] [varchar](200) NULL,
// [分类ID] [int] NULL,
// [剂型] [varchar](200) NULL,
// [用法] [text] NULL,
// [存量] [int] NULL,
// [生产厂家] [varchar](200) NULL,
// [进价] [decimal](10, 2) NULL,
// [售价] [decimal](10, 2) NULL,
// [进货日期] [date] NULL,
// [有效期] [date] NULL,
// [特殊禁忌] [text] NULL,
}
} catch (SQLException ex) {
// 处理SQLException,比如打印堆栈跟踪或显示错误消息给用户
ex.printStackTrace();
JOptionPane.showMessageDialog(chaxun.this, "查询出错:" + ex.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
}
}
if (t2.getText() != null && !t2.getText().isEmpty()) {
String query1 = "SELECT * FROM 药房库存管理系统.dbo.销售记录表,药房库存管理系统.dbo.药品表 WHERE 销售记录ID = '" + t2.getText()
+ "' AND 销售记录表.药品ID=药品表.药品ID ";
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
Statement state1 = conn.createStatement();
ResultSet result1 = state1.executeQuery(query1)) {
while (result1.next()) {
System.out.println("以下是销售记录ID为" + t2.getText() + "相关信息");
System.out.println("销售数量" + result1.getInt("销售数量") + "\n销售日期:" + result1.getDate("销售日期")
+ "\n销售总价:" + result1.getInt("销售总价"));
System.out.println("药品ID:" + result1.getInt("药品ID") + "\n药品名称:" + result1.getString("药品名称")
+ "\n分类ID:" + result1.getInt("分类ID"));
System.out.println("剂型:" + result1.getString("剂型") + "\n用法:" + result1.getString("用法")
+ "\n存量:" + result1.getInt("存量"));
System.out.println("生产厂家:" + result1.getString("生产厂家") + "\n进价:" + result1.getInt("进价")
+ "\n售价:" + result1.getInt("售价"));
System.out.println("进货日期:" + result1.getDate("进货日期") + "\n有效期:" + result1.getDate("有效期")
+ "\n特殊禁忌:" + result1.getString("特殊禁忌"));
// [销售记录ID] [int] IDENTITY(1,1) NOT NULL,
// [药品ID] [int] NULL,
// [销售数量] [int] NULL,
// [销售日期] [date] NULL,
// [销售总价] [decimal](10, 2) NULL,
}
} catch (SQLException ex) {
// 处理SQLException,比如打印堆栈跟踪或显示错误消息给用户
ex.printStackTrace();
JOptionPane.showMessageDialog(chaxun.this, "查询出错:" + ex.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
}
}
if (t3.getText() != null && !t3.getText().isEmpty()) {
String query2 = "SELECT * FROM 药房库存管理系统.dbo.进货记录表,药房库存管理系统.dbo.药品表 WHERE 进货记录ID = '" + t3.getText()
+ "'AND 进货记录表.药品ID=药品表.药品ID ";
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
Statement state2 = conn.createStatement();
ResultSet result2 = state2.executeQuery(query2)) {
while (result2.next()) {
System.out.println("以下是进货记录ID为" + t3.getText() + "相关信息");
System.out.println("进货数量" + result2.getInt("进货数量") + "\n进货日期:" + result2.getDate("进货日期")
+ "\n进货总价:" + result2.getInt("进货总价"));
System.out.println("药品ID:" + result2.getInt("药品ID") + "\n药品名称:" + result2.getString("药品名称")
+ "\n分类ID:" + result2.getInt("分类ID"));
System.out.println("剂型:" + result2.getString("剂型") + "\n用法:" + result2.getString("用法")
+ "\n存量:" + result2.getInt("存量"));
System.out.println("生产厂家:" + result2.getString("生产厂家") + "\n进价:" + result2.getInt("进价")
+ "\n售价:" + result2.getInt("售价"));
System.out.println("进货日期:" + result2.getDate("进货日期") + "\n有效期:" + result2.getDate("有效期")
+ "\n特殊禁忌:" + result2.getString("特殊禁忌"));
// [进货记录ID] [int] IDENTITY(1,1) NOT NULL,
// [药品ID] [int] NULL,
// [进货数量] [int] NULL,
// [进货日期] [date] NULL,
// [进货总价] [decimal](10, 2) NULL,
}
} catch (SQLException ex) {
// 处理SQLException,比如打印堆栈跟踪或显示错误消息给用户
ex.printStackTrace();
JOptionPane.showMessageDialog(chaxun.this, "查询出错:" + ex.getMessage(), "错误",
JOptionPane.ERROR_MESSAGE);
}
}
}
});
}
private BufferedImage loadImage(String string) {
// TODO 自动生成的方法存根
return null;
}
}
4. 增添功能
增添功能通过菜单选择具体要增添的表格(如药品表、销售记录表等),然后弹出对应的表单界面。用户在表单中输入数据后,点击保存按钮,系统将数据插入数据库。
class zengtian extends JFrame {
public zengtian() {
setTitle("增添");
setSize(550, 370);
JMenuBar menuBar = new JMenuBar();
JMenu tablesMenu = new JMenu("选择要增添的表格");
// 添加菜单项和动作监听器
JMenuItem drugMenuItem1 = new JMenuItem("药品表");
drugMenuItem1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrugForm drugForm = new DrugForm();
drugForm.setVisible(true);
}
});
tablesMenu.add(drugMenuItem1);
// ... 可以添加更多表格的菜单项
JMenuItem drugMenuItem2 = new JMenuItem("药品分类表");
drugMenuItem2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrugForm1 drugForm1 = new DrugForm1();
drugForm1.setVisible(true);
}
});
tablesMenu.add(drugMenuItem2);
JMenuItem drugMenuItem3 = new JMenuItem("销售记录表");
drugMenuItem3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrugForm2 drugForm2 = new DrugForm2();
drugForm2.setVisible(true);
}
});
tablesMenu.add(drugMenuItem3);
JMenuItem drugMenuItem4 = new JMenuItem("进货记录表");
drugMenuItem4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
DrugForm3 drugForm3 = new DrugForm3();
drugForm3.setVisible(true);
}
});
tablesMenu.add(drugMenuItem4);
menuBar.add(tablesMenu);
setJMenuBar(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// JPanel p1 = new JPanel();
// getContentPane().add(p1);
setLocationRelativeTo(null);
setVisible(true);
}
}
class DrugForm extends JFrame {
final String url = "jdbc:sqlserver://localhost:1433";
private JTextField Field1;
private JTextField Field2;
private JTextField Field3;
private JTextField Field4;
private JTextField Field5;
private JTextField Field6;
private JTextField Field7;
private JTextField Field8;
private JTextField Field9;
private JTextField Field10;
private JTextField Field11;
private JTextField Field12;
// ... 其他字段的文本框
public DrugForm() {
setTitle("药品增添");
setSize(500, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(13, 2, 10, 10)); // 13行2列的布局
// 添加标签和文本框到面板
panel.add(new JLabel("药品ID:"));
Field1 = new JTextField(20);
panel.add(Field1);
panel.add(new JLabel("药品名称:"));
Field2 = new JTextField(20);
panel.add(Field2);
panel.add(new JLabel("分类ID:"));
Field3 = new JTextField(20);
panel.add(Field3);
panel.add(new JLabel("剂型:"));
Field4 = new JTextField(20);
panel.add(Field4);
panel.add(new JLabel("用法:"));
Field5 = new JTextField(20);
panel.add(Field5);
panel.add(new JLabel("存量:"));
Field6 = new JTextField(20);
panel.add(Field6);
panel.add(new JLabel("生产厂商:"));
Field7 = new JTextField(20);
panel.add(Field7);
panel.add(new JLabel("进价:"));
Field8 = new JTextField(20);
panel.add(Field8);
panel.add(new JLabel("售价:"));
Field9 = new JTextField(20);
panel.add(Field9);
panel.add(new JLabel("进货日期:"));
Field10 = new JTextField(20);
panel.add(Field10);
panel.add(new JLabel("有效期:"));
Field11 = new JTextField(20);
panel.add(Field11);
panel.add(new JLabel("特殊禁忌:"));
Field12 = new JTextField(20);
panel.add(Field12);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
PreparedStatement pstmt = conn.prepareStatement(
"SET IDENTITY_INSERT 药房库存管理系统.dbo.药品表 ON INSERT INTO 药房库存管理系统.dbo.药品表 (药品ID, 药品名称,分类ID,剂型,用法,存量,生产厂家,进价,售价,进货日期,有效期,特殊禁忌 ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
pstmt.setString(1, Field1.getText());
pstmt.setString(2, Field2.getText());
pstmt.setString(3, Field3.getText());
pstmt.setString(4, Field4.getText());
pstmt.setString(5, Field5.getText());
pstmt.setString(6, Field6.getText());
pstmt.setString(7, Field7.getText());
pstmt.setString(8, Field8.getText());
pstmt.setString(9, Field9.getText());
pstmt.setString(10, Field10.getText());
pstmt.setString(11, Field11.getText());
pstmt.setString(12, Field12.getText());
int rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("药品已成功添加到数据库。");
}
} catch (SQLException ex) {
System.err.println("数据库操作出错: " + ex.getMessage());
}
}
});
// public void actionPerformed(ActionEvent e) {
// // 调用数据库插入方法,这里只是打印输入作为示例
// String id = Field1.getText();
// String name = Field2.getText();
// // ... 获取其他字段的值
// System.out.println("Inserting drug with ID: " + id + ", Name: " + name + "...");
// // TODO: 调用数据库插入方法
// }
panel.add(saveButton);
getContentPane().add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null);
setVisible(true);
}
}
class DrugForm1 extends JFrame {
final String url = "jdbc:sqlserver://localhost:1433";
private JTextField Field1;
private JTextField Field2;
public DrugForm1() {
setTitle("药品分类增添");
setSize(500, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(13, 2, 10, 10)); // 13行2列的布局
// 添加标签和文本框到面板
panel.add(new JLabel("分类ID:"));
Field1 = new JTextField(20);
panel.add(Field1);
panel.add(new JLabel("分类名称:"));
Field2 = new JTextField(20);
panel.add(Field2);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
PreparedStatement pstmt = conn.prepareStatement(
"SET IDENTITY_INSERT 药房库存管理系统.dbo.药品分类表 ON INSERT INTO 药房库存管理系统.dbo.药品分类表 (药品分类ID, 分类名称 ) VALUES (?, ?)")) {
pstmt.setString(1, Field1.getText());
pstmt.setString(2, Field2.getText());
int rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("药品分类记录已成功添加到数据库。");
}
} catch (SQLException ex) {
System.err.println("数据库操作出错: " + ex.getMessage());
}
}
});
panel.add(saveButton);
getContentPane().add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null);
setVisible(true);
}
}
class DrugForm2 extends JFrame {
final String url = "jdbc:sqlserver://localhost:1433";
private JTextField Field1;
private JTextField Field2;
private JTextField Field3;
private JTextField Field4;
private JTextField Field5;
public DrugForm2() {
setTitle("销售记录增添");
setSize(500, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(13, 2, 10, 10)); // 13行2列的布局
// 添加标签和文本框到面板
panel.add(new JLabel("销售记录ID:"));
Field1 = new JTextField(20);
panel.add(Field1);
panel.add(new JLabel("药品ID:"));
Field2 = new JTextField(20);
panel.add(Field2);
panel.add(new JLabel("销售数量:"));
Field3 = new JTextField(20);
panel.add(Field3);
panel.add(new JLabel("销售日期:"));
Field4 = new JTextField(20);
panel.add(Field4);
panel.add(new JLabel("销售总价:"));
Field5 = new JTextField(20);
panel.add(Field5);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
PreparedStatement pstmt = conn.prepareStatement(
"SET IDENTITY_INSERT 药房库存管理系统.dbo.销售记录表 ON INSERT INTO 药房库存管理系统.dbo.销售记录表 (销售记录ID,药品ID,销售数量,销售日期,销售总价 ) VALUES (?, ?, ?, ?, ?)")) {
pstmt.setString(1, Field1.getText());
pstmt.setString(2, Field2.getText());
pstmt.setString(3, Field3.getText());
pstmt.setString(4, Field4.getText());
pstmt.setString(5, Field5.getText());
int rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("销售记录已成功添加到数据库。");
}
} catch (SQLException ex) {
System.err.println("数据库操作出错: " + ex.getMessage());
}
}
});
panel.add(saveButton);
getContentPane().add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null);
setVisible(true);
}
}
class DrugForm3 extends JFrame {
final String url = "jdbc:sqlserver://localhost:1433";
private JTextField Field1;
private JTextField Field2;
private JTextField Field3;
private JTextField Field4;
private JTextField Field5;
public DrugForm3() {
setTitle("进货记录增添");
setSize(500, 400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(13, 2, 10, 10)); // 13行2列的布局
// 添加标签和文本框到面板
panel.add(new JLabel("进货记录ID:"));
Field1 = new JTextField(20);
panel.add(Field1);
panel.add(new JLabel("药品ID:"));
Field2 = new JTextField(20);
panel.add(Field2);
panel.add(new JLabel("进货数量:"));
Field3 = new JTextField(20);
panel.add(Field3);
panel.add(new JLabel("进货日期:"));
Field4 = new JTextField(20);
panel.add(Field4);
panel.add(new JLabel("进货总价:"));
Field5 = new JTextField(20);
panel.add(Field5);
JButton saveButton = new JButton("保存");
saveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
PreparedStatement pstmt = conn.prepareStatement(
"SET IDENTITY_INSERT 药房库存管理系统.dbo.进货记录表 ON INSERT INTO 药房库存管理系统.dbo.进货记录表 (进货记录ID,药品ID,进货数量,销售日期,进货总价 ) VALUES (?, ?, ?, ?, ?)")) {
pstmt.setString(1, Field1.getText());
pstmt.setString(2, Field2.getText());
pstmt.setString(3, Field3.getText());
pstmt.setString(4, Field4.getText());
pstmt.setString(5, Field5.getText());
int rowsInserted = pstmt.executeUpdate();
if (rowsInserted > 0) {
System.out.println("销售记录已成功添加到数据库。");
}
} catch (SQLException ex) {
System.err.println("数据库操作出错: " + ex.getMessage());
}
}
});
panel.add(saveButton);
getContentPane().add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null);
setVisible(true);
}
}
5. 修改功能
修改功能尚未完整实现,但通常流程是用户首先通过查询功能找到需要修改的记录,然后系统提供修改界面让用户输入新的数据,并更新数据库。
修改操作不想写了qwq,如果有人看上了这些代码,到时候再写吧……嘻嘻嘻嘻
6. 删除功能
删除功能允许用户输入要删除的表名和条件,执行删除操作。系统通过构建相应的SQL DELETE语句并执行,完成记录的删除。
这里的删除操作比较草率要手动写入SQL的删除语句才能执行,主要是想偷懒了
class shanchu extends JFrame {
final String url = "jdbc:sqlserver://localhost:1433";
private JTextField textField; // 用于输入要删除的记录的条件
private JTextField textField1;
public shanchu() {
setTitle("删除");
setSize(550, 370);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 使用GridBagLayout
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// 第一行:标签
JLabel label = new JLabel("输入要删除的表名:");
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(label, c);
// 第一行:文本框
textField = new JTextField(10);
c.gridx = 1;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(textField, c);
// 第二行(留空或添加其他组件)
// 例如,你可以添加另一个标签和文本框来输入其他条件
JLabel label1 = new JLabel("输入要删除的记录的条件:");
c.gridx = 0;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(label1, c);
// 第一行:文本框
textField1 = new JTextField(10);
c.gridx = 1;
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
panel.add(textField1, c);
// 底部:添加按钮
JButton deleteButton = new JButton("删除");
c.gridx = 0; // 可以设置为0或1,取决于你想要按钮的位置
c.gridy = 2; // 这是第二行(从0开始计数)
c.gridwidth = 2; // 跨两列
c.insets = new Insets(10, 0, 0, 0); // 上方添加一些间距
panel.add(deleteButton, c);
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textField.getText().isEmpty()||textField1.getText().isEmpty()) {
JOptionPane.showMessageDialog(shanchu.this, "请输入表名和条件!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
try (Connection conn = DriverManager.getConnection(url, "sa", "数据库密码");
PreparedStatement pstmt = conn.prepareStatement("DELETE FROM 药房库存管理系统.dbo."+textField.getText() +" WHERE " + "?")) {
// 注意:直接使用用户输入的列名存在SQL注入风险,这里仅作示例
// 在实际应用中,应该避免这种做法,或者使用白名单验证列名
pstmt.setString(1, textField1.getText());
int rowsDeleted = pstmt.executeUpdate();
if (rowsDeleted > 0) {
JOptionPane.showMessageDialog(shanchu.this, "已成功删除 " + rowsDeleted + " 条记录。", "成功", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(shanchu.this, "未找到匹配的记录进行删除。", "信息", JOptionPane.INFORMATION_MESSAGE);
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(shanchu.this, "数据库操作出错: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
ex.printStackTrace();
}
}
});
getContentPane().add(panel, BorderLayout.CENTER);
setLocationRelativeTo(null);
setVisible(true);
}
}
五、界面设计
系统界面采用Swing库进行开发,各窗口布局合理,操作直观。主界面和子窗口均包含必要的标签、文本框、按钮等组件,用户通过简单的鼠标操作即可完成各项功能。
我还没学明白如何正确的调节窗体的大小,使整体显示的窗体比较小,后续改进……