JDBC商品管理系統(tǒng)(有登錄注冊功能改進(jìn)版)含在很多行代碼旁寫的個(gè)人詳細(xì)注釋(一)

.sql1

--select *from shangpin? where sp_ID=1
--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 sp_Type values('書籍'),('CD')
,('機(jī)器人'),('裝備')
insert into shangpin values(' 《群山回唱》',9,1,'很好看和感人的書!' )
,('《詩書畫唱傳》',7,1, '一本人物自傳' )
,('TOP1曲CD',6,2,'好聽!' )
,('熱曲CD!',8,2,'好聽!點(diǎn)贊!' )
,('女性機(jī)器人',99,3,'魅力無限' )
,('男性機(jī)器人',77,3,'可滿足幻想!' )
insert into shangpin values('上天裝備',19,4,'炫酷體驗(yàn)!' )
,('盔甲裝備',56,4,'可滿足科幻迷!' )
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)


.java1

package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class mains {
/* 這個(gè)GouWuChe集合為購物車: */
public static ArrayList<shangpin> GouWuChe = new ArrayList<shangpin>();
/* ResultSet,數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表,通常通過執(zhí)行查詢數(shù)據(jù)庫的語句生成: */
public static ResultSet res_Select_Dantiao = null;
/* 下面是為了給全局能夠接受輸入的數(shù)據(jù): */
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) {
}
/* 下面是封裝打印商品內(nèi)容的方法 */
public static ArrayList<shangpin> getSp(ResultSet res) {
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.setSp_Jieshao(res.getString("sp_Jieshao"));
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_TypeID(sp_type);
arr.add(sp);
}
} catch (SQLException e) {
e.printStackTrace();
}
return arr;
}
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;
}
/*主要設(shè)計(jì)的思路:?
* 一.購買 購買的時(shí)候要做的事:?
* 1.提示用戶輸入購買的商品ID并遍歷商品
* 2.接收用戶輸入的商品的ID?
* 3.去數(shù)據(jù)庫里去查一下這個(gè)商品的ID的商品是否存在,存在就把這件商品放入到購物車中,不存在就提示用戶商品ID輸入錯(cuò)誤)
*?
* 二.商品管理?
* 用戶暫時(shí)是不知道有那些商品,所以要去數(shù)據(jù)庫去查詢所有的商品,并遍歷出來讓用戶輸入商品的ID, 把遍歷商品的代碼
* 封裝起來,會(huì)很方便,要用時(shí)調(diào)用遍歷的方法就可以了
*?
* 三.購物車?
* 先假設(shè)這個(gè)商品是存在的。 如果商品存在的話,需要就將這件商品放入到購物車中,
* 購物車其實(shí)也可以設(shè)置一張數(shù)據(jù)庫表代替,但是現(xiàn)在為了方便直接聲明一個(gè)集合當(dāng)做購物車
*?
* 四.連接數(shù)據(jù)庫,將Java語句和SQL語句聯(lián)系起來?
* 1.首先要有數(shù)據(jù)源,所以先創(chuàng)建數(shù)據(jù)庫表和內(nèi)容。 (Class.forName()方法的含義是:加載參數(shù)指定的類,并且初始化它。)
*? 2.導(dǎo)入包,即導(dǎo)入說明書,即導(dǎo)入jar包。?
*? 3.建立鏈接對象?
*? 4.建立執(zhí)行數(shù)據(jù)庫語句的對象
* 5.執(zhí)行SQL語句,得到結(jié)果集,即數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表
*? 6.遍歷結(jié)果集,即遍歷數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表 五.增加商品等 提示用戶輸入增加商品的名稱,價(jià)格,類型,介紹 用戶名和數(shù)據(jù)庫的名字要改對
*?
* 下面是主函數(shù),寫主要的程序。
*/
public static void main(String[] args) throws Exception {
/* 下面用了循環(huán): */
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("請輸入驗(yàn)證碼喲,親");
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("驗(yàn)證碼輸入正確哦!棒棒噠!(^_^)");
bb = false;
} else {
System.out.println("驗(yàn)證碼輸入錯(cuò)誤啦(-_-)");
}
}
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("用戶名或密碼輸入錯(cuò)誤,請重試!");
}
}
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("請輸入你的真實(shí)姓名");
String name = s.next();
System.out.println("請輸入你的年齡");
int age = s.nextInt();
System.out.println("請輸入你的性別");
String sex = s.next();
System.out.println("請輸入你的手機(jī)號");
String phone = s.next();
System.out.println("請輸入你的地址");
String address = s.next();
System.out.println("請輸入你的個(gè)人介紹哦");
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)容輸入錯(cuò)誤(。>_<。)!");
}
}
System.out.println("歡迎你來到詩書畫唱商店,尊敬的用戶" + yh.getYh_Uname()
+ ",請到b站給叫“詩書畫唱”的up主關(guān)注和點(diǎn)贊!拜托了!這對我很重要??!(^_^)");
while (true) {
System.out.println("請輸入你要執(zhí)行的操作:1.商品管理? 2.購買操作? ?"
+ " 3.訂單操作購物車4. 商品類型操作5.個(gè)人信息管理6.退出");
/* CaoZuo為操作: */
int CaoZuo = s.nextInt();
if (CaoZuo == 1) {
/* 下面是調(diào)用了封裝的方法,shangpinguanli()為封裝“商品管理”的方法 */
shangpinguanli();
} else if (CaoZuo == 2) {
System.out.println("---執(zhí)行購買商品操作---");
System.out.println("請輸入你要購買的商品ID");
String sql = "select * from shangpin a inner join"
+ " sp_Type b on a.sp_TypeID=b.sp_TypeID";
/* 下面是調(diào)用SQL查找的封裝方法: */
ResultSet res_sp = diaoyongSQL.Select(sql);
/*
調(diào)用getSp(res_sp)這個(gè)封裝打印商品內(nèi)容的方法, 在右邊賦值給左邊的被命名為arr_sp的
只接收shangpin類內(nèi)容的ArrayList集 ,遍歷了商品內(nèi)容:
*/
ArrayList<shangpin> arr_sp = getSp(res_sp);
/* 下面將得到的集合遍歷了一遍: */
for (shangpin a : arr_sp) {
System.out.println(a);
}
/* 接受用戶要購買哪個(gè)商品的ID,ID就是編號: */
int ShangPinID = s.nextInt();
/* 下面是SQL查找單件商品的語句: */
String sql_select_DianJian = "select * from shangpin "
+ "a inner join sp_Type b on "
+ "a.sp_typeID=b.sp_TypeID where a.sp_ID=" + ShangPinID
+ "";
/* 下面是調(diào)用SQL查找的封裝方法,放在右邊賦值給被命名為左邊的數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表: */
ResultSet res_ShuJuKu_JieGuoJi_ShuJvBiao = diaoyongSQL
.Select(sql_select_DianJian);
/*
* 下面是調(diào)用SQL查找的封裝方法,放在右邊賦值給被命名為左邊的數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表,
* 將查詢單件商品的內(nèi)容也轉(zhuǎn)為ArrayList
*/
ArrayList<shangpin> arr_sp_DianGe = getSp(res_ShuJuKu_JieGuoJi_ShuJvBiao);
/*
* arr_sp_DianGe.size()是內(nèi)容的大小,長度,為字節(jié)等的數(shù)值長度, 字節(jié)等的數(shù)值長度>0,
* 說明有字節(jié)等,判斷這個(gè)集合中有沒有數(shù)據(jù),有則用戶輸入的編號是正確的,沒有則商品不存在
*/
if (arr_sp_DianGe.size() > 0) {
/* 下面是一個(gè)被命名為sp_GouWuChe的集合: */
shangpin sp_GouWuChe = new shangpin();
/* 下面將購買內(nèi)容加到了shangpin集合中: */
for (shangpin i : arr_sp_DianGe) {
sp_GouWuChe.setSp_ID(i.getSp_ID());
sp_GouWuChe.setSp_Name(i.getSp_Name());
sp_GouWuChe.setSp_Price(i.getSp_Price());
sp_GouWuChe.setSp_Jieshao(i.getSp_Jieshao());
sp_GouWuChe.setSp_TypeID(i.getSp_TypeID());
}
/*
下面將sp_GouWuChe集合添加到GouWuChe集合中,我會(huì)幾乎每一行都寫注釋,就算前面講過我也寫,方便理解, 防遺忘而重新找和看注釋,編程有很多將意思進(jìn)行縮寫,省事,省數(shù)據(jù)的表達(dá)和執(zhí)行的方式。很多抽象的東西也是一種 省事, 省數(shù)據(jù)的表達(dá)和執(zhí)行的方式:
*/
GouWuChe.add(sp_GouWuChe);
System.out.println("商品添加購物車成功!");
} else {
System.out.println("商品ID不存在");
}
} else if (CaoZuo == 3) {
/* 下面是購物車,遍歷打印購物車集合中的內(nèi)容,查詢購物車有沒有商品 */
System.out.println("---執(zhí)行遍歷購物車功能---");
System.out.println("購物車現(xiàn)在的商品有:");
for (shangpin i : GouWuChe) {
System.out.println(i);
}
} else if (CaoZuo == 4) {
System.out.println("---執(zhí)行商品類型管理---");
System.out.println("請輸入你要對類型執(zhí)行的操作哦");
System.out.println("1.查詢商品類型2. 增加商品類型3. 修改商品類型4. 刪除商品類型");
/*下面的ShangPinType_CaoZuo為商品類型操作*/
int ShangPinType_CaoZuo = s.nextInt();
if (ShangPinType_CaoZuo == 1) {
ArrayList<sp_Type> arr_typeload = getSP_Type();
bianli_SPtype(arr_typeload);
} else if (ShangPinType_CaoZuo == 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 (ShangPinType_CaoZuo == 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 (ShangPinType_CaoZuo == 4) {
System.out.println("---執(zhí)行刪除商品類型操作啦!---");
System.out.println("請輸入你要?jiǎng)h除的商品類型ID呀!親!");
/*下面的delete_sp_TypeID為要?jiǎng)h除的商品類型ID*/
int delete_sp_TypeID = s.nextInt();
boolean b_SP_Type_Delete = true;
for (int i = 0; i < getSP_Type().size(); i++) {
if (getSP_Type().get(i).getSp_TypeID() == delete_sp_TypeID) {
String sql_SP_Type_Delete = "delete "
+ "sp_Type where sp_TypeID="
+ delete_sp_TypeID;
/* 有時(shí)代碼沒錯(cuò),SQL和Java的編譯器會(huì)出bug,等一下就恢復(fù)正常了 */
diaoyongSQL.ZSG(sql_SP_Type_Delete);
b_SP_Type_Delete = false;
}
}
/*因?yàn)樯唐穲?zhí)行順序是由上至下的,所以當(dāng)刪除成功后b_SP_Type_Delete = false;
如果b_SP_Type_Delete=true,說明沒刪成功,商品類型ID不存在才會(huì)讓
b_SP_Type_Delete=true不變*/
if (b_SP_Type_Delete) {
System.out.println("商品類型ID不存在哦");
} else {
System.out.println("商品刪除成功啦!");
}
}
else {
System.out.println("請輸入正確的的類型編號");
}
} else if (CaoZuo == 5) {
System.out.println("---執(zhí)行個(gè)人管理操作---");
System.out.println("請輸入你要執(zhí)行的功能");
System.out.println("1.查詢我的個(gè)人信息2.修改個(gè)人信息 3. 修改我的登錄密碼4. 注銷用戶");
/*下面的GeRenGuanLiCaoZuo為個(gè)人管理操作:*/
int GeRenGuanLiCaoZuo = s.nextInt();
if (GeRenGuanLiCaoZuo == 1) {
String sql_select_geren = "select * from yonghu a where a.yh_ID="
+ yh.getYh_ID();
ResultSet res_yonghu = diaoyongSQL.Select(sql_select_geren);
if (res_yonghu.next()) {
System.out.println("登錄成功");
System.out
.println("用戶編號\t用戶名\t密碼t年齡\t性別\t手機(jī)號\t地址\t個(gè)人介紹");
System.out.println(res_yonghu.getInt("yh_ID") + "\t");
System.out.print(res_yonghu.getString("yh_Uname")
+ "\t");
System.out
.print(res_yonghu.getString("yh_Name") + "\t");
System.out.print(res_yonghu.getInt("yh_Age") + "\t");
System.out.print(res_yonghu.getString("yh_Sex") + "\t");
System.out.print(res_yonghu.getString("yh_Phone")
+ "\t");
System.out.print(res_yonghu.getString("yh_Address")
+ "\t");
System.out.print(res_yonghu.getString("yh_Jieshao")
+ "\t");
System.out.println();
b = false;
}
} else if (GeRenGuanLiCaoZuo == 2) {
System.out.println("執(zhí)行修改用戶信息播作");
System.out.println("請輸入你的新名稱");
String new_Name = s.next();
System.out.println("請輸入你新的年齡");
int age = s.nextInt();
System.out.println("請輸入你新的性別");
String sex = s.next();
System.out.println("請輸入你新的手機(jī)號");
String phone = s.next();
System.out.println("請輸入你的地址");
String address = s.next();
System.out.println("請輸入你的個(gè)人介紹");
String jieshao = s.next();
String sql = "update yonghu set yh_Name='" + new_Name
+ "',yh_Age=" + age
+ ",yh_Sex='" + sex + "',yh_Phone='" + phone
+ "',yh_Address='" + address + "'"
+ ",yh_Jieshao='" + jieshao + ",'where yh_ID="
+ yh.getYh_ID();
/*diaoyongSQL為DB(database)utils(常用工具),數(shù)據(jù)庫常用工具,即增(Z)刪(S)改(G)查(Select)等方法*/
if (diaoyongSQL.ZSG(sql)) {
System.out.println("注冊成功");
} else {
System.out.println("注冊失敗,請重試");
}
} else if (GeRenGuanLiCaoZuo == 3) {
System.out.println("執(zhí)行修改用戶密碼操作");
System.out.println("請輸入你的新密碼");
String new_Pwd = s.next();
String sql = "update yonghu set yh_Pwd='" + new_Pwd
+ "'where yh_ID=" + yh.getYh_ID();
if (diaoyongSQL.ZSG(sql)) {
System.out.println("密碼修改成功");
} else {
System.out.println("修改失敗,請重試");
}
} else if (GeRenGuanLiCaoZuo == 4) {
System.out.println("執(zhí)行注銷用戶功能");
System.out.println("你確定要?jiǎng)h除掉自己的賬號信息嗎? 1. 我要?jiǎng)h除2. 再想想");
int num_delete_yonghu = s.nextInt();
if (num_delete_yonghu == 1) {
String sq1 = "delete yonghu where yh_ID="
+ yh.getYh_ID();
System.out.println("刪除成功");
System.out.println("3秒后跳轉(zhuǎn)到重新登錄界面");
Thread.sleep(3000);
}
} else {
System.out.println("輸入錯(cuò)誤");
}
}
else if (CaoZuo == 6) {
System.exit(0);
} else {
System.out.println("編號輸入錯(cuò)誤");
}
}
}
/* 下面是一個(gè)拋出找不到指定類和SQL語句執(zhí)行時(shí)出現(xiàn)錯(cuò)誤的異常的封裝的方法: */
private static void shangpinguanli() throws ClassNotFoundException,
SQLException {
System.out.println("執(zhí)行商品操作");
System.out.println("1.查詢商品? ?2.根據(jù)用戶輸入編號修改商品信息? ? ?3.增加商品? ? 4.刪除商品");
int ShangPinCaoZuo = s.nextInt();
if (ShangPinCaoZuo == 1) {
System.out.println("執(zhí)行查詢所有商品操作");
/* 下面是創(chuàng)建數(shù)據(jù)庫表和內(nèi)容,導(dǎo)入包,即導(dǎo)入說明書,即導(dǎo)入jar包: */
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 下面是建立鏈接對象: */
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/* 下面是建立執(zhí)行數(shù)據(jù)庫語句的對象: */
Statement sta = con.createStatement();
/* 下面是SQL語句: */
String sql = "select * from shangpin a inner "
+ "join sp_Type b on a.sp_TypeID=b.sp_TypeID";
/* 執(zhí)行SQL語句,得到結(jié)果集,即數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表: */
ResultSet res = sta.executeQuery(sql);
/* 下面是提示的話語,即列名: */
System.out.println("商品編號\t商品名稱\t價(jià)格\t類型名稱\t類型介紹");
/*
* 下面是打印商品內(nèi)容,這樣“下面是......:”的注釋方式很嚴(yán)謹(jǐn)和容易一行一行地讀懂, 感受到編程的嚴(yán)謹(jǐn)和邏輯性強(qiáng)等:
*/
while (res.next()) {
System.out.println(res.getInt("sp_ID") + "\t"
+ res.getString("sp_Name") + "\t"
+ res.getInt("sp_Price") + "\t"
+ res.getString("sp_TypeName") + "\t"
+ res.getString("sp_Jieshao"));
}
} else if (ShangPinCaoZuo == 2) {
System.out.println("---執(zhí)行修改商品操作---");
System.out.println("請輸入你要修改的商品編號");
/* 下面是創(chuàng)建數(shù)據(jù)庫表和內(nèi)容,導(dǎo)入包,即導(dǎo)入說明書,即導(dǎo)入jar包: */
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 下面是建立鏈接對象: */
Connection con_LianJieDuiXiang = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/* 下面是建立執(zhí)行數(shù)據(jù)庫語句的對象: */
Statement sta_ShuJuKuYuJuDuiXiang = con_LianJieDuiXiang
.createStatement();
/* 下面是SQL語句: */
String sql = "select * from shangpin a inner join sp_Type b "
+ "on a.sp_TypeID=b.sp_TypeID";
/* 執(zhí)行SQL語句,得到結(jié)果集,即數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表: */
ResultSet res = sta_ShuJuKuYuJuDuiXiang.executeQuery(sql);
/*
下面是打印商品內(nèi)容,這樣“下面是......:”的注釋方式很嚴(yán)謹(jǐn)和容易一行一行地讀懂, 感受到編程的嚴(yán)謹(jǐn)和邏輯性強(qiáng)等:
*/
/* 下面是提示的話語,即列名: */
System.out.println("商品編號\t商品名稱\t價(jià)格\t類型名稱\t類型介紹");
while (res.next()) {
System.out.println(res.getInt("sp_ID") + "\t"
+ res.getString("sp_Name") + "\t"
+ res.getInt("sp_Price") + "\t"
+ res.getString("sp_TypeName") + "\t"
+ res.getString("sp_Jieshao"));
}
/* XiuGaiShangpinDeID為接收顧客輸入的要修改的商品的ID */
int XiuGaiShangpinDeID = s.nextInt();
/* 下面是SQL查詢語句: */
String sql_Select_one = "select * from shangpin a "
+ "inner join sp_Type b on a.sp_TypeID=b.sp_TypeID where sp_ID="
+ XiuGaiShangpinDeID + "";
/* 下面是命名為res_Select_Dantiao,查詢單條商品數(shù)據(jù)的數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表: */
res_Select_Dantiao = sta_ShuJuKuYuJuDuiXiang
.executeQuery(sql_Select_one);
/*
* 結(jié)果集,即數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表判斷不出里面有沒有內(nèi)容,所以將這個(gè)數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表 轉(zhuǎn)換為集合,
* 寫一個(gè)轉(zhuǎn)換集合的方法,下面將數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表 轉(zhuǎn)換為集合:
*/
ArrayList<shangpin> arr_sp_DianGe = getSp(res_Select_Dantiao);
/*
* arr_sp_DianGe.size()是內(nèi)容的大小,長度,為字節(jié)等的數(shù)值長度, 字節(jié)等的數(shù)值長度>0,
* 說明有字節(jié)等,判斷這個(gè)集合中有沒有數(shù)據(jù),有則用戶輸入的編號是正確的,沒有則商品不存在 ,
* 下面是一個(gè)條件語句的片段,如果有數(shù)據(jù)就繼續(xù)執(zhí)行修改內(nèi)容程序的代碼
*/
if (arr_sp_DianGe.size() > 0) {
for (shangpin sp : arr_sp_DianGe) {
System.out.println(sp);
}
System.out.println("請選擇你要修改該商品的哪項(xiàng)內(nèi)容");
System.out.println("1.名稱? ?2.價(jià)格? ?3.類別? ? 4.介紹");
/* 下面可能只能一個(gè)一個(gè)判斷,可能無法循環(huán) */
int XiuGaiDeShangpinNeiRong = s.nextInt();
if (XiuGaiDeShangpinNeiRong == 1) {
System.out.println("請輸入該商品的新名稱");
String sp_NewName = s.next();
/* 下面是SQL修改語句: */
String sql_Update_spName = "update shangpin set sp_Name='"
+ sp_NewName + "' where sp_ID="
+ XiuGaiShangpinDeID + "";
/*
* 下面的executeUpdate是“執(zhí)行(execute)修改(Update)”
* sta_ShuJuKuYuJuDuiXiang為執(zhí)行 數(shù)據(jù)庫語句的對象
*/
if (sta_ShuJuKuYuJuDuiXiang
.executeUpdate(sql_Update_spName) > 0) {
System.out.println("商品名稱修改成功");
} else {
System.out.println("修改失敗,請重試");
}
} else if (XiuGaiDeShangpinNeiRong == 2) {
System.out.println("請輸入該商品的新價(jià)格");
String sp_NewName = s.next();
String sql_Update_spName = "update shangpin set sp_price='"
+ sp_NewName + "' where sp_ID="
+ XiuGaiShangpinDeID + "";
if (sta_ShuJuKuYuJuDuiXiang
.executeUpdate(sql_Update_spName) > 0) {
System.out.println("商品價(jià)格修改成功");
} else {
System.out.println("修改失敗,請重試");
}
} else if (XiuGaiDeShangpinNeiRong == 3) {
System.out.println("---執(zhí)行修改商品類型的操作---");
String sql_select_sp_Type = "select * from sp_Type";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 下面 sta_ShuJuKuYuJuDuiXiang是建立執(zhí)行數(shù)據(jù)庫語句的對象: */
/* 執(zhí)行SQL語句,得到結(jié)果集,即數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表: */
ResultSet res_type = sta_ShuJuKuYuJuDuiXiang
.executeQuery(sql_select_sp_Type);
System.out.println("類型ID\t類型名稱");
/* 下面是打印商品類型內(nèi)容: */
while (res_type.next()) {
System.out.println(res_type.getInt("sp_TypeID") + "\t"
+ res_type.getString("sp_TypeName"));
}
/* ShuRuDeShangPinTypeID 為接收用戶輸入的商品類型ID */
int ShuRuDeShangPinTypeID = s.nextInt();
sql_select_sp_Type = "update shangpin set sp_TypeID='"
+ ShuRuDeShangPinTypeID + "' where sp_ID="
+ XiuGaiShangpinDeID + "";
if (sta_ShuJuKuYuJuDuiXiang
.executeUpdate(sql_select_sp_Type) > 0) {
System.out.println("商品類型修改成功");
} else {
System.out.println("修改失敗,請重試");
}
} else if (XiuGaiDeShangpinNeiRong == 4) {
System.out.println("請輸入該商品的新介紹");
String sp_NewJieShao = s.next();
String sql_Update_sp_Jieshao = "update shangpin set sp_Jieshao='"
+ sp_NewJieShao
+ "' where sp_ID="
+ XiuGaiShangpinDeID + "";
if (sta_ShuJuKuYuJuDuiXiang
.executeUpdate(sql_Update_sp_Jieshao) > 0) {
System.out.println("商品介紹修改成功");
} else {
System.out.println("修改失敗,請重試");
}
}
} else {
System.out.println("編號輸入錯(cuò)誤");
}
} else if (ShangPinCaoZuo == 3) {
System.out.println("---執(zhí)行增加商品操作---");
System.out.println("請輸入商品的名稱");
String sp_Name = s.next();
System.out.println("請輸入商品的價(jià)格");
double sp_Price = s.nextDouble();
System.out.println("請輸入商品的類型ID");
/* 下面是去數(shù)據(jù)庫中查詢一下有哪些類型 */
/* 1.下面是用Class.forName()創(chuàng)建數(shù)據(jù)庫表和內(nèi)容,導(dǎo)入包,導(dǎo)入說明書,導(dǎo)入jar包: */
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 2.下面是建立鏈接對象: */
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/* 3.下面是建立執(zhí)行數(shù)據(jù)庫語句的對象: */
Statement sta = con.createStatement();
/* 4.下面是SQL語句 */
String sql = "select * from sp_Type";
/* 5.執(zhí)行sql語句,得到數(shù)據(jù)庫結(jié)果集的數(shù)據(jù)表 */
ResultSet res = sta.executeQuery(sql);
System.out.println("類型ID\t類型名稱");
while (res.next()) {
System.out.println(res.getInt(1) + "\t" + res.getString(2));
}
int sp_TypeID = s.nextInt();
System.out.println("請輸入商品的介紹");
String sp_jieshao = s.next();
/* 下面是SQL增加語句,增加輸入的商品內(nèi)容 */
sql = "insert into shangpin values('" + sp_Name + "'," + sp_Price
+ "," + sp_TypeID + ",'" + sp_jieshao + "')";
if (sta.executeUpdate(sql) > 0) {
System.out.println("商品添加成功!");
} else {
System.out.println("添加失敗,請重試!");
}
} else if (ShangPinCaoZuo == 4) {
System.out.println("---執(zhí)行刪除商品操作---");
System.out.println("請輸入你要?jiǎng)h除的商品ID");
/*
先創(chuàng)建數(shù)據(jù)庫表和內(nèi)容,導(dǎo)入包,即導(dǎo)入說明書,即導(dǎo)入jar包, Class.forName()
方法的含義是:加載參數(shù)指定的類,并且初始化它。:
*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 然后建立鏈接對象賦值給左邊的Connection(連接): */
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/*
然后用用con.獲取數(shù)據(jù)的createStatement(),
* 建立執(zhí)行數(shù)據(jù)庫語句的對象再右邊被賦值給左邊的被命名為sta的Statement(聲明):
*/
Statement sta = con.createStatement();
String sql = "select * from shangpin a inner join sp_Type b "
+ "on a.sp_TypeID=b.sp_TypeID";
/* 下面數(shù)據(jù)庫結(jié)果集(Result)的數(shù)據(jù)表(set),通過"東西被命名的名字."的形式來獲取東西的數(shù)據(jù) */
ResultSet res = sta.executeQuery(sql);
System.out.println("商品編號\t商品名稱\t價(jià)格\t類型名稱\t類型介紹");
/* 下面是打印內(nèi)容 */
while (res.next()) {
System.out.println(res.getInt("sp_ID") + "\t"
+ res.getString("sp_Name") + "\t"
+ res.getInt("sp_Price") + "\t"
+ res.getString("sp_TypeName") + "\t"
+ res.getString("sp_Jieshao"));
}
int num_spID_Delete = s.nextInt();
/* 下面是SQL刪除語句 */
sql = "delete shangpin where sp_ID=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setInt(1, num_spID_Delete);
if (ps.executeUpdate() > 0) {
System.out.println("刪除成功");
} else {
System.out.println("刪除失敗,請重試");
}
} else {
System.out.println("編號輸入錯(cuò)誤");
}
}
}





個(gè)人總結(jié)的注意事項(xiàng):
SQL執(zhí)行順序問題(要按照一下順序執(zhí)行代碼,不然無論在SQL還是Java中都會(huì)報(bào)錯(cuò),比如SQL中刪除完某種商品后才刪得了商品類型,填加商品類型的內(nèi)容后才可添加商品內(nèi)容,Java中也是刪除完某種商品后才刪得了商品類型,但有時(shí)可以直接刪,我遇到過這種情況,有時(shí)編譯器會(huì)有bug):




不按順序的后果:





按順序的成功:









