首页 > 数据库 >9.12将仓库管理系统改为数据库实现

9.12将仓库管理系统改为数据库实现

时间:2023-09-12 19:15:25浏览次数:52  
标签:String 管理系统 resultSet 数据库 9.12 System sc println out

今天使用IDEA完成了仓库管理系统的连接数据库MYSQL,基本实现了增,删,改,查。

import java.sql.*;
import java.util.Scanner;

//11111111 华为 华为公司 20230101 20230102 111 00001121 10 5
//22222222 小米 小米公司 20230102 20230202 222 00001122 11 4
//33333333 魅族 魅族公司 20230103 20230302 333 00001123 12 3
//44444444 京东 京东公司 20230104 20230402 444 00001124 13 2
//55555555 淘宝 淘宝公司 20230105 20230502 555 00001125 14 1
public class Main {
public static void main(String[] args) throws SQLException {
Scanner sc = new Scanner(System.in);
while (true) {
show();
System.out.print("请输入数字:");
int choice = sc.nextInt();
switch (choice) {
case 1 -> addshop();
case 2 -> setshop();
case 3 -> outshop();
case 4 -> showAllshop();
case 5 -> deleteshop();
case 6 -> System.exit(0);
default -> System.out.println("该选项不存在");
}
}
}

public static void show() {
System.out.println("***********************************************************");
System.out.println(" 石家庄铁道大学前进22软件开发有限公司 ");
System.out.println(" 仓库管理系统2022版 ");
System.out.println("***********************************************************");
System.out.println(" 1、商品入库管理 ");
System.out.println(" 2、商品信息修改 ");
System.out.println(" 3、商品出库管理 ");
System.out.println(" 4、仓库盘点管理 ");
System.out.println(" 5、删除商品信息 ");
System.out.println(" 6、关闭系统 ");
System.out.println("**********************************************************");
}

public static void show1(String itemno1, String itemname1, String shipmenttime1, String suppliername1, String warehousenumber1, String warehouseplace1, int itemnumber1) {
System.out.println("***********************************************************");
System.out.println(" 石家庄铁道大学前进22软件开发有限公司 ");
System.out.println(" 仓库管理系统2022版 ");
System.out.println("***********************************************************");
System.out.println(" 1商品编号:" + itemno1);
System.out.println(" 商品名称:" + itemname1);
System.out.println(" 供货商信息:" + shipmenttime1);
System.out.println(" 入库时间:" + suppliername1);
System.out.println(" 存放仓库号:" + warehousenumber1);
System.out.println(" 存放位置信息:" + warehouseplace1);
System.out.println(" 入库商品数量:" + itemnumber1);
}

public static void show2() {
System.out.println("***********************************************************");
System.out.println(" 石家庄铁道大学前进22软件开发有限公司 ");
System.out.println(" 仓库管理系统2022版 ");
System.out.println("***********************************************************");
System.out.println(" 请输入商品编号:XXXXXXXX ");
System.out.println("***********************************************************");
}


public static int addshop() throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "用户名", "密码");
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入相关信息:");
System.out.println("1商品编号2商品名称3供货商名称4入库时间5出库时间6仓库编号7存放商品的具体位置8入库商品的数量9出库商品的数量");
String itemno1 = sc.next();
String itemname1 = sc.next();
String suppliername1 = sc.next();
String warehousingtime1 = sc.next();
String shipmenttime1 = sc.next();
String warehousenumber1 = sc.next();
String warehouseplace1 = sc.next();
int itemnumber1 = sc.nextInt();
int outnumber1 = sc.nextInt();
if (!itemno1.matches("\\d{8}") || !warehousingtime1.matches("\\d{8}") || !warehousenumber1.matches(".{3}") || !shipmenttime1.matches("\\d{8}")) {
System.out.println("信息错误,请重新输入");
continue;
}
if (shipmenttime1.compareTo(warehousingtime1) < 0 || warehouseplace1.matches("^\\d{4}YY\\d{2}$")) {
System.out.println("信息错误,请重新输入");
continue;
}
String insertQuery0 = "Select itemno,count(*) from shop where itemno = ? ";
PreparedStatement preparedStatement0 = connection.prepareStatement(insertQuery0);
preparedStatement0.setString(1, itemno1);
ResultSet resultSet = preparedStatement0.executeQuery();
if (resultSet.next()) {
int itemCount = resultSet.getInt(2);
System.out.println("找到了 " + itemCount + " 条符合要求的数据。");
if (itemCount != 0) {
System.out.println("商品编号存在请重新输入");
continue;
}
}
show1(itemno1, itemname1, shipmenttime1, suppliername1, warehousenumber1, warehouseplace1, itemnumber1);
System.out.println("该商品入库操作已完成,是否提交(Y/N)");
System.out.println("**********************************************************");
String choice1 = sc.next();
if (choice1.equals("N")) {
continue;
}
if (choice1.equals("Y")) {
String insertQuery = "INSERT INTO shop (itemno, itemname, suppliername, warehousingtime, shipmenttime, warehousenumber, warehouseplace, itemnumber, outnumber) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
preparedStatement.setString(1, itemno1);
preparedStatement.setString(2, itemname1);
preparedStatement.setString(3, suppliername1);
preparedStatement.setString(4, warehousingtime1);
preparedStatement.setString(5, shipmenttime1);
preparedStatement.setString(6, warehousenumber1);
preparedStatement.setString(7, warehouseplace1);
preparedStatement.setInt(8, itemnumber1);
preparedStatement.setInt(9, outnumber1);
int rowsAffected = preparedStatement.executeUpdate();
if (rowsAffected > 0) {
System.out.println("商品录入成功");
} else {
System.out.println("商品录入失败");
}
return 0;
}
}
}

