版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、JAVA程序设计JAVA程序设计学生:占 豪学号:1305110130武汉轻工大学数学与计算机学院计算机类1303班_JAVA程序设计课程设计一、SWING编程一、Swing介绍Swing是一个用于开发Java应用程序用户界面的开发工具包。 以抽象窗口工具包(AWT)为基础使跨平台应用程序可以使用任何可插拔的外观风格。使用Swing开发图形界面比AWT更加优秀,因为Swing是一种轻量级组件,它采用100%的Java实现,不再依赖与本地的图形界面,所以可以在所有平台保持相同的运行效果,对跨平台支持比较出色。并且,Swing提供了比AWT更多的图形组件,因此可以开发出更美观的图形界面。二、实验目
2、的使用Java做图形界面啦,了解图形用户界面基本组件的使用,布局管理器如何对组件进行管理以及事件处理机制的应用。三、实验内容1、实验任务使用Swing设计一个商品管理系统,实现对商店商品数据库的增、查、改、删的用户图形操作界面。商品Goods的四个属性为:(int)GoodsID、(String)GoodsName、(double)GoodsPrice、(String)GoodsDesc,分别表示商品的ID,名称,价格,描述。2、设计过程3、运行结果商品管理界面:添加商品界面修改商品界面功能:添加商品、查找商品、修改商品、删除商品添加商品: 查找商品:修改商品删除商品 四、附录程序代码Good
3、s:public class Goods/ TODO 自定义 Goods 类(商品)private int GoodsID;private String GoodsName;private double GoodsPrice;private String GoodsDesc;public Goods(int goodsID, String goodsName, double goodsPrice, String goodsDesc)/ TODO 自定义 Goods 构造方法GoodsID = goodsID;GoodsName = goodsName;GoodsPrice = goodsPri
4、ce;GoodsDesc = goodsDesc;public int getGoodsID()return GoodsID;public void setGoodsID(int goodsID)GoodsID = goodsID;public String getGoodsName()return GoodsName;public void setGoodsName(String goodsName)GoodsName = goodsName;public double getGoodsPrice()return GoodsPrice;public void setGoodsPrice(do
5、uble goodsPrice)GoodsPrice = goodsPrice;public String getGoodsDesc()return GoodsDesc;public void setGoodsDesc(String goodsDesc)GoodsDesc = goodsDesc;Overridepublic String toString() return "Goods GoodsID=" + GoodsID + ", GoodsName=" + GoodsName+ ", GoodsPrice=" + GoodsP
6、rice + ", GoodsDesc=" + GoodsDesc+ ""GoodsDao:import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import Model.Goods;import Utils.ConnectDB;public class GoodsDaoConnectDB conn = new ConnectDB();public void Add(Goods goods) throws SQLExceptionString s
7、ql = "INSERT INTO goods VALUES ("+ goods.getGoodsID() + ",'"+ goods.getGoodsName() + "',"+ goods.getGoodsPrice() + ",'"+ goods.getGoodsDesc() +"')"conn.insert(sql);public ArrayList<Goods> GetAll() throws SQLExceptionResultSet re
8、s = null;tryString sql = "SELECT * FROM goods"res = conn.select(sql);ArrayList<Goods> allgoods = new ArrayList<Goods>();while(res.next()Goods goods = new Goods(res.getInt("GoodsID"), res.getString("GoodsName"),res.getDouble("GoodsPrice"), res.getSt
9、ring("GoodsDesc");allgoods.add(goods);return allgoods;finallyif (res != null)res.close();public ArrayList<Goods> Search(String name) throws SQLExceptionResultSet res = null;tryString sql = "SELECT * FROM goods WHERE GoodsName = '" + name + "'"res = conn.se
10、lect(sql);ArrayList<Goods> somegoods = new ArrayList<Goods>();while(res.next()Goods goods = new Goods(res.getInt("GoodsID"), res.getString("GoodsName"),res.getDouble("GoodsPrice"), res.getString("GoodsDesc");somegoods.add(goods);return somegoods;fi
11、nallyif (res != null)res.close();public void Modify(int id, Goods goods) throws SQLExceptionString sql = "UPDATE goods SET "+ "GoodsID = " + goods.getGoodsID() + ","+ "GoodsName = '" + goods.getGoodsName() + "',"+ "GoodsPrice = " +
12、goods.getGoodsPrice() + ","+ "GoodsDesc = '" + goods.getGoodsDesc() + "'"+ "WHERE GoodsID = '" + id +"'"conn.update(sql);public void Remove(Goods goods) throws SQLExceptionString sql = "DELETE FROM goods WHERE GoodsID = '&quo
13、t; + goods.getGoodsID() + "'"conn.delete(sql);ConnectDB:import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class ConnectDBprivate Connection conn;private Statement stmt;public Connection getCo
14、nn()String driver = "com.mysql.jdbc.Driver"String url = "jdbc:mysql:/:3306/store"String user = "Housz"String pass = "123456"tryClass.forName(driver);return DriverManager.getConnection(url, user, pass);catch (Exception e)System.out.println("-MySQL
15、 连接失败 -");e.printStackTrace();return null;public ResultSet select(String sql) throws SQLException/ TODO 在数据库中查询数据conn = getConn();stmt = conn.createStatement();ResultSet rs = stmt.executeQuery(sql);return rs;public void insert(String sql) throws SQLException/ TODO 在数据库中插入数据tryconn = getConn();s
16、tmt = conn.createStatement();stmt.executeUpdate(sql);finallyif (stmt != null)stmt.close();if (conn != null)conn.close();public void update(String sql) throws SQLException/ TODO 在数据库中修改数据tryconn = getConn();stmt = conn.createStatement();stmt.executeUpdate(sql);finallyif (stmt != null)stmt.close();if
17、(conn != null)conn.close();public void delete(String sql) throws SQLException/ TODO 在数据库中删除数据tryconn = getConn();stmt = conn.createStatement();stmt.executeUpdate(sql);finallyif (stmt != null)stmt.close();if (conn != null)conn.close();ManageGUI:import java.awt.event.ActionEvent;import java.awt.event.
18、ActionListener;import java.sql.SQLException;import java.util.ArrayList;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.WindowConstants;import javax.swing.table.DefaultTableModel;import Dao.Good
19、sDao;import Model.Goods;public class ManageGUI implements ActionListenerprivate JFrame frame;private JPanel panel;private JScrollPane JSP;private JTable Table;private DefaultTableModel Model;private JButton add;private JButton search;private JButton update;private JButton delete;private ArrayList<
20、;Goods> goodsList;public void Init()frame = new JFrame();frame.setTitle("商品管理");frame.setLocation(205, 150);frame.setSize(480, 400);panel = new JPanel();panel.setLayout(null);Object columnTitle = "编号", "名称", "价格", "描述" ;Table = new JTable();Model
21、= new DefaultTableModel(new String , columnTitle);Table.setModel(Model);JSP = new JScrollPane(Table);JSP.setBounds(5, 5, 455, 275);panel.add(JSP);frame.add(panel);add = new JButton();search = new JButton();update = new JButton();delete = new JButton();panel.add(add);panel.add(search);panel.add(updat
22、e);panel.add(delete);add.setText("添加商品");search.setText("查找商品");update.setText("修改商品");delete.setText("删除商品");add.setBounds(22, 305, 90, 25);search.setBounds(132, 305, 90, 25);update.setBounds(242, 305, 90, 25);delete.setBounds(352, 305, 90, 25);add.addActionL
23、istener(this);add.setActionCommand("add");search.addActionListener(this);search.setActionCommand("search");update.addActionListener(this);update.setActionCommand("update");delete.addActionListener(this);delete.setActionCommand("delete");frame.setDefaultCloseOp
24、eration(WindowConstants.DISPOSE_ON_CLOSE);frame.setVisible(true);public static void main(String args)/ TODO Auto-generated method stubnew ManageGUI().Init();Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stubString cmd = e.getActionCommand();switch (cmd)case "sea
25、rch":search();break;case "add":new AddGUI().Init();break;case "update":update();break;case "delete":delete();break;public void search()try goodsList = new GoodsDao().GetAll(); catch (SQLException e) e.printStackTrace();for (int i = Model.getRowCount() - 1; i >=
26、0; i-) Model.removeRow(i);for (int i = 0; i < goodsList.size(); i+) Object row = goodsList.get(i).getGoodsID(), goodsList.get(i).getGoodsName(),goodsList.get(i).getGoodsPrice(), goodsList.get(i).getGoodsDesc() ;Model.addRow(row);public void update()int selectedRow = Table.getSelectedRow();if (sel
27、ectedRow != -1)Goods goods = new Goods(int) Table.getValueAt(selectedRow, 0),(String) Table.getValueAt(selectedRow, 1),(double) Table.getValueAt(selectedRow, 2),(String) Table.getValueAt(selectedRow, 3);new UpdateGUI().Init(goods);elsenew WarnGUI("请先在主版面表格中选择一项商品信息!");public void delete()i
28、nt selectedRow = Table.getSelectedRow();if (selectedRow != -1)Goods goods = new Goods(int) Table.getValueAt(selectedRow, 0),(String) Table.getValueAt(selectedRow, 1),(double) Table.getValueAt(selectedRow, 2),(String) Table.getValueAt(selectedRow, 3);try new GoodsDao().Remove(goods); catch (SQLExcept
29、ion e) e.printStackTrace();Model.removeRow(selectedRow);elsenew WarnGUI("请先在主版面表格中选择一项商品信息!");AddGUI:import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import ja
30、vax.swing.JPanel;import javax.swing.JTextField;import javax.swing.WindowConstants;import Dao.GoodsDao;import Model.Goods;public class AddGUIpublic void Init()JDialog AddJD = new JDialog();JPanel AddJP = new JPanel();JLabel IDJL = new JLabel("产品ID");JLabel nameJL = new JLabel("产品名"
31、;);JLabel priceJL = new JLabel("价格");JLabel descJL = new JLabel("描述");JTextField ID = new JTextField();JTextField name = new JTextField();JTextField price = new JTextField();JTextField desc = new JTextField();JButton submit = new JButton("提交");JButton cancel = new JButt
32、on("取消");tryAddJD.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);AddJD.setVisible(true);AddJD.setTitle("添加商品");AddJD.setLocation(685, 150);AddJD.setSize(480, 320);AddJP.setLayout(null);AddJD.add(AddJP);AddJP.add(IDJL);AddJP.add(nameJL);AddJP.add(priceJL);AddJP.add(des
33、cJL);AddJP.add(ID);AddJP.add(name);AddJP.add(price);AddJP.add(desc);AddJP.add(submit);AddJP.add(cancel);IDJL.setBounds(50, 45, 75, 15);nameJL.setBounds(50, 85, 75, 15);priceJL.setBounds(50, 125, 75, 15);descJL.setBounds(50, 165, 75, 15);ID.setBounds(108, 40, 250, 30);name.setBounds(108, 80, 250, 30)
34、;price.setBounds(108, 120, 250, 30);desc.setBounds(108, 160, 250, 30);submit.setBounds(108, 210, 100, 30);cancel.setBounds(258, 210, 100, 30);submit.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent eve)/ TODO Auto-generated method stubGoods goods = new Goods(Inte
35、ger.parseInt(ID.getText(), name.getText(), Float.parseFloat(price.getText(), desc.getText();trynew GoodsDao().Add(goods);catch (SQLException e)e.printStackTrace();AddJD.dispose(););cancel.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent eve)/ TODO Auto-generated
36、method stubAddJD.dispose(););catch (Exception e)e.printStackTrace();UpdateGUIimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.SQLException;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swin
37、g.JTextField;import javax.swing.WindowConstants;import Dao.GoodsDao;import Model.Goods;public class UpdateGUI public void Init(Goods go)/ TODO “修改商品信息”界面的初始化JDialog JD = new JDialog();JPanel UpdateJP = new JPanel();JLabel IDJL = new JLabel("产品ID");JLabel nameJL = new JLabel("产品名"
38、);JLabel priceJL = new JLabel("价格");JLabel descJL = new JLabel("描述");JTextField ID = new JTextField();JTextField name = new JTextField();JTextField price = new JTextField();JTextField desc = new JTextField();JButton submit = new JButton("提交");JButton cancel = new JButto
39、n("取消");tryJD.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);JD.setVisible(true);JD.setTitle("修改商品信息");JD.setLocation(685, 150);JD.setSize(480, 320);UpdateJP.setLayout(null);JD.add(UpdateJP);UpdateJP.add(IDJL);UpdateJP.add(nameJL);UpdateJP.add(priceJL);UpdateJP.add(de
40、scJL);UpdateJP.add(ID);UpdateJP.add(name);UpdateJP.add(price);UpdateJP.add(desc);UpdateJP.add(submit);UpdateJP.add(cancel);IDJL.setBounds(50, 45, 75, 15);nameJL.setBounds(50, 85, 75, 15);priceJL.setBounds(50, 125, 75, 15);descJL.setBounds(50, 165, 75, 15);ID.setBounds(108, 40, 250, 30);name.setBound
41、s(108, 80, 250, 30);price.setBounds(108, 120, 250, 30);desc.setBounds(108, 160, 250, 30);submit.setBounds(108, 210, 100, 30);cancel.setBounds(258, 210, 100, 30);submit.addActionListener(new ActionListener()Overridepublic void actionPerformed(ActionEvent ev) / TODO Auto-generated method stubint id =
42、go.getGoodsID();Goods goods = new Goods(Integer.parseInt(ID.getText(), name.getText(), Float.parseFloat(price.getText(), desc.getText();/System.out.println(goods + " - name = " + name);trynew GoodsDao().Modify(id, goods);catch (SQLException e)e.printStackTrace();JD.dispose(););cancel.addAc
43、tionListener(new ActionListener() Overridepublic void actionPerformed(ActionEvent e)/ TODO Auto-generated method stubJD.dispose(););catch (Exception e)e.printStackTrace();课程设计二、SERVLET编程1.实验截图/product_input/product_save数据库数据项目截图:2核心代码-SaveProduct.java-package cn.whpu.action;import java.sql.Connectio
44、n;import java.sql.DriverManager;import java.sql.SQLException;import java.sql.Statement;import cn.whpu.model.Product;public class SaveProduct public void save(Product product) Connection conn = null;Statement stat = null;try conn = getConnection();stat = conn.createStatement();String sql = "inse
45、rt into product (name,description,price) values('"+ product.getName()+ "','"+ product.getDescription()+ "'," + product.getPrice() + ")"stat.execute(sql); catch (SQLException e) e.printStackTrace(); finally try if (stat != null)stat.close();if (conn
46、!= null)conn.close(); catch (SQLException e) e.printStackTrace();public static Connection getConnection() String dbDriver = "com.mysql.jdbc.Driver"String username = "root"String password = "123456"String url = "jdbc:mysql:/localhost:3306/czj"Connection conn =
47、null;try Class.forName(dbDriver);conn = DriverManager.getConnection(url, username, password); catch (SQLException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace();return conn;- ControllerServlet.java-import java.io.IOException;import javax.servlet.RequestDispatcher;import
48、 javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import cn.whpu.action.SaveProduct;import cn.whpu.form.ProductForm;import cn.whpu.model.Product;W
49、ebServlet(name = "ControllerServlet", urlPatterns = "/product_input","/product_save" )public class ControllerServlet extends HttpServlet private static final long serialVersionUID = 1579L;Overridepublic void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException process(request, response);Overridepublic void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException process(request, r
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中职数学基础模块下册直线与圆的位置关系课件
- 二零二五年度水电工程安全生产标准化建设承包合同汇编4篇
- 二零二五年度校企合作实习基地人才培养协议4篇
- 二零二五年度房地产租赁合同补充协议4篇
- 2025年度美容院店铺租赁合同范本(含违约责任及赔偿条款)2篇
- 道路货物运输的优化和效率提升方法
- 大型钢桁节段梁成套架设中的质量控制与保证体系研究
- 二零二五年度智能车间承包项目投资协议4篇
- 二零二五年度智能化车间承包经营合作合同范本4篇
- 2025年摩托车维修配件供应链合作协议4篇
- 2025年湖北武汉工程大学招聘6人历年高频重点提升(共500题)附带答案详解
- 【数 学】2024-2025学年北师大版数学七年级上册期末能力提升卷
- GB/T 26846-2024电动自行车用电动机和控制器的引出线及接插件
- 辽宁省沈阳市皇姑区2024-2025学年九年级上学期期末考试语文试题(含答案)
- 2024年国家工作人员学法用法考试题库及参考答案
- 妊娠咳嗽的临床特征
- 国家公务员考试(面试)试题及解答参考(2024年)
- 《阻燃材料与技术》课件 第6讲 阻燃纤维及织物
- 2024年金融理财-担保公司考试近5年真题附答案
- 泰山产业领军人才申报书
- 高中语文古代文学课件:先秦文学
评论
0/150
提交评论