这个项目结合数据库实现简单的数据的增删查改操作
项目结构如下:
项目效果:
可以实现基本的数据修改操作,下面是代码部分
Go_Shopping.java
package view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.*;
/**
* 主界面
*
*
*/
public class Go_Shopping extends JFrame{
//商场
Shop shop=new Shop();
//购物车
Shopping_cart cart=new Shopping_cart();
JPanel panel;
JButton button1,button2,button3;
public Go_Shopping() throws ClassNotFoundException, SQLException {
//主界面设计
shop.setVisible(false);//最开始进入主界面购物车和商场隐藏
cart.setVisible(false);
setTitle("Let's go shopping !!O(∩_∩)O");
setBounds(300, 300,500,300);
this.setLayout(new BorderLayout(10, 10));
panel = new JPanel(new GridLayout(1,3));
button1 = new JButton("商店");
button1.setFont(new java.awt.Font("黑体", 1, 30));
button1.setBackground(Color.yellow);
button1.setBorder(BorderFactory.createRaisedBevelBorder());
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shop.setVisible(true); //显示商店界面
setVisible(false); //隐藏主界面
}
});
button2 = new JButton("购物车");
button2.setFont(new java.awt.Font("黑体", 1, 30));
button2.setBackground(Color.pink);
button2.setBorder(BorderFactory.createRaisedBevelBorder());
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cart.setVisible(true); //显示购物车界面
setVisible(false); //隐藏主界面
}
});
button3 = new JButton("退出");
button3.setFont(new java.awt.Font("黑体", 1, 30));
button3.setBackground(Color.green);
button3.setBorder(BorderFactory.createRaisedBevelBorder());
button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0); //关闭
}
});
panel.add(button1);
panel.add(button2);
panel.add(button3);
this.add(panel,BorderLayout.CENTER);
//商店Shop界面添加返回按钮事件
shop.b5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(true); //显示主界面,隐藏商店界面
shop.setVisible(false);
try {
cart.updataUi(); //更新购物车数据
shop.updataUi(); //更新商店数据
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
}
});
//购物车Shopping_cart界面的返回按钮事件
cart.button3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(true); //显示主界面,隐藏购物车界面
cart.setVisible(false);
try {
cart.updataUi(); //更新购物车数据
shop.updataUi(); //更新商店数据
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
}
});
//购物车Shopping_cart界面的结算按钮事件
cart.button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
cart.updataUi(); //更新购物车数据
shop.updataUi(); //更新商店数据
JOptionPane.showConfirmDialog(null, "成功", "确认", JOptionPane.YES_NO_OPTION);
sql.Del();
cart.updataUi(); //更新购物车数据
shop.updataUi(); //更新商店数据
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
}
});
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws ClassNotFoundException, SQLException {
new Go_Shopping();
}
}
Shop.java
package view;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Shop extends JFrame implements ActionListener{
JButton b1,b2,b3,b4,b5;
JPanel p1;
JLabel[] label=new JLabel[4];
public Shop() throws ClassNotFoundException, SQLException {
setTitle("阳光衣橱");
setSize(500, 300);
setLocation(300, 300);
this.setLayout(new BorderLayout());
b1 = new JButton("耐克");
Button_beatiful.oneKeyBeautiful(b1, new view.GetImagIcon().getImagIcon("/photo/Nike.jpg"), 0);
b1.setFont(new java.awt.Font("黑体", 1, 0));
b1.setBorder(BorderFactory.createRaisedBevelBorder());
b2 = new JButton("阿迪");
Button_beatiful.oneKeyBeautiful(b2, new view.GetImagIcon().getImagIcon("/photo/adidas.jpg"), 0);
b2.setFont(new java.awt.Font("黑体", 1, 0));
b2.setBorder(BorderFactory.createRaisedBevelBorder());
b3 = new JButton("彪马");
Button_beatiful.oneKeyBeautiful(b3, new view.GetImagIcon().getImagIcon("/photo/puma.jpg"), 0);
b3.setBounds(0, 0, 100, 100);
b4 = new JButton("Dickies");
Button_beatiful.oneKeyBeautiful(b4, new view.GetImagIcon().getImagIcon("/photo/dick.jpg"), 0);
JPanel ps=new JPanel();
ps.setLayout(new FlowLayout(FlowLayout.CENTER, 0, (this.getHeight()-60)/4-ps.getHeight()));
ps.add(new JLabel("库存"));
JLabel j=new JLabel("10");
j.setForeground(Color.red);
ps.add(j);
ps.add(new JLabel("件"));
JPanel panel=new JPanel();
panel.add(ps);
p1 = new JPanel(new GridLayout(2, 4,5,60));
sql.initDate();
p1.add(b1);
label[0]=new JLabel();
label[0].setText("<html>库存<font color='red'>"+sql.shopnike+"</font>件</html>");
p1.add(label[0]);
p1.add(b2);
label[1]=new JLabel();
label[1].setText("<html>库存<font color='red'>"+sql.shopadi+"</font>件</html>");
p1.add(label[1]);
p1.add(b3);
label[2]=new JLabel();
label[2].setText("<html>库存<font color='red'>"+sql.shoppuma+"</font>件</html>");
p1.add(label[2]);
p1.add(b4);
label[3]=new JLabel();
label[3].setText("<html>库存<font color='red'>"+sql.shopdisk+"</font>件</html>");
p1.add(label[3]);
b1.addActionListener(this);
b1.setActionCommand(b1.getText());
b2.addActionListener(this);
b2.setActionCommand(b2.getText());
b3.addActionListener(this);
b3.setActionCommand(b3.getText());
b4.addActionListener(this);
b4.setActionCommand(b4.getText());
this.add(p1);
b5=new JButton("返回首页");
this.add(b5,BorderLayout.SOUTH);
Button_beatiful.oneKeyBeautiful(b5, new view.GetImagIcon().getImagIcon("/photo/mainPage.png"), 0);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(b5.getSize());
}
@Override
public void actionPerformed(ActionEvent e) {
String string=e.getActionCommand();
switch (string) {
case "耐克":
{
add("nike");
}
break;
case "阿迪":
{
add("adi");
}
break;
case "彪马":
{
add("puma");
}
break;
case "Dickies":
{
add("dick");
}
break;
}
try {
updataUi();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void add(String name) {
try {
String inputValue = JOptionPane.showInputDialog( "请输入你买的件数:");
if(!inputValue.equals("")) {
int number=Integer.parseInt(inputValue);
try {
sql.addCart(name, number);
} catch (ClassNotFoundException | SQLException e) {
System.out.println("输入有误");
}
}
} catch (Exception e) {
System.out.println("输入有误,请重新输入一个这正确的值");
JOptionPane.showMessageDialog(null, "输入有误", "请重新输入一个这正确的值", JOptionPane.ERROR_MESSAGE);
add(name);
}
}
//更新界面数据
public void updataUi() throws ClassNotFoundException, SQLException {
sql.initDate();
label[0].setText("<html>库存<font color='red'>"+sql.shopnike+"</font>件</html>");
label[1].setText("<html>库存<font color='red'>"+sql.shopadi+"</font>件</html>");
label[2].setText("<html>库存<font color='red'>"+sql.shoppuma+"</font>件</html>");
label[3].setText("<html>库存<font color='red'>"+sql.shopdisk+"</font>件</html>");
}
}
Shopping_cart.java
package view;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import UI.GameStart;
public class Shopping_cart extends JFrame implements ActionListener{
JButton button1,button3;
JTextArea t1;
JPanel panel1,panel2;
JButton b1,b2,b3,b4,b5;
JPanel p1;
JLabel[] label=new JLabel[4];
public void Shop() throws ClassNotFoundException, SQLException {
setTitle("阳光衣橱");
setSize(500, 300);
setLocation(300, 300);
this.setLayout(new BorderLayout());
b1 = new JButton("耐克");
Button_beatiful.oneKeyBeautiful(b1, new view.GetImagIcon().getImagIcon("/photo/Nike.jpg"), 0);
b1.setFont(new java.awt.Font("黑体", 1, 0));
b1.setBorder(BorderFactory.createRaisedBevelBorder());
b2 = new JButton("阿迪");
Button_beatiful.oneKeyBeautiful(b2, new view.GetImagIcon().getImagIcon("/photo/adidas.jpg"), 0);
b2.setFont(new java.awt.Font("黑体", 1, 0));
b2.setBorder(BorderFactory.createRaisedBevelBorder());
b3 = new JButton("彪马");
Button_beatiful.oneKeyBeautiful(b3, new view.GetImagIcon().getImagIcon("/photo/puma.jpg"), 0);
b3.setBounds(0, 0, 100, 100);
b4 = new JButton("Dickies");
Button_beatiful.oneKeyBeautiful(b4, new view.GetImagIcon().getImagIcon("/photo/dick.jpg"), 0);
JPanel ps=new JPanel();
ps.setLayout(new FlowLayout(FlowLayout.CENTER, 0, (this.getHeight()-60)/4-ps.getHeight()));
ps.add(new JLabel("库存"));
JLabel j=new JLabel("10");
j.setForeground(Color.red);
ps.add(j);
ps.add(new JLabel("件"));
JPanel panel=new JPanel();
panel.add(ps);
p1 = new JPanel(new GridLayout(2, 4,5,60));
sql.initDate();
p1.add(b1);
label[0]=new JLabel();
label[0].setText("<html>库存<font color='red'>"+sql.shopnike+"</font>件</html>");
p1.add(label[0]);
p1.add(b2);
label[1]=new JLabel();
label[1].setText("<html>库存<font color='red'>"+sql.shopadi+"</font>件</html>");
p1.add(label[1]);
p1.add(b3);
label[2]=new JLabel();
label[2].setText("<html>库存<font color='red'>"+sql.shoppuma+"</font>件</html>");
p1.add(label[2]);
p1.add(b4);
label[3]=new JLabel();
label[3].setText("<html>库存<font color='red'>"+sql.shopdisk+"</font>件</html>");
p1.add(label[3]);
b1.addActionListener(this);
b1.setActionCommand(b1.getText());
b2.addActionListener(this);
b2.setActionCommand(b2.getText());
b3.addActionListener(this);
b3.setActionCommand(b3.getText());
b4.addActionListener(this);
b4.setActionCommand(b4.getText());
this.add(p1);
b5=new JButton("返回首页");
this.add(b5,BorderLayout.SOUTH);
Button_beatiful.oneKeyBeautiful(b5, new view.GetImagIcon().getImagIcon("/photo/mainPage.png"), 0);
setVisible(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println(b5.getSize());
}
public Shopping_cart() throws ClassNotFoundException, SQLException {
Shop();
setTitle("购物车");
setSize(500, 300);
setLocation(300,300);
button1 = new JButton("结算");
button1.setFont(new java.awt.Font("黑体", 1, 25));
button1.setBackground(Color.pink);
button3 = new JButton("返回");
button3.setFont(new java.awt.Font("黑体", 1, 25));
button3.setBackground(Color.pink);
JPanel panel1=new JPanel();
panel1.add(button1);
panel1.add(button3);
this.add(panel1,BorderLayout.SOUTH);
this.add(p1,BorderLayout.CENTER);
updataUi();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String string=e.getActionCommand();
switch (string) {
case "耐克":
{
add("nike");
}
break;
case "阿迪":
{
add("adi");
}
break;
case "彪马":
{
add("puma");
}
break;
case "Dickies":
{
add("dick");
}
break;
}
try {
updataUi();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//修改数据库的值
public void add(String name) {
try {
String inputValue = JOptionPane.showInputDialog( "请输入你取消购买的件数:");
if(!inputValue.equals("")) {
int number=Integer.parseInt(inputValue);
try {
sql.addCart(name, -number);
} catch (ClassNotFoundException | SQLException e1) {
e1.printStackTrace();
}
}else {
System.out.println("输入数据有误");
}
} catch (Exception e) {
System.out.println("输入有误");
//JOptionPane.showConfirmDialog(null, "输入有误", "请重新输入一个这正确的值", JOptionPane.YES_OPTION);
JOptionPane.showMessageDialog(null, "输入有误", "请重新输入一个这正确的值", JOptionPane.ERROR_MESSAGE);
add(name);
}
}
//更新界面数据
public void updataUi() throws ClassNotFoundException, SQLException {
sql.initDate();
label[0].setText("<html>收藏<font color='red'>"+sql.nike+"</font>件</html>");
label[1].setText("<html>收藏<font color='red'>"+sql.adi+"</font>件</html>");
label[2].setText("<html>收藏<font color='red'>"+sql.puma+"</font>件</html>");
label[3].setText("<html>收藏<font color='red'>"+sql.disk+"</font>件</html>");
}
}
sql.java
package view;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class sql {
private static String user="root";
private static String password="123456";
private static String jdbc = "jdbc:mysql://127.0.0.1:3306/shop?characterEncoding=utf-8";//数据库链接链接
//数组得第一个代表adi在商城得总数量,以此类推 //初始化各商品的总量,都初始化为40件
static int alladi=40;
static int alldisk=40;
static int allnike=40;
static int allpuma=40;
static int[] array=new int[8];
static int adi = array[0];
static int disk = array[1];
static int nike = array[2];
static int puma = array[3];
static int shopadi = array[4];
static int shopdisk = array[5];
static int shopnike = array[6];
static int shoppuma = array[7];
static String sql;
//初始化数据
public static void initDate() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(jdbc, user, password);// 链接到数据库
sql="select * from shop"; // sql语句
Statement state = conn.createStatement();// 容器
ResultSet rs = state.executeQuery(sql); // 将sql语句传至数据库,返回的值为一个字符集用一个变量接收
for(int i=0;i<8;i++) {
if(rs.next())
array[i]=Integer.parseInt(rs.getString(2));
}
adi = array[0];
disk = array[1];
nike = array[2];
puma = array[3];
shopadi = array[4];
shopdisk = array[5];
shopnike = array[6];
shoppuma = array[7];
conn.close();// 关闭通道
}
//返回商品总数
public static int returnNumber(String string) {
switch (string) {
case "alladi":
return alladi;
case "alldisk":
return alldisk;
case "allnike":
return allnike;
case "allpuma":
return allpuma;
case "adi":
return adi;
case "disk":
return disk;
case "nike":
return nike;
case "puma":
return puma;
default:
return -1;
}
}
//更新数据库得总量
public static void updataAllGoods() {
alladi=shopadi;
alldisk=shopdisk;
allnike=shopnike;
allpuma=shoppuma;
}
//添加商品的方法
public static void addCart(String object,int number) throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(jdbc, user, password);// 链接到数据库
sql="select * from shop"; // sql语句
Statement state = conn.createStatement();// 容器
ResultSet rs = state.executeQuery(sql); // 将sql语句传至数据库,返回的值为一个字符集用一个变量接收
for(int i=0;i<8;i++) {
if(rs.next())
array[i]=Integer.parseInt(rs.getString(2));
}
adi = array[0];
disk = array[1];
nike = array[2];
puma = array[3];
shopadi = array[4];
shopdisk = array[5];
shopnike = array[6];
shoppuma = array[7];
System.out.println("输入的件数是:"+number);
int tep=0;
if(object.equals("nike")) {
tep=nike+number;
}
if(object.equals("adi")) {
adi+=number;
tep=adi;
}
if(object.equals("dick")) {
disk+=number;
tep=disk;
}
if(object.equals("puma")) {
puma+=number;
tep=puma;
}
sql="update shop set number="+tep+" where name="+"'"+object+"'";
System.out.println(sql);
state.executeUpdate(sql); // 将sql语句上传至数据库执行
sql="update shop set number="+(40-tep)+" where name="+"'"+"shop"+object+"'";
System.out.println(sql);
state.executeUpdate(sql); // 将sql语句上传至数据库执行
conn.close();// 关闭通道
}
public static void Del() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(jdbc, user, password);// 链接到数据库
sql="select * from shop"; // sql语句
Statement state = conn.createStatement();// 容器
ResultSet rs = state.executeQuery(sql); // 将sql语句传至数据库,返回的值为一个字符集用一个变量接收
int i;
String arr[] = {"nike","adi","puma","dick"};
for(i = 0;i<4;i++) {
sql="update shop set number="+0+" where name="+"'"+arr[i]+"'"; //购物车清空
System.out.println(sql);
state.executeUpdate(sql); // 将sql语句上传至数据库执行
sql="update shop set number="+(returnNumber("all"+arr[i])-returnNumber(arr[i]))+" where name="+"'"+"shop"+arr[i]+"'"; //仓库剩余多少
System.out.println(sql);
state.executeUpdate(sql); // 将sql语句上传至数据库执行
//修改总量
}
updataAllGoods();
conn.close();// 关闭通道
}
}
Button_beatiful.java
package view;
import java.awt.Font;
import java.awt.Insets;
import javax.swing.ImageIcon;
import javax.swing.JButton;
/**
* 这个类用于美化按钮组件
* @author 赵智林
*
*/
public class Button_beatiful {
public static void oneKeyBeautiful(JButton button,ImageIcon imgIcon,int height) {
button.setBorderPainted(false);
button.setFont(new Font("楷体",Font.BOLD,height));
button.setIcon(imgIcon);
//button.setIcon(new ImageIcon(getClass().getResource("/Img/start_game.png")));
button.setMargin(new Insets(0,0,0,0));
button.setIconTextGap(0);//将标签中显示的文本和图标之间的间隔量设置为0
button.setBorderPainted(false);//不打印边框
button.setBorder(null);//除去边框
button.setFocusPainted(false);//除去焦点的框
button.setContentAreaFilled(false);//除去默认的背景填充
}
}
GetImagIcon.java
package view;
import javax.swing.ImageIcon;
/**
* 这个类提供方法返回imagicon图像文件
* @author 赵智林
*
*/
public class GetImagIcon {
public ImageIcon getImagIcon(String path) {
return new ImageIcon(getClass().getResource(path));
}
}
GetImg.java
package view;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
/**
* 用于输入图片路径返回图像文件的工具类
* @author 赵智林
*
*/
//获取图片工具类
public class GetImg {
private GetImg() {
//私有构造方法,不能被实例
}
public static Image getImage(String path) {
URL u = GetImg.class.getClassLoader().getResource(path);
BufferedImage img = null;
try {
img = ImageIO.read(u);
}
catch(IOException e) {
e.printStackTrace();
}
if(img==null)System.out.println("图片空");
else System.out.println("图片不为空");
return img;
}
}
所用到的图片文件请自己准备下载,我自己的就不上传了,注意图片所在的位置,里面有两个获取图片的工具类,主义路径的书写格式。不然会报错,本文档的写法是没有错误的。