Java小项目另一个水果摊

Posted 汤米先生

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java小项目另一个水果摊相关的知识,希望对你有一定的参考价值。


前言

前一段时间利用Java基础知识集合和IO流做了个简单的小项目-水果摊,感觉不过瘾,最近又想着用GUI和mysql数据库重做一下,名为另一个水果摊,下面就来分享一下代码吧

一、包和表截图

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、源代码

1.JDBC连接Mysql数据

管理员界面:增删查改

package com.vector.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.vector.dao.Fruit;
import com.vector.units.ConnectMsql;

public class FruitDao {

	public static String add(Fruit fruit) {
		String s = null;
		try {
			// Connection connection = ConnectMsql.getConnectMsql();
			String sql = "insert into fruit(name,price,number) values(?,?,0)";
			Connection connection = ConnectMsql.getConnectMsql();

			PreparedStatement ad = connection.prepareStatement(sql);

			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery("select * from fruit");
			int f = 0;
			while (rs.next()) {
				if (fruit.getName().equals(rs.getString("name"))) {
					f = 1;
				}
				/*
				 * System.out.println(rs.getInt("id") + " " + rs.getString("name") + " " +
				 * rs.getDouble("price") + " " + rs.getInt("number"));
				 */
			}

			if (f == 0) {
				ad.setString(1, fruit.getName());
				ad.setDouble(2, fruit.getPrice());
				int i = ad.executeUpdate();
				if (i > 0) {
					// System.out.println("添加成功!");
					s = "添加成功!";
				} else {
					// System.out.println("水果重复,添加失败!");
					s = "水果重复,添加失败!";
				}
			} else {
				s = "水果重复,添加失败!";
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
		}
		return s;

	}

	public static String delete(String name) {
		String sql = "delete from fruit where name = ?";
		String s1 = null;
		Connection connection = ConnectMsql.getConnectMsql();
		PreparedStatement dele = null;
		try {
			dele = connection.prepareStatement(sql);
			dele.setString(1, name);
			int i = dele.executeUpdate();
			if (i > 0) {
				s1 = "删除成功!";
			} else {
				s1 = "删除失败!未找到该水果!";
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return s1;
	}

	public static String change(String name, double price) {
		String sql = "update fruit set price= ? where name= ?";
		String s2 = null;
		try {
			Connection connection = ConnectMsql.getConnectMsql();
			PreparedStatement upda = connection.prepareStatement(sql);
			upda.setDouble(1, price);
			// upda.setInt(2, 2);
			upda.setString(2, name);
			// System.out.println(name);
			int i = upda.executeUpdate();
			if (i > 0) {
				s2 = " 修改成功!";
			} else {
				s2 = "修改失败,水果不存在!";
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return s2;

	}

	public static ResultSet list() {
		ResultSet rs = null;

		Connection connection = ConnectMsql.getConnectMsql();
		Statement stmt;
		try {
			stmt = connection.createStatement();
			rs = stmt.executeQuery("select * from fruit");

			/*
			 * while (rs.next()) { s="序号:"+rs.getInt("id") + " 水果名称:" + rs.getString("name")
			 * + " 水果价格:" + rs.getDouble("price") + " ";
			 * System.out.println("序号:"+rs.getInt("id") + " 水果名称:" + rs.getString("name") +
			 * " 水果价格:" + rs.getDouble("price") + " " );
			 * 
			 * }
			 */

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;

	}

}

用户界面 :查找水果

package com.vector.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.vector.dao.Fruit;
import com.vector.units.ConnectMsql;

public class UFruitDao {

	public static List<Fruit> list = new ArrayList<>();
	static Fruit f = null;

	public static List Uadd(String name, int number) {
		ResultSet rs = null;
		Connection connection = ConnectMsql.getConnectMsql();
		try {

			Statement stmt = connection.createStatement();
			rs = stmt.executeQuery("select * from fruit");
			int k = 0;
			while (rs.next()) {
				if (rs.getString("name").equals(name)) {
					f = new Fruit(rs.getString("name"), rs.getDouble("price"), number);
					k = 1;
				}

				/*
				 * System.out.println("序号:" + rs.getInt("id") + " 水果名称:" + rs.getString("name")
				 * + " 水果价格:" + rs.getDouble("price") + " ");
				 */
			}
			if (k == 0) {
				System.out.println("水果不纯在,添加失败!");
			}
			k = 0;
			for (Fruit s : list) {
				if (s.getName().equals(name)) {
					s.setNumber(s.getNumber() + number);
					k = 1;
				}

			}
			if (k == 0)
				list.add(f);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}
}

建立断开连接

package com.vector.units;

import java.sql.*;

public class ConnectMsql {
	public static Connection connect;

	public static Connection getConnectMsql() {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			// Class.forName("org.gjt.mm.mysql.Driver");
			// System.out.println("成功加载Mysql驱动程序!");
		} catch (Exception e) {
			System.out.print("加载Mysql驱动程序时出错!");
			e.printStackTrace();
		}
		try {
			connect = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/fru?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true",
					"root", "zpx");

			// System.out.println("成功连接Mysql服务器!");
			Statement stmt = connect.createStatement();
		} catch (Exception e) {
			System.out.print("获取连接错误!");
			e.printStackTrace();
		}
		return connect;
	}

	public static void closeConnection() {
		if (connect != null) {
			try {
				connect.close();
				// sSystem.out.println("数据库连接关闭");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}

2.GUI窗口界面

开始选择界面

package com.vector.view;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.vector.test.logon;
import com.vector.view.userInterface;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JTextArea;
import java.awt.Color;
import java.awt.CardLayout;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.SystemColor;

public class selInterface extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void star() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					selInterface frame = new selInterface();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public selInterface() {
		setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\\\eclip\\\\FruitV\\\\src\\\\tubiao.png")); //左上角图标
		setTitle("\\u6C34\\u679C\\u644A");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 600, 600);
		
		JMenuBar menuBar = new JMenuBar();
		menuBar.setToolTipText("");
		setJMenuBar(menuBar);
		
		JMenu menu = new JMenu("\\u9875\\u9762\\u9009\\u62E9");
		menuBar.add(menu);
		
		Monitor2 monitor = new Monitor2();
		JMenu menu_1 = new JMenu("\\u7BA1\\u7406\\u5458\\u754C\\u9762");
		menu_1.setIcon(new ImageIcon("D:\\\\eclip\\\\FruitV\\\\src\\\\556.png"));   //管理员图标
		menu.add(menu_1);
		
		
		JMenuItem menuItem = new JMenuItem("\\u767B\\u5F55");
		menu_1.add(menuItem);
		menuItem.addActionListener(monitor);
		
		
		
		JMenuItem menuItem_2 = new JMenuItem("\\u987E\\u5BA2\\u754C\\u9762");
		menuItem_2.setIcon(new ImageIcon("D:\\\\eclip\\\\FruitV\\\\src\\\\557.png"));    //登录图标
		menu.add(menuItem_2);
		menuItem_2.addActionListener(monitor);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel_1 = new JLabel("");
		lblNewLabel_1.setBounds(0, 71, 584, 465);
		lblNewLabel_1.setIcon(new ImageIcon("D:\\\\eclip\\\\FruitV\\\\src\\\\122.png"));     //顾客图标
		contentPane.add(lblNewLabel_1);
		
		JLabel label = new JLabel("\\u6B22\\u8FCE\\u6765\\u5230\\u6C34\\u679C\\u644A");
		label.setBounds(245, 0, 99, 71);
		contentPane.add(label);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("D:\\\\eclip\\\\FruitV\\\\src\\\\lan.png"));      //窗口上方蓝色方块
		lblNewLabel.setBackground(Color.GREEN);
		lblNewLabel.setBounds(0, 0, 584, 71);
		contentPane.add(lblNewLabel);
		
		
		ImageIcon ig = new ImageIcon("D:\\\\eclip\\\\FruitV\\\\src\\\\tubiao.png");   //左上角图标
		
		
		ImageIcon img = new ImageIcon("D:\\\\eclip\\\\FruitV\\\\src\\\\QQ图片20210420205633.png");     //窗口背景图
		JLabel imgLabel = new JLabel(img);
		this.getLayeredPane().add(imgLabel, new Integer(Integer.MIN_VALUE));
		imgLabel.setBounds(0, 0, img.getIconWidth(), img.getIconHeight());
	}
}
class Monitor2 implements ActionListener {
	public void actionPerformed(ActionEvent e) {
		//System.out.println("有响应");
		String buttonName = e.getActionCommand();
		微信小程序代码片段

java项目小手册

Java精品项目源码第81期在线水果商城系统

Java精品项目源码第68期前台+后台精品水果商城系统

Java精品项目源码前后端分离系统第25期水果生鲜商城系统

如何在单击 RecyclerView 项目时从一个片段移动到另一个片段