JDBC商品管理系統(tǒng):商品管理操作,商品類型操作,編譯器bug解決和封裝快捷方式等方法


--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主關(guān)注和收藏!' )
,('詩書畫唱牌含最熱10首全球榜單TOP1曲CD',999.9,2,'嘿嘿!好聽!請到b站給叫“詩書畫唱”的up主關(guān)注和投幣!' )
,('詩書畫唱牌含最新10首熱曲CD!',999.9,2,'哈哈!好聽!請到b站給叫“詩書畫唱”的up主關(guān)注和點贊!' )
,('詩書畫唱牌可生人類孩子的人類版女性機器人',9999.9,3,'魅力無限,智慧,可愛,性感,迷人,身材好,
顏值高,可根據(jù)個人喜歡定制,可滿足你的一切幻想!' )
,('詩書畫唱牌可一起生人類生孩子的人類版男性機器人',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("--------親,歡迎使用詩書畫唱商品管理系統(tǒng)喲(^_^)--------");
System.out.println(" ------請輸入你要執(zhí)行的操作喲--------");
System.out.println("1.登錄2.注冊 3.退出系統(tǒng)");
int num = s.nextInt();
if (num == 1) {
System.out.println("------執(zhí)行登錄功能-----");
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("---------執(zhí)行注冊功能啦(^_^)--------");
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("內(nèi)容輸入錯誤(。>_<。)!");
}
}
System.out.println("歡迎你來到詩書畫唱商店,尊敬的用戶" + yh.getYh_Uname()
+ ",請到b站給叫“詩書畫唱”的up主關(guān)注和點贊!拜托了!這對我很重要?。。╚_^)");
while (true) {
System.out.println("請選擇你要執(zhí)行的操作,嘻嘻!");
System.out.println("1.商品管理操作2. 商品類型操作3.購買操作 4.訂單操作 5.個人信息管理 6. 退出");
int yzxdcz = s.nextInt();
if (yzxdcz == 1) {
System.out.println("---執(zhí)行商品管理操作啦,嘿嘿!---");
System.out.println("請輸入你要執(zhí)行的功能");
System.out.println("1.查詢商品2. 修改商品3. 增加商品4. 刪除商品");
int num_sp = s.nextInt();
if (num_sp == 1) {
System.out.println("執(zhí)行查詢商品操作,呵!");
System.out.println("請輸入你要進行那種查詢啦!賣萌!");
System.out.println("1.查詢?nèi)啃畔?. 按照價格查詢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("親,現(xiàn)在已經(jīng)從數(shù)據(jù)庫查詢了所有的商品類型啦,"
+ "用戶可以根據(jù)提示輸入類型編號了哦!快夸夸本寶寶!嘻嘻!");
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("---執(zhí)行商品類型管理---");
System.out.println("請輸入你要對類型執(zhí)行的操作哦");
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("---執(zhí)行增加商品類型操作啦,親---");
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("---執(zhí)行修改商品類型功能喲---");
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("---執(zhí)行刪除商品類型操作啦!---");
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("編號輸入錯誤");
}
}
}
}
/*
?*注意事項: 包名不要取名時,含中文,不然會有代碼中的中文變亂碼等問題!
?* 注釋(寫時這些我是寫在代碼旁的:現(xiàn)在寫下面,看不懂代碼時,可看以下代碼就容易會和懂了?。?
?* 登錄成功則用b = false跳出循環(huán)
?*/
/* ydlxzxdcz(要對類型執(zhí)行的查找) */
/* while(true){}要包裹yzxdcz(要執(zhí)行的操作)中的操作,除非點這里的6.退出選項,否則就一直循環(huán) */
/* bianli(arr_typeload);為封裝了的bianli遍歷方法 */
/* 因為main函數(shù)那里加了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(),是并集關(guān)系,得到并集結(jié)果
?*/
/* ySQLyj_sld_sp_lx_mc為用SQL語句輸入的商品類型名稱 *//* diaoyongSQL為調(diào)用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可對應另一個表中的所有數(shù)據(jù)
*/
private String sp_Jieshao;
private String sp_Name;
private double sp_Price;
private sp_Type sp_TypeID;
public shangpin() {
}/* 無參構(gòu)造方法 */
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 + "]";
}
}






封裝快捷方式圖析:





