java商品管理系统(增删改查)
此商品管理系统是在myeclipse控制台实现的商品增删改查操作,较为普通,不是web端项⽬的那种,只能作为平时的实验,如果是课程设计的话应该制作web端那种。
项⽬建⽴了⼀个包为System2,其中有两个class⽂件,分别为ShopSystem.java和Test.java。
以下代码为ShopSystem.java的内容
package System2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ShopSystem {
public void insertShop(int id,String name,String introduce){  //插⼊
Connection conn  = null;
Statement stmt = null;
try{
Class.forName("sql.jdbc.Driver");
conn = Connection("jdbc:mysql://localhost:3306/test", "root", "111");
// 创造SQL语句
String sql = "INSERT INTO shop(id,name,introduce) VALUES ('"+id+"' ,'"+name+"', '"+introduce+"' )";
stmt = ateStatement();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void deleteShop(String name){  //删除
Connection conn = null;
Statement stmt = null;
try{
Class.forName("sql.jdbc.Driver");
conn = Connection("jdbc:mysql://localhost:3306/test","root","111");
String sql = "Delete From shop where name = '"+name+"'";
stmt = ateStatement();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
conn.close();
基本的增删改查语句
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void updateShop(String name,String introduce){  //更新
Connection conn = null;
Statement stmt = null;
try{
Class.forName("sql.jdbc.Driver");
conn = Connection("jdbc:mysql://localhost:3306/test", "root", "111");
String sql = "UPDATE shop SET introduce = '"+introduce+"' " +"WHERE name = '"+name+"'";  stmt = ateStatement();
}catch(Exception e){
e.printStackTrace();
}finally{
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void searchAllShop(){  //输出全部信息
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("sql.jdbc.Driver");
conn = Connection("jdbc:mysql://localhost:3306/test","root","111");
String sql = "SELECT * FROM shop";
stmt = ateStatement();
rs = uteQuery(sql);
()){
int id = rs.getInt(1);
String name = rs.getString(2);
String introduce = rs.getString(3);
System.out.println(id+"-"+name+"-"+introduce);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
conn = Connection("jdbc:mysql://localhost:3306/test", "root", "111");
//test是mysql⾥⾯创建的数据库,root是⽤户名,111是密码
String sql = "INSERT INTO shop(id,name,introduce) VALUES ('"+id+"' ,'"+name+"', '"+introduce+"' )"; //shop是test数据库⾥的表,包含id,name,introduce三个字段。
以下代码是test.java⾥⾯的内容:
package System2;
import java.util.Scanner;
public class Test {
public static void main(String args[]){
ShopSystem ss = new ShopSystem();
int id;
String name = null;
String introduce = null;
System.out.println("-----商品管理系统-----");
System.out.println("--物联⽹194吴铭世制作--");
while(true){
System.out.println("(1)添加商品信息");
System.out.println("(2)修改商品信息");
System.out.println("(3)删除商品信息");
System.out.println("(4)显⽰全部商品信息");
System.out.println("(5)退出");
System.out.println("请输⼊相应数字选择相应操作");
Scanner scanner = new Scanner(System.in);
int key = Int();
switch (key) {
case 1:
System.out.println("新增数据:");
System.out.println("输⼊id:");
id = Int();
//System.out.println(id);
System.out.println("输⼊name:");
name = Line();
//System.out.println(name);
System.out.println("输⼊introduce:");
//Line();
introduce = Line();
//1System.out.println(introduce);
ss.insertShop(id, name, introduce);
System.out.println("增加商品成功!\n");
System.out.println("增加商品成功!\n");
break;
case 2:
System.out.println("修改信息:");
System.out.println("输⼊name:");
name = Line();
System.out.println("输⼊introduce:");
//Line();
introduce = Line();
ss.updateShop(name, introduce);
System.out.println("修改成功!\n");
break;
case 3:
System.out.println("删除商品:");
System.out.println("输⼊name:");
name = Line();
ss.deleteShop(name);
System.out.println("删除成功!\n");
break;
case 4:
System.out.println("显⽰全部商品信息:");
ss.searchAllShop();
/
/Line();
System.out.println("\n");
break;
case 5:
System.out.println("成功退出!\n");
return;
default:
System.out.println("请输⼊1-5选择相应操作!!\n");
break;
}
}
}
}
两个java⽂件创建完,数据库连接成功,运⾏test.java⽂件就可以在控制台测试了,具体运⾏情况如下:
好好打代码,别偷懒哦
尽快好主修⽅向(最晚⼤三开学时),前端、软件、单⽚机、嵌⼊式……精通其中⼀个才好⼯作。希望能帮到你们,别忘了点个赞哦。
2021.11.22

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。