Add package assignment1
This commit is contained in:
1 parent
d6e67e683c
commit
112d58e3a0
10 files changed
+361
-4
No files matched your search
Vendored
+3
-4
@@ -1,7 +1,6 @@
|
||||
{
|
||||
"java.project.explorer.showNonJavaResources": false,
|
||||
"java.project.referencedLibraries": [
|
||||
"lib/**/*.jar",
|
||||
"excel/jxl.jar"
|
||||
],
|
||||
"java.project.explorer.showNonJavaResources": false
|
||||
"lib/**/*.jar"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package assignment1;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
//对字符串进行加密
|
||||
public class Encrypt {
|
||||
public static String MD5(String str) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md.update(str.getBytes());
|
||||
byte[] b = md.digest();
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (byte c : b) {
|
||||
int val = ((int) c) & 0xff;
|
||||
if (val < 16)
|
||||
sb.append("0");
|
||||
sb.append(Integer.toHexString(val));
|
||||
}
|
||||
return sb.toString().toUpperCase();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(MD5("123456"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package assignment1;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Scanner;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PasswordChange {
|
||||
static final String DBURL = "jdbc:mysql://localhost:3306/xiaoshou?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
Connection conn = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
conn = DriverManager.getConnection(DBURL);
|
||||
System.out.print("请输入用户名:");
|
||||
String username = scanner.nextLine();
|
||||
String checkUserSql = "SELECT xingming FROM t_yong WHERE yonghuming=?";
|
||||
pstmt = conn.prepareStatement(checkUserSql);
|
||||
pstmt.setString(1, username);
|
||||
rs = pstmt.executeQuery();
|
||||
if (!rs.next()) {
|
||||
System.out.println("该用户不存在,请重新输入");
|
||||
return;
|
||||
}
|
||||
System.out.print("请输入当前用户的原密码:");
|
||||
String oldPassword = scanner.nextLine();
|
||||
String checkPasswordSql = "SELECT xingming FROM t_yong WHERE yonghuming=? AND mima=MD5(?)";
|
||||
pstmt = conn.prepareStatement(checkPasswordSql);
|
||||
pstmt.setString(1, username);
|
||||
pstmt.setString(2, oldPassword);
|
||||
rs = pstmt.executeQuery();
|
||||
if (!rs.next()) {
|
||||
System.out.println("原密码输入不正确,请重新输入");
|
||||
return;
|
||||
}
|
||||
String newPassword;
|
||||
while (true) {
|
||||
System.out.print("请设置新的密码:");
|
||||
newPassword = scanner.nextLine();
|
||||
if (!isPasswordComplex(newPassword))
|
||||
System.out.println("您的密码不符合复杂性要求(密码长度不少于6个字符,至少有一个小写字母,至少有一个大写字母,至少一个数字),请重新输入:");
|
||||
else
|
||||
break;
|
||||
}
|
||||
System.out.print("请输入确认密码:");
|
||||
String confirmPassword = scanner.nextLine();
|
||||
if (!newPassword.equals(confirmPassword)) {
|
||||
System.out.println("两次输入的密码必须一致,请重新输入确认密码:");
|
||||
return;
|
||||
}
|
||||
String updatePasswordSql = "UPDATE t_yong SET mima=? WHERE yonghuming=?";
|
||||
pstmt = conn.prepareStatement(updatePasswordSql);
|
||||
pstmt.setString(1, Encrypt.MD5(newPassword));
|
||||
pstmt.setString(2, username);
|
||||
int result = pstmt.executeUpdate();
|
||||
if (result > 0)
|
||||
System.out.println("您已成功修改密码,请谨记");
|
||||
else
|
||||
System.out.println("密码修改失败,请重试");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
if (pstmt != null)
|
||||
pstmt.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPasswordComplex(String password) {
|
||||
if (password.length() < 6)
|
||||
return false;
|
||||
Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{6,}$");
|
||||
Matcher matcher = pattern.matcher(password);
|
||||
return matcher.matches();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package assignment1;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SupermarketManagement {
|
||||
private static final String DBURL = "jdbc:mysql://localhost:3306/xiaoshou?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int choice;
|
||||
try (Connection conn = DriverManager.getConnection(DBURL)) {
|
||||
do {
|
||||
System.out.println("===超市商品管理维护====");
|
||||
System.out.println("1、输入商品");
|
||||
System.out.println("2、查询商品");
|
||||
System.out.println("3、退出");
|
||||
System.out.print("请选择(1-3):");
|
||||
choice = scanner.nextInt();
|
||||
scanner.nextLine();
|
||||
switch (choice) {
|
||||
case 1:
|
||||
addProduct(scanner, conn);
|
||||
break;
|
||||
case 2:
|
||||
queryProduct(scanner, conn);
|
||||
break;
|
||||
case 3:
|
||||
System.out.println("退出系统");
|
||||
break;
|
||||
default:
|
||||
System.out.println("无效选择,请重新选择(1-3):");
|
||||
}
|
||||
} while (choice != 3);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static void addProduct(Scanner scanner, Connection conn) throws SQLException {
|
||||
System.out.print("请输入商品信息(格式:条形码,商品名称,单价,供应商):");
|
||||
String input = scanner.nextLine();
|
||||
String[] productInfo = input.split(",");
|
||||
String tiaoma = productInfo[0];
|
||||
PreparedStatement checkStmt = conn.prepareStatement("SELECT COUNT(*) FROM t_shangping WHERE tiaoma = ?");
|
||||
checkStmt.setString(1, tiaoma);
|
||||
ResultSet rs = checkStmt.executeQuery();
|
||||
rs.next();
|
||||
if (rs.getInt(1) > 0) {
|
||||
System.out.println("条形码不能重复,请重新输入");
|
||||
return;
|
||||
}
|
||||
PreparedStatement insertStmt = conn.prepareStatement(
|
||||
"INSERT INTO t_shangping (tiaoma, mingcheng, danjia, gongyingshang) VALUES (?, ?, ?, ?)");
|
||||
insertStmt.setString(1, tiaoma);
|
||||
insertStmt.setString(2, productInfo[1]);
|
||||
insertStmt.setBigDecimal(3, new BigDecimal(productInfo[2]));
|
||||
insertStmt.setString(4, productInfo[3]);
|
||||
insertStmt.executeUpdate();
|
||||
System.out.println("增加成功");
|
||||
}
|
||||
|
||||
private static void queryProduct(Scanner scanner, Connection conn) throws SQLException {
|
||||
System.out.print("请输入查询的商品名称:");
|
||||
String nameQuery = scanner.nextLine();
|
||||
PreparedStatement queryStmt = conn.prepareStatement("SELECT * FROM t_shangping WHERE mingcheng LIKE ?");
|
||||
queryStmt.setString(1, "%" + nameQuery + "%");
|
||||
ResultSet rs = queryStmt.executeQuery();
|
||||
System.out.println("满足条件的记录总共" + rs.getFetchSize() + "条,信息如下:");
|
||||
System.out.println("序号\t条形码\t商品名称\t单价\t供应商");
|
||||
int count = 1;
|
||||
while (rs.next()) {
|
||||
System.out.println(count++ + "\t" +
|
||||
rs.getString("tiaoma") + "\t" +
|
||||
rs.getString("mingcheng") + "\t" +
|
||||
rs.getBigDecimal("danjia") + "\t" +
|
||||
rs.getString("gongyingshang"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package assignment1;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class UserLogin {
|
||||
static final String DBURL = "jdbc:mysql://localhost:3306/xiaoshou?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT";
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
Connection conn = null;
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
conn = DriverManager.getConnection(DBURL);
|
||||
int attempts = 0;
|
||||
boolean loginSuccess = false;
|
||||
while (attempts < 3 && !loginSuccess) {
|
||||
System.out.println("欢迎使用阳光超市收银系统,请登陆");
|
||||
System.out.print("请输入用户名: ");
|
||||
String username = scanner.nextLine();
|
||||
System.out.print("请输入密码: ");
|
||||
String password = scanner.nextLine();
|
||||
String sql = "SELECT xingming FROM t_yong WHERE yonghuming=? AND mima=?";
|
||||
pstmt = conn.prepareStatement(sql);
|
||||
pstmt.setString(1, username);
|
||||
pstmt.setString(2, Encrypt.MD5(password));
|
||||
rs = pstmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
loginSuccess = true;
|
||||
String xingming = rs.getString("xingming");
|
||||
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
|
||||
String currentTime = formatter.format(new Date());
|
||||
System.out.println("欢迎使用阳光超市收银系统");
|
||||
System.out.println("当前收银员:" + xingming);
|
||||
System.out.println("当前时间:" + currentTime);
|
||||
} else {
|
||||
System.out.println("用户名或密码不正确,请重新输入");
|
||||
attempts++;
|
||||
}
|
||||
}
|
||||
if (!loginSuccess)
|
||||
System.out.println("最多只能尝试3次");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
if (pstmt != null)
|
||||
pstmt.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException se) {
|
||||
se.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
商品条码 商品名称 单价 供应商
|
||||
=========================
|
||||
100001 手机 4500.00 华为
|
||||
100002 鼠标 61.00 华为
|
||||
300001 运动鞋 485 耐克
|
||||
300002 衬衣 270 雅戈尔
|
||||
300003 牛仔裤 156.5 啄木鸟
|
||||
400001 电冰箱 4500 海尔
|
||||
400002 电视机 6999 长虹
|
||||
Binary file not shown.
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
Navicat MySQL Data Transfer
|
||||
|
||||
Source Server : localhost_3306
|
||||
Source Server Version : 80032
|
||||
Source Host : localhost:3306
|
||||
Source Database : xiaoshou
|
||||
|
||||
Target Server Type : MYSQL
|
||||
Target Server Version : 80032
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 2023-05-11 10:27:07
|
||||
*/
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_shangping
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_shangping`;
|
||||
CREATE TABLE `t_shangping` (
|
||||
`tiaoma` varchar(255) NOT NULL,
|
||||
`mingcheng` varchar(255) DEFAULT NULL,
|
||||
`danjia` decimal(10,2) DEFAULT NULL,
|
||||
`gongyingshang` varchar(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`tiaoma`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_shangping
|
||||
-- ----------------------------
|
||||
INSERT INTO `t_shangping` VALUES ('100001', '手机', '4500.00', '华为');
|
||||
INSERT INTO `t_shangping` VALUES ('100002', '鼠标', '61.00', '华为');
|
||||
INSERT INTO `t_shangping` VALUES ('100003', '矿泉水', '2.50', '农夫山泉');
|
||||
INSERT INTO `t_shangping` VALUES ('100004', '香烟', '20.00', '武汉卷烟厂');
|
||||
INSERT INTO `t_shangping` VALUES ('100005', '牙膏', '4.50', '中华牙膏厂');
|
||||
INSERT INTO `t_shangping` VALUES ('200001', '电脑', '4300.00', 'dell');
|
||||
INSERT INTO `t_shangping` VALUES ('200002', '小明同学', '5.50', '武汉饮料集团');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_shouyinmingxi
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_shouyinmingxi`;
|
||||
CREATE TABLE `t_shouyinmingxi` (
|
||||
`liushuihao` varchar(255) NOT NULL,
|
||||
`tiaoma` varchar(255) DEFAULT NULL,
|
||||
`mingcheng` varchar(255) DEFAULT NULL,
|
||||
`danjia` decimal(10,0) DEFAULT NULL,
|
||||
`shuliang` int DEFAULT NULL,
|
||||
`shouyinyuan` varchar(255) DEFAULT NULL,
|
||||
`xiaoshoushijian` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`liushuihao`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_shouyinmingxi
|
||||
-- ----------------------------
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for t_yonghu
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `t_yonghu`;
|
||||
CREATE TABLE `t_yonghu` (
|
||||
`yonghuming` varchar(255) NOT NULL,
|
||||
`mima` varchar(255) DEFAULT NULL,
|
||||
`xingming` varchar(255) DEFAULT NULL,
|
||||
`juese` varchar(255) DEFAULT NULL,
|
||||
PRIMARY KEY (`yonghuming`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of t_yonghu
|
||||
-- ----------------------------
|
||||
INSERT INTO `t_yonghu` VALUES ('niegang', null, '聂刚', '管理员');
|
||||
INSERT INTO `t_yonghu` VALUES ('wangyuxin', null, '王玉欣', '收银员');
|
||||
INSERT INTO `t_yonghu` VALUES ('xiaowang', null, '小王', '收银员');
|
||||
File renamed without changes.
Binary file not shown.
Reference in new issue
Block a user