Java小项目另一个水果摊

本文档介绍了一个使用Java和Mysql实现的水果摊管理小项目。通过JDBC连接数据库,实现了管理员界面的增删查改功能和用户界面的水果查找功能。GUI窗口包括开始选择、购买水果、管理员登录等多个界面,详细描述了各部分代码实现和水果属性。
摘要由CSDN通过智能技术生成


前言

前一段时间利用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
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤米先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值