欢迎光临散文网 会员登陆 & 注册

JDBC商品管理系统:商品管理操作,商品类型操作,编译器bug解决和封装快捷方式等方法

2020-03-25 10:14 作者:诗书画唱  | 我要投稿


--delete shangpin where sp_TypeID=1

--select * from yonghu

--select * from sp_Type

--select*from shangpin

--create database yonghu

--use yonghu

--drop database yonghu

--select * from yonghu

--drop table yonghu

--drop table sp_Type

--drop table shangpin 

--select* from shangpin 

--select * from sp_Type

--select *from shangpin a inner join sp_type b on a.sp_TypeID=b.sp_TypeID where b.sp_typeid=1

create table sp_Type(

sp_TypeID int primary key identity(1, 1),

sp_TypeName nvarchar(100) not null)


create table shangpin (

sp_ID int primary key identity(1, 1),

sp_Name nvarchar(100) not null,

sp_Price decimal(10, 2) check(sp_Price>0),

sp_TypeID int,

sp_Jieshao nvarchar (500),

foreign key(sp_TypeID) references sp_Type (sp_TypeID))



insert into shangpin values(' 《群山回唱》',19.9,1,'很好看和感人的书!' )

,('《诗书画唱传》',99.9,1, '一本讲述有“历史上最伟大和对人类最大贡献的划时代人物,

人工智能之父,全能高手”之称的传奇人

物的传奇人生和世人大多没想到的伟大成就的书,请到b站给叫“诗书画唱”的up主关注和收藏!' )

,('诗书画唱牌含最热10首全球榜单TOP1曲CD',999.9,2,'嘿嘿!好听!请到b站给叫“诗书画唱”的up主关注和投币!' )

,('诗书画唱牌含最新10首热曲CD!',999.9,2,'哈哈!好听!请到b站给叫“诗书画唱”的up主关注和点赞!' )

,('诗书画唱牌可生人类孩子的人类版女性机器人',9999.9,3,'魅力无限,智慧,可爱,性感,迷人,身材好,

颜值高,可根据个人喜欢定制,可满足你的一切幻想!' )

,('诗书画唱牌可一起生人类生孩子的人类版男性机器人',9999.9,3,'可满足广大女性的一切幻想!' )


,('诗书画唱牌钢铁侠上天装备',9999.9,4,'可满足广大科幻迷的炫酷体验!' )

,('诗书画唱牌钢铁侠盔甲装备',9999.9,4,'可满足广大科幻迷的钢铁侠撩妹体验!' )





insert into sp_Type values('书籍'),('CD')

,('诗书画唱牌机器人'),('诗书画唱牌钢铁侠装备')




create table yonghu(


yh_ID int,

yh_Uname varchar(30),

yh_Pwd varchar(30),

yh_Name varchar(30),

yh_Age int not null,

yh_Sex varchar(20) not null,

yh_Phone varchar(100)not null,

yh_Address varchar(200)not null,

yh_Jieshao varchar(500)not null)








4个.java文件:


1


package JDBC;


import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;


public class main {

public static Scanner s = new Scanner(System.in);

static yonghu yh = new yonghu();


public static void bianli_SP(ArrayList<shangpin> arr) {

for (shangpin i : arr) {

System.out.println(i);

}

}


private static void bianli_SPtype(ArrayList<sp_Type> arr_typeload) {

for (sp_Type i : arr_typeload) {

System.out.println(i);

}

}


private static void Bianli_TypeLoad(ArrayList<sp_Type> sp_Type) {


}


public static ArrayList<shangpin> getSP(String sql) {


ResultSet res = diaoyongSQL.Select(sql);

ArrayList<shangpin> arr = new ArrayList<shangpin>();


try {

while (res.next()) {

shangpin sp = new shangpin();

sp.setSp_ID(res.getInt("sp_ID"));

sp.setSp_Name(res.getString("sp_Name"));

sp.setSp_Price(res.getDouble("sp_Price"));

sp_Type sp_type = new sp_Type();

sp_type.setSp_TypeID(res.getInt("sp_TypeID"));

sp_type.setSp_TypeName(res.getString("sp_TypeName"));

sp.setSp_Jieshao(res.getString("sp_jieshao"));

sp.setSp_TypeID(sp_type);

arr.add(sp);


}

} catch (SQLException e) {

e.printStackTrace();

}

return arr;

}


public static ArrayList<sp_Type> getSp_Load(String sql) {


ArrayList<sp_Type> arr_sptype = new ArrayList<sp_Type>();


ResultSet res = diaoyongSQL.Select(sql);


try {

while (res.next()) {

sp_Type sp_type = new sp_Type();

sp_type.setSp_TypeID(res.getInt("sp_TypeID"));

sp_type.setSp_TypeName(res.getString("sp_TypeName"));

arr_sptype.add(sp_type);

}


} catch (SQLException e) {


e.printStackTrace();

}

return arr_sptype;

}


private static ArrayList<sp_Type> getSP_Type() {

String sql = "select * from sp_Type";


ResultSet res = diaoyongSQL.Select(sql);


ArrayList<sp_Type> arr_typeload = new ArrayList<sp_Type>();

try {

while (res.next()) {

sp_Type sp = new sp_Type();

sp.setSp_TypeID(res.getInt(1));

sp.setSp_TypeName(res.getString(2));

arr_typeload.add(sp);

}

} catch (SQLException e) {


e.printStackTrace();

}

return arr_typeload;

}


public static void main(String[] args) throws Exception {

boolean b = true;


while (b) {

System.out.println("--------亲,欢迎使用诗书画唱商品管理系统哟(^_^)--------");

System.out.println(" ------请输入你要执行的操作哟--------");

System.out.println("1.登录2.注册 3.退出系统");

int num = s.nextInt();

if (num == 1) {


System.out.println("------执行登录功能-----");

System.out.println("请输入你的用户名哟");

String uname = s.next();

System.out.println("请输入你的密码哟");

String pwd = s.next();

boolean bb = true;

while (bb) {

System.out.println("请输入验证码哟,亲");


Random r = new Random();

int num_yanzhengma_bianhao = r.nextInt(9000) + 1000;

System.out.println(num_yanzhengma_bianhao);

int num_yanzhengma = s.nextInt();


if (num_yanzhengma_bianhao == num_yanzhengma) {


System.out.println("验证码输入正确哦!棒棒哒!(^_^)");

bb = false;

} else {

System.out.println("验证码输入错误啦(-_-)");


}

}


String sql = "select * from yonghu where yh_Uname= '" + uname

+ "' and yh_Pwd='" + pwd + "'";


ResultSet res = diaoyongSQL.Select(sql);

if (res.next()) {

System.out.println("登录成功啦!");

yh.setYh_ID(res.getInt("yh_ID"));

yh.setYh_Uname(res.getString("yh_Uname"));

yh.setYh_Pwd(res.getString("yh_Pwd"));

yh.setyh_Name(res.getString("yh_Name"));

yh.setYh_Age(res.getInt("yh_Age"));

yh.setYh_Sex(res.getString("yh_Sex"));

yh.setYh_Phone(res.getString("yh_Phone"));

yh.setYh_Address(res.getString("yh_Address"));

yh.setYh_Jieshao(res.getString("yh_Jieshao"));

b = false;

} else {

System.out.println("用户名或密码输入错误,请重试!");

}

}


else if (num == 2) {


System.out.println("---------执行注册功能啦(^_^)--------");

System.out.println("请输入你的ID");

int ID = s.nextInt();

System.out.println("请输入你的用户名");

String uname = s.next();

System.out.println("请输入你的用户密码");

String pwd = s.next();

System.out.println("请输入你的真实姓名");

String name = s.next();

System.out.println("请输入你的年龄");

int age = s.nextInt();

System.out.println("请输入你的性别");

String sex = s.next();

System.out.println("请输入你的手机号");

String phone = s.next();

System.out.println("请输入你的地下址");

String address = s.next();

System.out.println("请输入你的个人介绍哦");

String jieshao = s.next();

String sql = "insert into yonghu values(" + ID + ",'" + uname

+ "'" + ", '" + pwd + "',' " + name + "', " + age

+ " , '" + sex + "' ," + "'" + phone + "'," + "'"

+ address + "'" + "," + "'" + jieshao + "')";

if (diaoyongSQL.ZSG(sql)) {


System.out.println("注册成功(^_^)");

} else {


System.out.println("注册失败,请重试,加油!亲!");

}

} else if (num == 3) {


System.exit(0);

} else {


System.out.println("内容输入错误(。>_<。)!");

}

}

System.out.println("欢迎你来到诗书画唱商店,尊敬的用户" + yh.getYh_Uname()

+ ",请到b站给叫“诗书画唱”的up主关注和点赞!拜托了!这对我很重要啊!(^_^)");


while (true) {

System.out.println("请选择你要执行的操作,嘻嘻!");

System.out.println("1.商品管理操作2. 商品类型操作3.购买操作 4.订单操作 5.个人信息管理 6. 退出");


int yzxdcz = s.nextInt();

if (yzxdcz == 1) {


System.out.println("---执行商品管理操作啦,嘿嘿!---");

System.out.println("请输入你要执行的功能");

System.out.println("1.查询商品2. 修改商品3. 增加商品4. 删除商品");

int num_sp = s.nextInt();

if (num_sp == 1) {


System.out.println("执行查询商品操作,呵!");

System.out.println("请输入你要进行那种查询啦!卖萌!");

System.out.println("1.查询全部信息2. 按照价格查询3. 按照类型进行查询");


int num_sp_type = s.nextInt();

if (num_sp_type == 1) {

String sql = "select *from shangpin a "

+ "inner join sp_type b on "

+ "a.sp_TypeID=b.sp_TypeID";


bianli_SP(getSP(sql));

} else if (num_sp_type == 2) {


String sql = "select * from shangpin a inner join sp_"

+ "Type b on a.sp_TypeID=b.sp"

+ "_TypeID order by a.sp_Price";

bianli_SP(getSP(sql));

} else if (num_sp_type == 3) {


System.out.println("请输入你要查询的商品类型的编号哟");

bianli_SPtype(getSP_Type());

System.out.println("亲,现在已经从数据库查询了所有的商品类型啦,"

+ "用户可以根据提示输入类型编号了哦!快夸夸本宝宝!嘻嘻!");

int sp_typebianhao = s.nextInt();

String sql_leixing = "select *from shangpin"

+ " a inner join sp_type b on"

+ " a.sp_typeid=b.sp_typeid where b.sp_typeid="

+ sp_typebianhao;

bianli_SP(getSP(sql_leixing));


}


}

} else if (yzxdcz == 2) {


System.out.println("---执行商品类型管理---");

System.out.println("请输入你要对类型执行的操作哦");

System.out.println("1.查询商品类型2. 增加商品类型3. 修改商品类型4. 删除商品类型");

int ydlxzxdcz = s.nextInt();

if (ydlxzxdcz == 1) {


ArrayList<sp_Type> arr_typeload = getSP_Type();

bianli_SPtype(arr_typeload);


} else if (ydlxzxdcz == 2) {


System.out.println("---执行增加商品类型操作啦,亲---");


System.out.println("请输入你要增加的商品类型名称哦!");

String yzgdsplxmc = s.next();


String ySQLyj_zjd_sp_mc = "insert into sp_Type values"

+ "('" + yzgdsplxmc + "')";


if (diaoyongSQL.ZSG(ySQLyj_zjd_sp_mc)) {

System.out.println("商品类型增加成功!恭喜!");


} else {

System.out.println("类型增加失败,请重试,加油!");

}


} else if (ydlxzxdcz == 3) {


System.out.println("---执行修改商品类型功能哟---");

System.out.println("请输入你要修改的商品类型的ID哦!");


int yxgd_sp_lx_ID = s.nextInt();


for (int i = 0; i < getSP_Type().size(); i++) {


if (getSP_Type().get(i).getSp_TypeID() == yxgd_sp_lx_ID) {

System.out.println("请输入新的商品类型名称啊!嘿嘿!");


String ySQLyj_sld_sp_lx_mc = s.next();


String sql_Sp_Type_Update = "update sp_Type set"

+ " sp_TypeName='" + ySQLyj_sld_sp_lx_mc

+ "' where sp_TypeID=" + yxgd_sp_lx_ID;


diaoyongSQL.ZSG(sql_Sp_Type_Update);


}

}

} else if (ydlxzxdcz == 4) {


System.out.println("---执行删除商品类型操作啦!---");

System.out.println("请输入你要删除的商品类型ID呀!亲!");

int yscd_sp_lx_ID = s.nextInt();


boolean b_SP_Type_Delete = true;


for (int i = 0; i < getSP_Type().size(); i++) {

if (getSP_Type().get(i).getSp_TypeID() == yscd_sp_lx_ID) {


String sql_SP_Type_Delete = "delete "

+ "sp_Type where sp_TypeID="

+ yscd_sp_lx_ID;

/*有时代码没错,SQL和Java的编译器会出bug,等一下就恢复正常了*/

diaoyongSQL.ZSG(sql_SP_Type_Delete);


b_SP_Type_Delete = false;


}


}


if (b_SP_Type_Delete) {

System.out.println("商品类型ID不存在哦");

} else {


System.out.println("商品删除成功啦!");

}


}


else {

System.out.println("请输入正确的的类型编号");

}


}


else if (yzxdcz == 3) {


} else if (yzxdcz == 4) {


} else if (yzxdcz == 5) {


} else if (yzxdcz == 6) {

System.exit(0);

} else {

System.out.println("编号输入错误");


}

}


}

}

/*

 *注意事项: 包名不要取名时,含中文,不然会有代码中的中文变乱码等问题!

 * 注释(写时这些我是写在代码旁的:现在写下面,看不懂代码时,可看以下代码就容易会和懂了!) 

 * 登录成功则用b = false跳出循环

 */



/* ydlxzxdcz(要对类型执行的查找) */



/* while(true){}要包裹yzxdcz(要执行的操作)中的操作,除非点这里的6.退出选项,否则就一直循环 */



/* bianli(arr_typeload);为封装了的bianli遍历方法 */



/* 因为main函数那里加了throws Exception,所以以本来这里会有红线,这里会没红线*/



/* yzgdsplxmc为要增加的商品类型名称 ySQLyj_zjd_sp_mc为用SQL语句增加的商品名称*/


/* yxgd_sp_lx_ID为要修改的商品类型ID */


/* i在for中都遍历,i为下标,当用getSP_Type().get(i)得到的第i下标的商品类型, getSP_Type

 * ().get(i).getSp_TypeID()得到的第i下标的商品类型中的商品类型ID 在getxxx()后加

 * .get(x)表表明得到第x下标的xxx, 后面可以加多个getxxxx(),是并集关系,得到并集结果

 */



/* ySQLyj_sld_sp_lx_mc为用SQL语句输入的商品类型名称 *//* diaoyongSQL为调用SQL方法 */



/* yscd_sp_lx_ID为要删除的商品类型ID */







2

package JDBC;


public class sp_Type {

private int sp_TypeID;

private String sp_TypeName;

public sp_Type(){}

public int getSp_TypeID() {

return sp_TypeID;

}

public String getSp_TypeName() {

return sp_TypeName;

}

public void setSp_TypeID(int sp_TypeID) {

this.sp_TypeID = sp_TypeID;

}

public void setSp_TypeName(String sp_TypeName) {

this.sp_TypeName = sp_TypeName;

}

@Override

public String toString() {

return "sp_Type [sp_TypeID=" + sp_TypeID + ", sp_TypeName=" + sp_TypeName

+ "]";

}


}


3



package JDBC;


public class yonghu {


private String yh_Address;// yh_Address代表用户地址

private int yh_Age;// 代表用户年龄

private int yh_ID;// 代表用户编号

private String yh_Jieshao;// 代表用户 个人介绍

private String yh_Name;// 代表用户 真实姓名

private String yh_Phone;// 代表用户 手机号

private String yh_Pwd;// 代表用户 登录密码

private String yh_Sex;// 代表用户 性别

private String yh_Uname;// 代表用户登录名


public String getYh_Address() {

return yh_Address;

}


public int getYh_Age() {

return yh_Age;

}


public int getYh_ID() {

return yh_ID;

}


public String getYh_Jieshao() {

return yh_Jieshao;

}


public String getYh_Name() {

return yh_Name;

}


public String getYh_Phone() {

return yh_Phone;

}


public String getYh_Pwd() {

return yh_Pwd;

}


public String getYh_Sex() {

return yh_Sex;

}


public String getYh_Uname() {

return yh_Uname;

}


public void setYh_Address(String yh_Address) {

this.yh_Address = yh_Address;

}


public void setYh_Age(int yh_Age) {

this.yh_Age = yh_Age;

}


public void setYh_ID(int yh_ID) {

this.yh_ID = yh_ID;

}


public void setYh_Jieshao(String yh_Jieshao) {

this.yh_Jieshao = yh_Jieshao;

}


public void setyh_Name(String string) {


}


public void setYh_Name(String yh_Name) {

this.yh_Name = yh_Name;

}


public void setYh_Phone(String yh_Phone) {

this.yh_Phone = yh_Phone;

}


public void setYh_Pwd(String yh_Pwd) {

this.yh_Pwd = yh_Pwd;

}


public void setYh_Sex(String yh_Sex) {

this.yh_Sex = yh_Sex;

}


public void setYh_Uname(String yh_Uname) {

this.yh_Uname = yh_Uname;

}


@Override

public String toString() {

return "yonghu [yh_ID=" + yh_ID + ", yh_Uname=" + yh_Uname

+ ", yh_Pwd=" + yh_Pwd + ", yh_Name=" + yh_Name + ", yh_Age="

+ yh_Age + ", yh_Sex=" + yh_Sex + ", yh_Phone=" + yh_Phone

+ ", yh_Address=" + yh_Address + ", yh_Jieshao=" + yh_Jieshao

+ "]";

}


}


4


package JDBC;


import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;


public class diaoyongSQL {


private static Connection con = null;

private static ResultSet res = null;

private static Statement sta = null;


static {

try {

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

} catch (ClassNotFoundException e) {

e.printStackTrace();

}

}



public static Connection getCon() {


if (con == null) {

try {

con = DriverManager.getConnection(

"jdbc:sqlserver://DESKTOP-49FTFSP;"

+ "databaseName=yonghu", "sa", "1234abcd");

} catch (SQLException e) {

e.printStackTrace();

}

}

return con;

}



public static ResultSet Select(String sql) {


con = getCon();


try {

sta = con.createStatement();


res = sta.executeQuery(sql);

} catch (SQLException e) {


e.printStackTrace();

}

return res;

}



public static boolean ZSG(String sql) {


boolean b = false;

con = getCon();


try {

sta = con.createStatement();


int num = sta.executeUpdate(sql);

if (num > 0) {

b = true;

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return b;

}


}



package JDBC;


public class shangpin {

private int sp_ID;

/*

* 这里int 变成sp_Type是为了更简便,因为SQL代码中两表之间有sp_TypeID设置为主键和外键,

* 所以这样设置时,一表中的sp_Type可对应另一个表中的所有数据

*/

private String sp_Jieshao;

private String sp_Name;

private double sp_Price;

private sp_Type sp_TypeID;


public shangpin() {

}/* 无参构造方法 */


public int getSp_ID() {

return sp_ID;

}


public String getSp_Jieshao() {

return sp_Jieshao;

}


public String getSp_Name() {

return sp_Name;

}


public double getSp_Price() {

return sp_Price;

}


public sp_Type getSp_TypeID() {

return sp_TypeID;

}


public void setSp_ID(int sp_ID) {

this.sp_ID = sp_ID;

}


public void setSp_Jieshao(String sp_Jieshao) {

this.sp_Jieshao = sp_Jieshao;

}


public void setSp_Name(String sp_Name) {

this.sp_Name = sp_Name;

}


public void setSp_Price(double sp_Price) {

this.sp_Price = sp_Price;

}


public void setSp_TypeID(sp_Type sp_TypeID) {

this.sp_TypeID = sp_TypeID;

}


@Override

public String toString() {

return "shangpin [sp_ID=" + sp_ID + ", sp_Name=" + sp_Name

+ ", sp_Price=" + sp_Price + ", sp_TypeID=" + sp_TypeID

+ ", sp_Jieshao=" + sp_Jieshao + "]";

}


}






封装快捷方式图析:



JDBC商品管理系统:商品管理操作,商品类型操作,编译器bug解决和封装快捷方式等方法的评论 (共 条)

分享到微博请遵守国家法律