public static int setshop() throws SQLException {
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "用户名", "密码");
Scanner sc = new Scanner(System.in);
while (true) {
show2();
System.out.println("请输入八位商品编号:");
String itemno2 = sc.next();
String insertQuery0 = "Select itemno,count(*) from shop where itemno = ? ";
PreparedStatement preparedStatement0 = connection.prepareStatement(insertQuery0);
preparedStatement0.setString(1, itemno2);
ResultSet resultSet = preparedStatement0.executeQuery();
if (resultSet.next()) {
int itemCount = resultSet.getInt(2);
System.out.println("找到了 " + itemCount + " 条符合要求的数据。");
if (itemCount == 0) {
System.out.println("请重新输入");
continue;
}
}
resultSet.close();
preparedStatement0.close();
System.out.println(" 请选择需要修改的信息编号(1-7):");
System.out.println("***********************************************************");
int choice2 = sc.nextInt();
if (choice2 > 7 || choice2 < 1) {
System.out.println("该选项不存在");
break;
}
String choice3[] = {"商品编号", "商品名称", "供货商信息", "入库时间", "存放仓库号", "存放位置信息", "商品数量"};
System.out.print("请输入修改后的" + choice3[choice2 - 1] + ":");
String s = sc.next();
System.out.println("***********************************************************");
System.out.println("请输入Y/N:");
String s2 = sc.next();
String shop[] = {"itemno", "itemname", "suppliername", "warehousingtime", "warehousenumber", "warehouseplace", "itemnumber"};
if (s2.equals("Y")) {
String insertQuery1 = "UPDATE shop SET " + shop[choice2 - 1] + " = ? where itemno = ? ";
PreparedStatement preparedStatement1 = connection.prepareStatement(insertQuery1);
if (choice2 != 7) {
preparedStatement1.setString(1, s);
} else {
preparedStatement1.setInt(1, Integer.parseInt(s));
}
preparedStatement1.setString(2, itemno2);
preparedStatement1.executeUpdate();
preparedStatement1.close();
System.out.println("商品修改成功");
break;
}
}
return 0;
}


