序言: 学Java已经有几个月了,上一次总结是针对GUI界面写的简易计算器,实现加减乘除功能以及计算器标准型和科学型之间的转换,有兴趣可以看看 Java混合计算器以及界面切换 . 这次写的超市管理系统,实现的功能有账户的注册、登录,超市商品类别的添加、修改和删除以及商品的添加、修改和删除的功能。用户注册之后把注册信息导入数据库;用户登录时候查询用户表,方可登录进去;商品类别和商品的增加也如注册信息一样,把信息导入商品类别表和商品表。由于快期末考试了,超市管理系统的一些功能还没有完善,后续还会更新顾客登录超市系统后,只能拥有查询商品的权限,并且还可以实现购买商品的功能,哈~哈 ~…
1、首先是建立数据库表:
2、实现主页面:
3、实现用户注册:
核心代码:
publicstaticvoidmain(String args[]){
java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newRegister().setVisible(true);}});}privatevoidjButton1ActionPerformed(java.awt.event.ActionEvent evt){
String name=this.jTextField1.getText();
String age=this.jTextField3.getText();
String QQ=this.jTextField4.getText();
String userName=this.jTextField5.getText();
String password1=this.jPasswordField1.getText();
String password2=this.jPasswordField2.getText();if(StringUtil.isEmpty(name)){
JOptionPane.showMessageDialog(null,"姓名不能为空");return;}if(StringUtil.isEmpty(age)){
JOptionPane.showMessageDialog(null,"年龄不能为空");return;}if(StringUtil.isEmpty(QQ)){
JOptionPane.showMessageDialog(null,"QQ不能为空");return;}if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null,"注册账号不能为空");return;}if(StringUtil.isEmpty(password1)){
JOptionPane.showMessageDialog(null,"注册密码不能为空");return;}if(StringUtil.isEmpty(password2)){
JOptionPane.showMessageDialog(null,"确认密码不能为空");return;}if(!password1.equals(password2)){
JOptionPane.showMessageDialog(null,"两个密码填写不一致");return;}
User user=newUser(userName,password1);
Connection con=null;try{
con=dbUtil.getCon();int n=userDao.add(con,user);if(n==1){
JOptionPane.showMessageDialog(null,"用户注册成功!");}else{
JOptionPane.showMessageDialog(null,"注册失败!!");}}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"注册失败!!");}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//上一步privatevoidjButton2ActionPerformed(java.awt.event.ActionEvent evt){dispose();newHomePage().setVisible(true);}
注册成功之后可以返回上一步,重新登录
4、实现用户登录:
核心代码:
//重置privatevoidresetValueActionPerformed(ActionEvent evt){this.userNameTxt.setText("");this.passwordTxt.setText("");}//登录事件处理privatevoidloginActionPerformed(ActionEvent evt){
String userName=this.userNameTxt.getText();
String password=newString(this.passwordTxt.getPassword());if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null,"用户名不能为空!");return;}if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(null,"密码不能为空");return;}
User usr=newUser(userName,password);
Connection con=null;try{
con=dbUtil.getCon();
User currentUser=userDao.login(con,usr);if(currentUser!=null){dispose();newMainFrm().setVisible(true);}else{
JOptionPane.showMessageDialog(null,"登录失败,用户名密码错误!");}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}
接下来是商品类别和商品的实现,主要是在JFrame窗口中加入Inter窗口
5、主菜单之关于:
核心代码:
6、主菜单之商品类别管理:
核心代码:
privatevoidjButton1ActionPerformed(ActionEvent evt){
String goodsTypeName=this.goodsTypeNameTxt.getText();
String goodsTypeDesc=this.goodsTypeDescTxt.getText();if(StringUtil.isEmpty(goodsTypeName)){
JOptionPane.showMessageDialog(null,"商品类别不能为空");return;}
GoodsType goodsType=newGoodsType(goodsTypeName,goodsTypeDesc);
Connection con=null;try{
con=dbUtil.getCon();int n=goodsTypeDao.add(con,goodsType);if(n==1){
JOptionPane.showMessageDialog(null,"商品类别添加成功!");jButton2ActionPerformed(evt);}else{
JOptionPane.showMessageDialog(null,"添加失败!!");}}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"添加失败!!");}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//重置privatevoidjButton2ActionPerformed(java.awt.event.ActionEvent evt){this.goodsTypeNameTxt.setText("");this.goodsTypeDescTxt.setText("");}
//表格行点击事件privatevoidjTable1MousePressed(java.awt.event.MouseEvent evt){int row=jTable1.getSelectedRow();//获取行数
idTxt.setText((String)jTable1.getValueAt(row,0));
goodsTypeNameTxt.setText((String)jTable1.getValueAt(row,1));
goodsTypeDescTxt.setText((String)jTable1.getValueAt(row,2));}//鼠标单击后,删除privatevoidjButton3ActionPerformed(java.awt.event.ActionEvent evt){
String id=idTxt.getText();if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要删除的记录");return;}int n=JOptionPane.showConfirmDialog(null,"Are you 确定delete this记录?");if(n==0){
Connection con=null;try{
con=dbUtil.getCon();int deleteNum=goodsTypeDao.delect(con,id);//System.out.println(deleteNum);if(deleteNum==1){
JOptionPane.showMessageDialog(null,"删除成功");this.resetValue();this.fillTable(newGoodsType());}else{
JOptionPane.showMessageDialog(null,"删除失败");}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}}//鼠标单击后,修改privatevoidjButton2ActionPerformed(java.awt.event.ActionEvent evt){
String id=idTxt.getText();
String goodsTypeName=goodsTypeNameTxt.getText();
String goodsTypeDesc=goodsTypeDescTxt.getText();if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要修改的记录");return;}
GoodsType goodsType=newGoodsType(Integer.parseInt(id),goodsTypeName,goodsTypeDesc);
Connection con=null;try{
con=dbUtil.getCon();int modifyNum=goodsTypeDao.update(con,goodsType);if(modifyNum==1){
JOptionPane.showMessageDialog(null,"修改成功");this.resetValue();this.fillTable(newGoodsType());}else{
JOptionPane.showMessageDialog(null,"修改失败");}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//查询商品类型privatevoidjButton1ActionPerformed(java.awt.event.ActionEvent evt){
String t_goodsTypeName=this.jTextField2.getText();
GoodsType goodsType=newGoodsType();
goodsType.setGoodsTypeName(t_goodsTypeName);this.fillTable(goodsType);}//插入商品类型privatevoidfillTable(GoodsType goodsType){
DefaultTableModel dtm=(DefaultTableModel)jTable1.getModel();
dtm.setRowCount(0);
Connection con=null;try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,goodsType);while(rs.next()){
Vector v=newVector();
v.add(rs.getString("id"));
v.add(rs.getString("goodsTypeName"));
v.add(rs.getString("goodsTypeDesc"));
dtm.addRow(v);}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//修改后重置privatevoidresetValue(){this.idTxt.setText("");this.goodsTypeNameTxt.setText("");this.goodsTypeDescTxt.setText("");}
7、主菜单之商品管理:
核心代码:
//查询privatevoidjButton1ActionPerformed(java.awt.event.ActionEvent evt){
String t_goodsName=this.s_goodsNameTxt.getText();
Goods goods=newGoods();
goods.setGoodsName(t_goodsName);this.fillTable(goods);}//下拉框privatevoidfillGoodsType(String type){
Connection con=null;
GoodsType goodsType=null;try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,newGoodsType());if("search".equals(type)){
goodsType=newGoodsType();
goodsType.setGoodsTypeName("请选择");
goodsType.setId(-1);this.s_goodsJcb.addItem(goodsType);}while(rs.next()){
goodsType=newGoodsType();
goodsType.setGoodsTypeName(rs.getString("goodsTypeName"));
goodsType.setId(rs.getInt("id"));if("search".equals(type)){this.s_goodsJcb.addItem(goodsType);}elseif("modify".equals(type)){}}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//表privatevoidfillTable(Goods goods){
DefaultTableModel dtm=(DefaultTableModel) jTable1.getModel();
dtm.setRowCount(0);
Connection con=null;try{
con=dbUtil.getCon();
ResultSet rs=goodsDao.list(con,goods);while(rs.next()){
Vector v=newVector();
v.add(rs.getInt("id"));
v.add(rs.getString("goodsName"));
v.add(rs.getFloat("price"));
v.add(rs.getString("goodsDesc"));
dtm.addRow(v);}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//鼠标点击表格事件privatevoidjTable1MousePressed(java.awt.event.MouseEvent evt){int row=jTable1.getSelectedRow();this.idTxt.setText((Integer)jTable1.getValueAt(row,0)+"");this.goodsNameTxt.setText((String)jTable1.getValueAt(row,1));this.priceTxt.setText((Float)jTable1.getValueAt(row,2)+"");this.goodsDescTxt.setText((String)jTable1.getValueAt(row,3));}//鼠标单击后,修改privatevoidjButton2ActionPerformed(java.awt.event.ActionEvent evt){
String id=this.idTxt.getText();if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要修改的记录");return;}
String goodsName=this.goodsNameTxt.getText();
String price=this.priceTxt.getText();
String goodsDesc=this.goodsDescTxt.getText();if(StringUtil.isEmpty(goodsName)){
JOptionPane.showMessageDialog(null,"商品名称不能为空");return;}if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null,"商品价钱不能为空");return;}if(StringUtil.isEmpty(goodsDesc)){
JOptionPane.showMessageDialog(null,"商品描述不能为空");return;}
Goods goods=newGoods(Integer.parseInt(id),goodsName, Float.parseFloat(price),goodsDesc);
Connection con=null;try{
con=dbUtil.getCon();int modifyNum=goodsDao.update(con,goods);if(modifyNum==1){
JOptionPane.showMessageDialog(null,"修改成功");this.resetValue();this.fillTable(newGoods());}else{
JOptionPane.showMessageDialog(null,"修改失败");}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}//鼠标单击后删除privatevoidjButton3ActionPerformed(java.awt.event.ActionEvent evt){
String id=idTxt.getText();if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null,"请选择要删除的记录");return;}int n=JOptionPane.showConfirmDialog(null,"Are you 确定delete this记录?");if(n==0){
Connection con=null;try{
con=dbUtil.getCon();int deleteNum=goodsDao.delete(con,id);if(deleteNum==1){
JOptionPane.showMessageDialog(null,"删除成功");this.resetValue();this.fillTable(newGoods());}else{
JOptionPane.showMessageDialog(null,"删除失败");}}catch(Exception e){
e.printStackTrace();}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}}//修改后重置privatevoidresetValue(){this.idTxt.setText("");this.goodsNameTxt.setText("");this.goodsDescTxt.setText("");}
privatevoidjButton2ActionPerformed(ActionEvent evt){this.resetValue();}//商品添加privatevoidjButton1ActionPerformed(ActionEvent evt){
String goodsName=this.goodsNameTxt.getText();
String price=this.priceTxt.getText();
String goodsDesc=this.goodsDecTxt.getText();if(StringUtil.isEmpty(goodsName)){
JOptionPane.showMessageDialog(null,"商品类别名称不能为空");return;}if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null,"商品价格不能为空");return;}
GoodsType goodsType=(GoodsType)goodsTypeJcb.getSelectedItem();int goodsTypeId=goodsType.getId();
Goods goods=newGoods(goodsName,Float.parseFloat(price),goodsDesc);
Connection con=null;try{
con=dbUtil.getCon();int addNum=goodsDao.add(con,goods);if(addNum==1){
JOptionPane.showMessageDialog(null,"商品添加成功");resetValue();}else{
JOptionPane.showMessageDialog(null,"商品添加失败");}}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null,"商品添加失败");}finally{try{
dbUtil.closeCon(con);}catch(Exception e){
e.printStackTrace();}}}/**
* 重置表单
*/privatevoidresetValue(){this.goodsNameTxt.setText("");this.priceTxt.setText("");this.goodsDecTxt.setText("");if(this.goodsTypeJcb.getItemCount()>0){this.goodsTypeJcb.setSelectedIndex(0);}}//初始化商品类别下拉框privatevoidfillGoodsType(){
Connection con=null;
GoodsType goodsType=null;try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,newGoodsType());while(rs.next()){
goodsType=newGoodsType();
goodsType.setId(rs.getInt("id"));
goodsType.setGoodsTypeName(rs.getString("goodsTypeName"));this.goodsTypeJcb.addItem(goodsType);}}catch(Exception e){
e.printStackTrace();}finally{}}
8、退出:
版权归原作者 我菜就爱学 所有, 如有侵权,请联系我们删除。