填写姓名和班级后,点击添加按钮后可添加数据
3、修改,如图
通过点击查询界面中“修改按钮”,可在修改界面修改当前选中行数据
三、数据库
=================
注意:数据库名称为“班级_姓名”,如“1705_小白”。
表名称:tGoods
字段
字段名 | 数据类型 | 描述 | 约束 |
goodsID | int | 商品编号 | 主键自增长 |
name | varchar(20) | 商品名称 | |
typeName | varchar(20) | 类别名称 | |
stock | Int | 库存 |
四**、评分规则(共100分)******
=================================================
标题 | 分值 |
合理注释 | 10分 |
命名规范 | 10分 |
查询 | 20分 |
修改 | 10分 |
删除 | 10分 |
添加 | 10分 |
数据库创建 | 10分 |
数据库连接 | 10分 |
整体效果 | 10分 |
实现代码:
=====
数据库 链接: https://pan-yz.chaoxing.com/external/m/file/483246110958415872
Java文件 链接: https://pan-yz.chaoxing.com/external/m/file/483246085097291776
数据库:
– Table structure for tgoods
DROP TABLE IF EXISTS tgoods
;
CREATE TABLE tgoods
(
goodsID
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(20) DEFAULT NULL,
typeName
varchar(20) DEFAULT NULL,
stock
int(11) DEFAULT NULL,
PRIMARY KEY (goodsID
)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
– Records of tgoods
INSERT INTO tgoods
VALUES (‘9’, ‘统一冰红茶’, ‘饮料’, ‘24’);
INSERT INTO tgoods
VALUES (‘10’, ‘娃哈哈营养快线’, ‘饮料’, ‘23’);
com.test.db >>> DbConnection
package com.test.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
public class DbConnection {
//驱动类的类名
private static final String DRIVERNAME=“com.mysql.jdbc.Driver”;
//连接数据的URL路径
private static final String URL=“jdbc:mysql://localhost:3306/1902_杨明金”;
//数据库登录账号
private static final String USER=“root”;
//数据库登录密码
private static final String PASSWORD=“root123”;
//加载驱动
static{
try {
Class.forName(DRIVERNAME);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取数据库连接
public static Connection getConnection() {
try {
return DriverManager.getConnection(URL,USER,PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//查询
public static ResultSet query(String sql) {
System.out.println(sql);
//获取连接
Connection connection=getConnection();
PreparedStatement psd;
try {
psd = connection.prepareStatement(sql);
return psd.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//增、删、改、查
public static int updataInfo(String sql) {
System.out.println(sql);
//获取连接
Connection connection=getConnection();
try {
PreparedStatement psd=connection.prepareStatement(sql);
return psd.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//关闭连接
public static void colse(ResultSet rs,Statement stmt,Connection conn) throws Exception{
try { if (rs != null){ rs.close(); }
if (stmt != null) { stmt.cancel(); }
if (conn != null) { conn.close(); }
} catch (Exception e) {
e.printStackTrace(); throw new Exception();
}
}
}
com.test.entity >>> Goods
package com.test.entity;
public class Goods {
private int goodsID;//商品ID
private String name;//商品名称
private String typeName;//商品类别
private int stock;//库存
public Goods(int goodsID, String name, String typeName, int stock) {
super();
this.goodsID = goodsID;
this.name = name;
this.typeName = typeName;
this.stock = stock;
}
public Goods() {
super();
}
public int getGoodsID() {
return goodsID;
}
public void setGoodsID(int goodsID) {
this.goodsID = goodsID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
}
com.test.controller >>> Updata
package com.test.controller;
import com.test.db.DbConnection;
public class Updata {
//添加数据
public static int addData(String sql) {
return DbConnection.updataInfo(sql);
}
}
com.test.controller >>> Select
package com.test.controller;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.test.db.DbConnection;
import com.test.entity.Goods;
public class Select {
public static Object[][] getGoods() {
String sql = “SELECT * FROM tgoods”;
ResultSet resultSet = DbConnection.query(sql);
ArrayList list=new ArrayList();
try {
while (resultSet.next()) {
Goods goods=new Goods();
goods.setGoodsID(resultSet.getInt(1));
goods.setName(resultSet.getString(2));
goods.setTypeName(resultSet.getString(3));
goods.setStock(resultSet.getInt(4));
list.add(goods);
}
} catch (SQLException e) {
e.printStackTrace();
}
Object[][] objects=new Object[list.size()][4];
for(int i=0;i<list.size();i++) {
objects[i][0]=list.get(i).getGoodsID();
objects[i][1]=list.get(i).getName();
objects[i][2]=list.get(i).getTypeName();
objects[i][3]=list.get(i).getStock();
}
return objects;
}
}
com.test.View >>> IndexGUI
package com.test.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.test.controller.Select;
import com.test.controller.Updata;
import com.test.entity.Goods;
public class IndexGUI extends JFrame {
Object[] columnNames = {“商品编号”,“名称”,“类别名称”,“库存”};
Object[][] data = Select.getGoods();
DefaultTableModel df = new DefaultTableModel(data, columnNames);
public IndexGUI() {
super(“商品信息管理”);
this.setBounds(0, 0, 780, 500);
this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
this.setResizable(false);//让窗口大小不可改变
this.setLayout(null);
JTable jTable=new JTable(df);
JScrollPane jp = new JScrollPane(jTable);
jp.setBounds(0, 10, 780, 350);
this.add(jp);
JButton tj = new JButton(“添加”);
tj.setBounds(50, 400, 100, 30);
tj.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
IncreaseGUL i = new IncreaseGUL();
i.setVisible(true);
}
});
JButton sc = new JButton(“删除”);
sc.setBounds(180, 400, 100, 30);
sc.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTable.getSelectedColumn()<0) {
JOptionPane.showMessageDialog(null, “请选中要删除的数据!”);
} else {
int goodsID = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
String sql=“delete from tgoods where goodsid=”+goodsID;
Updata updata = new Updata();
int result = updata.addData(sql);
if (result>0) {
JOptionPane.showMessageDialog(null, “删除成功!”);
JOptionPane.showMessageDialog(null, “记得刷新一下哦!”);
} else {
JOptionPane.showMessageDialog(null, “删除失败!”);
}
}
}
});
JButton xg = new JButton(“修改”);
xg.setBounds(310, 400, 100, 30);
xg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jTable.getSelectedColumn()<0) {
JOptionPane.showMessageDialog(null, “请选择要修改的数据!”);
} else {
int goodsID = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());