一个非常简单的超市销售系统,只涉及增删改查。
1. 登录
两种方式登录系统,这里主要分享管理员登录
最简单的方法,指定账号密码
JButton button_login = new JButton("登录");
button_login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String name = textField_username.getText();
String password= passwordField.getText();
if (name.equals("1234") && password.equals("1234"))
{
GoodsManagerJframe mainJframe = new GoodsManagerJframe();
mainJframe.setVisible(true);
dispose();
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误!", "标题",JOptionPane.WARNING_MESSAGE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
}
);
2.菜单
商品的增删改查
3.商品入库(最简单
向输入框中输入商品信息,保存即可
添加数据库SQL语句
public void JDBCSQLAdd throws ClassNotFoundException, SQLException {
// 添加商品
String sql = "INSERT INTO `goodstable` VALUES(?,?,?,?)";// 添加语句
statement = con.prepareStatement(sql);
statement.setString(1, number1);
statement.setString(2, name1);
statement.setString(3, price1);
statement.setString(4, stock1);
int n = statement.executeUpdate();
if (n > 0) {
System.out.println("商品入库成功!");
} else {
System.out.println("商品入库失败!");
}
}
窗口事件处理
//添加事件处理
private void GoodsTypeAddActionEvent(ActionEvent e) {
String number= textField_number.getText();
String name = textField_name.getText();
String price= textField_price.getText();
String stock= textField_stock.getText();
if(StringUtil.isEmpty(number)) {
JOptionPane.showMessageDialog(null, "商品信息不能为空!");
return;
}
//JDBCSQLAdd add=new JDBCSQLAdd();
AddDao add=new AddDao();
try {
add.JDBCSQLAdd(number,name,price,stock);
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
JOptionPane.showMessageDialog(null, "商品已入库!", "标题",JOptionPane.PLAIN_MESSAGE);
this.resetValue();
this.Table(new Goods());
}
//初始化表格
public void Table(Goods goods) {
DefaultTableModel dtm=(DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbUtil.getCon();
ResultSet rs=queryDao.list1(con, goods);
while(rs.next()) {
Vector v =new Vector();
v.add(rs.getString ("Number"));
v.add(rs.getString ("Name"));
v.add(rs.getString ("Price"));
v.add(rs.getString("Stock"));
dtm.addRow(v);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
dbUtil.close(con);;
} catch (Exception e) {
e.printStackTrace();
}
}
}
//重置表单
private void resetValue(){
this.textField_number.setText("");
this.textField_name.setText("");
this.textField_price.setText("");
this.textField_stock.setText("");
}
4.查询商品
按名称查找
查找数据库SQL语句
public class QueryDao {
//按名称查找
public ResultSet list(Connection con,Goods goods) throws SQLException {
StringBuffer sb =new StringBuffer("select * from goodstable where Name like '%"+goods.getName()+"%'");
PreparedStatement pstmt=con.prepareStatement(sb.toString());
return pstmt.executeQuery();
}
窗口事件处理
//商品搜索事件处理
private void GoodsSearchActionPerformed(ActionEvent evt) {
String Name=this.textField_name.getText();
Goods goods=new Goods();
goods.setName(Name);
if(StringUtil.isEmpty(Name)) {
JOptionPane.showMessageDialog(null, "请输入商品名称!");
return;
}else {
this.resetValue();
this.Table(goods);
}
}
//初始化表格
public void Table(Goods goods) {
DefaultTableModel dtm=(DefaultTableModel) bookTypeTable.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con, goods);
while(rs.next()) {
Vector v =new Vector();
v.add(rs.getInt ("Number"));
v.add(rs.getString ("Name"));
v.add(rs.getString ("Price"));
v.add(rs.getString ("Stock"));
dtm.addRow(v);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
dbUtil.close(con);;
} catch (Exception e) {
e.printStackTrace();
}
}
}
//刷新表格
private void resetValue(){
this.textField_name.setText("");
}
5.商品出库
先按名称查找到需要删除的商品,随即删除
删除数据库SQL语句
public class DeleteDao {
public int delete(Connection con,Goods goods) throws SQLException {
String sql="DELETE FROM `goodstable` WHERE Name= ? ";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, goods.getName());
return pstmt.executeUpdate();
}
}
窗口事件处理
//删除事件处理
private void GoodsDeleteActionPerformed(ActionEvent e) {
String name= textField_name.getText();
textField_name.setText("");
DeleteDao d=new DeleteDao();
try {
d.JDBCSQLDelete(name);
JOptionPane.showConfirmDialog(null, "确定要删除该记录吗?");
} catch (SQLException e1) {
e1.printStackTrace();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
if(StringUtil.isEmpty(name)) {
JOptionPane.showMessageDialog(null, "商品出库成功!", "标题",JOptionPane.PLAIN_MESSAGE);
}else {
this.resetValue();
this.Table(new Goods());
}
}
//查找事件处理
private void GoodsSearchActionPerformed(ActionEvent e) {
String Name=this.textField_name.getText();
Goods goods=new Goods();
goods.setName(Name);
this.resetValue();
this.Table(goods);
}
//初始化表格
public void Table(Goods goods) {
DefaultTableModel dtm=(DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con, goods);
while(rs.next()) {
Vector v =new Vector();
v.add(rs.getString ("Number"));
v.add(rs.getString ("Name"));
v.add(rs.getString ("Price"));
v.add(rs.getString ("Stock"));
dtm.addRow(v);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
dbUtil.close(con);;
} catch (Exception e) {
e.printStackTrace();
}
}
}
//刷新表格
private void resetValue(){
this.textField_number.setText("");
this.textField_name.setText("");
this.textField_price.setText("");
this.textField_stock.setText("");
}
6.修改商品(最难
修改前要查找(按名称查找),点击表格获取商品信息,点击商品编号或者商品名称、商品单价、商品库存可以进行修改,修改成功自动保存并且修改数据库中的数据
修改数据库SQL语句
public class UpdateDao {
public int update(Connection con,Goods goods) throws SQLException {
String sql="update goodstable set Name=?,Price=?,Stock=? where Number=?";
PreparedStatement pstmt=con.prepareStatement(sql);
pstmt.setString(1, goods.getName());
pstmt.setString(2, goods.getPrice());
pstmt.setString(3, goods.getStock());
pstmt.setInt(4,goods.getNumber());
return pstmt.executeUpdate();
}
}
窗口事件处理
//修改事件
private void GoodsUpdateActionPerformed(ActionEvent e) {
String Number= textField_u_number.getText();
String Name = textField_u_name.getText();
String Price= textField_u_price.getText();
String Stock= textField_u_stock.getText();
if(StringUtil.isEmpty(Number)) {
JOptionPane.showMessageDialog(null, "请选择要修改的记录");
return;
}
Goods goods=new Goods(Integer.parseInt(Number),Name,Price,Stock);
Connection con=null;
try {
con=dbUtil.getCon();
int modifyNum=updateDao.update(con, goods);
if(modifyNum==1) {
JOptionPane.showMessageDialog(null, "修改成功");
this.resetValueUpdate();
this.fillTable1(new Goods());
}else {
JOptionPane.showMessageDialog(null, "修改失败");
}
} catch (Exception e1) {
e1.printStackTrace();
}
//查找事件
private void GoodsSearchActionPerformed(ActionEvent e) {
String Name=this.textField_s_name.getText();
Goods goods=new Goods();
goods.setName(Name);
this.resetValueQuery();
this.Table(goods);
}
//初始化查询表格
public void Table(Goods goods) {
DefaultTableModel dtm=(DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbUtil.getCon();
ResultSet rs=queryDao.list(con, goods);
while(rs.next()) {
Vector v =new Vector();
v.add(rs.getString ("Number"));
v.add(rs.getString ("Name"));
v.add(rs.getString ("Price"));
v.add(rs.getString("Stock"));
dtm.addRow(v);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
dbUtil.close(con);;
} catch (Exception e) {
e.printStackTrace();
}
}
}
//初始化修改表单
public void fillTable1(Goods goods) {
DefaultTableModel dtm=(DefaultTableModel) table.getModel();
dtm.setRowCount(0);
Connection con=null;
try {
con=dbUtil.getCon();
ResultSet rs=queryDao.list1(con, goods);
while(rs.next()) {
Vector v =new Vector();
v.add(rs.getString ("Number"));
v.add(rs.getString ("Name"));
v.add(rs.getString ("Price"));
v.add(rs.getString("Stock"));
dtm.addRow(v);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
dbUtil.close(con);;
} catch (Exception e) {
e.printStackTrace();
}
}
}
//重置更新表单
private void resetValueUpdate(){
this.textField_u_number.setText("");
this.textField_u_name.setText("");
this.textField_u_price.setText("");
this.textField_u_stock.setText("");
}
//重置查询表单
private void resetValueQuery(){
this.textField_s_name.setText("");
}
源码连接
链接: https://pan.baidu.com/s/1iM0kv1nmZsmWZcOIwEsAvQ?pwd=cikr 提取码: cikr 复制这段内容后打开百度网盘手机App,操作更方便哦
开发工具:eclipse
数据库:Access、连接ODBC