public static int outshop() throws SQLException {
Scanner sc = new Scanner(System.in);
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "用户名", "密码");
while (true) {
System.out.println("请输入商品编号");
String itemno2 = sc.next();
String insertQuery0 = "Select itemno,count(*) from shop where itemno = ? ";
PreparedStatement preparedStatement0 = connection.prepareStatement(insertQuery0);
preparedStatement0.setString(1, itemno2);
ResultSet resultSet = preparedStatement0.executeQuery();
if (resultSet.next()) {
int itemCount = resultSet.getInt(2);
System.out.println("找到了 " + itemCount + " 条符合要求的数据。");
if (itemCount == 0) {
System.out.println("没有符合要求的编号请重新输入");
continue;
}
}
preparedStatement0.close();
System.out.println("请输入出库时间和数量:");
String outtime = sc.next();
int outshop = sc.nextInt();
String selectQuery = "SELECT warehousingtime,outnumber,itemnumber from shop where itemno = ?";
PreparedStatement preparedStatement1 = connection.prepareStatement(selectQuery);
preparedStatement1.setString(1, itemno2);
ResultSet resultSet1 = preparedStatement1.executeQuery();
preparedStatement1.close();
if (resultSet1.next()) {
int column1Value9 = resultSet1.getInt("outnumber");
int column1Value10 = resultSet1.getInt("itemnumber");
String column1Value11 = resultSet1.getString("warehousingtime");
if (column1Value9 + outshop - column1Value10 > 0) {
System.out.println("出库数量过多请重新输入全部信息");
continue;
}
if (column1Value11.compareTo(outtime) > 0) {
System.out.println("时间不正常请重新输入全部信息");
continue;
}
String insertQuery1 = "update shop set outnumber = ?,shipmenttime = ? where itemno = ? ";
PreparedStatement preparedStatement2 = connection.prepareStatement(insertQuery1);
preparedStatement2.setInt(1, column1Value9 + outshop);
preparedStatement2.setString(2, outtime);
preparedStatement2.setString(3, itemno2);
preparedStatement2.executeUpdate();
preparedStatement2.close();
System.out.println("出库成功");
}
return 0;
}
}


