文章目录
前言
前一段时间利用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