public static void showAllshop() {
System.out.println("数据如下");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
// 建立数据库连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "用户名", "密码");
// 创建并执行查询语句
String selectQuery = "SELECT * FROM shop";
preparedStatement = connection.prepareStatement(selectQuery);
resultSet = preparedStatement.executeQuery();
// 遍历结果集并打印内容
while (resultSet.next()) {
String column1Value1 = resultSet.getString("itemno"); // 替换成实际的列名
String column1Value2 = resultSet.getString("itemname"); // 替换成实际的列名
String column1Value3 = resultSet.getString("suppliername"); // 替换成实际的列名
String column1Value4 = resultSet.getString("warehousingtime"); // 替换成实际的列名
String column1Value5 = resultSet.getString("shipmenttime"); // 替换成实际的列名
String column1Value6 = resultSet.getString("warehousenumber"); // 替换成实际的列名
String column1Value7 = resultSet.getString("warehouseplace"); // 替换成实际的列名
int column1Value8 = resultSet.getInt("itemnumber"); // 替换成实际的列名
int column1Value9 = resultSet.getInt("outnumber"); // 替换成实际的列名
System.out.println(column1Value1 + " " + column1Value2 + " " + column1Value3 + " " + column1Value4 + " " + column1Value5 + " " + column1Value6 + " " + column1Value7 + " " + column1Value8 + " " + column1Value9 + " ");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
try {
if (resultSet != null) resultSet.close();
if (preparedStatement != null) preparedStatement.close();
if (connection != null) connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

public static void deleteshop() throws SQLException {
Scanner sc = new Scanner(System.in);
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库名称", "用户名", "密码");
PreparedStatement preparedStatement0;
while (true) {
System.out.println("请输入商品编号");
String itemno2 = sc.next();
String deleteQuery0 = "DELETE FROM shop WHERE itemno = ?";
preparedStatement0 = connection.prepareStatement(deleteQuery0);
preparedStatement0.setString(1, itemno2);
int rowsAffected = preparedStatement0.executeUpdate();
if (rowsAffected > 0) {
System.out.println("成功删除 " + rowsAffected + " 条符合要求的数据。");
break;
} else {
System.out.println("没有找到符合要求的数据,请检查查询条件。");
}
}
preparedStatement0.close();
}
}

标签:String,管理系统,resultSet,数据库,9.12,System,sc,println,out
From: https://www.cnblogs.com/zhaoqianwan/p/17697548.html

相关文章

  • openGauss学习笔记-67 openGauss 数据库管理-创建和管理普通表-创建表
    openGauss学习笔记-67openGauss数据库管理-创建和管理普通表-创建表67.1背景信息表是建立在数据库中的,在不同的数据库中可以存放相同的表。甚至可以通过使用模式在同一个数据库中创建相同名称的表。创建表前请先规划存储模型。67.2创建表执行如下命令创建表。openGauss=#......
  • 达梦数据库的执行计划解读
    概念描述达梦数据库跟oracle、mysql一样,也有自己的优化器,优化器会根据需要执行的sql,计算出执行该sql使用的各种方案的代价(即时间),然后会选择一个代价最低的方案,去执行SQL。也就是执行计划。常用的查看执行计划有两种方式:1.EXPLAIN+SQL语句查看执行计划,在需要执行的sql前,加上explain......
  • 9.12
    今天学vue<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="......
  • openGauss数据库tpcc测试
     建表语句CREATETABLESPACEexample2relativelocation'tablespace2';CREATETABLESPACEexample3relativelocation'tablespace3';createtablebmsql_config(cfg_namevarchar(30),cfg_valuevarchar(50));createtablebmsql_wa......
  • 数据库重构之路,以 OrientDB 到 NebulaGraph 为例
    “本文由社区用户@阿七从第一视角讲述其团队重构图数据库的过程,首发于阿七公众号「浅谈架构」”原文出处:https://mp.weixin.qq.com/s/WIJNq-nuuAGtMjYo5rPLyg一、写在前面读过我公众号文章的同学都知道,我做过很多次重构,可以说是“重构钉子户”,但是这次,重构图数据库OrientDB......
  • 每日总结(9.12)
    感受一下小小的朝鲜震撼   以上为朝鲜自研操作系统,红星OS3.0,看起来很像苹果家的MACOS。据说现在朝鲜红星OS4.0也出了,回头有资源了品鉴一下。......
  • 数据库三大范式
    面试又双叒叕被问到数据库三大范式,怎么答才能让面试官认可呢(qq.com)范式是我们设计数据库表时遵循的一种规范要求,主要有两个优点:消除重复数据减少冗余数据,从而让数据库内的数据能划分的更合理,让磁盘空间得到更有效利用的一种标准化标准;消除潜在异常(插入异常,更新异常,删除异常......
  • 如何使用Oracle Enterprise Manager Database Express连接到PDB数据库
    1.问题重复弹出登录框,无法登陆关闭登录框,显示invalidcontainername2.解决方法参考链接为PDB启动EMExpress要为PDB启动EMExpress,请确保PDB以读/写模式打开,然后尝试本主题中描述的以下方法之一(按所示顺序):连接到包含PDB的CDB的CDB$ROOT容器,并发出以下SQL......
  • AntDB数据库参加ACDU中国行杭州站,分享数据库运维实践与经验
    关于ACDU和中国行:ACDU是由墨天轮社区举办的中国数据库联盟的品牌活动之一,在线下汇集数据库领域的行业知名人士,共同探讨数据库前沿技术及其应用,促进行业发展和创新的平台,也为开发者们提供友好交流的机会。AntDB作为具有技术前瞻性的国产数据库产品,积极分享15年行业实践经验、线......
  • 首家!亚信科技AntDB数据库完成中国信通院数据库迁移工具专项测试
    近日,在中国信通院“可信数据库”数据库迁移工具专项测试中,湖南亚信安慧科技有限公司(简称:亚信安慧科技)数据库数据同步平台V2.1产品依据《数据库迁移工具能力要求》、结合亚信科技AntDB分布式关系型数据库产品,成为首款完成标准所规定的测试产品。测试过程依据标准在基础功能、数据库......