JDBC商品管理系統(tǒng)(無(wú)登錄注冊(cè)功能完善版),知識(shí)和單詞收集整理

ResultSet:

ClassNotFoundException:

SQLException:




executeUpdate:


Statement:

Connection:

Class.forName:



Driver:

microsoft:

PreparedStatement:




utils:


--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,'很好看和感人的書!' )
,('《詩(shī)書畫唱傳》',7,1, '一本人物自傳' )
,('TOP1曲CD',6,2,'好聽(tīng)!' )
,('熱曲CD!',8,2,'好聽(tīng)!點(diǎn)贊!' )
,('女性機(jī)器人',99,3,'魅力無(wú)限' )
,('男性機(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)




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.Scanner;
public class mains {
/* 這個(gè)GouWuChe集合為購(gòu)物車: */
public static ArrayList<shangpin> GouWuChe = new ArrayList<shangpin>();
/* ResultSet,數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表,通常通過(guò)執(zhí)行查詢數(shù)據(jù)庫(kù)的語(yǔ)句生成: */
public static ResultSet res_Select_Dantiao = null;
/* 下面是為了給全局能夠接受輸入的數(shù)據(jù): */
public static Scanner s = new Scanner(System.in);
/* 下面是封裝打印商品內(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;
}
/*
下面是主函數(shù),寫重要的程序:(主要設(shè)計(jì)的思路:
一.購(gòu)買
購(gòu)買的時(shí)候要做的事: 1.提示用戶輸入購(gòu)買的商品ID并遍歷商品
?2.接收用戶輸入的商品的ID 3.去數(shù)據(jù)庫(kù)里去查一下這個(gè)商品的ID的商品是否存在,存在就把這件商品放入到購(gòu)物車中,不存在就提示用戶商品ID輸入錯(cuò)誤)?
二.商品管理?
用戶暫時(shí)是不知道有那些商品,所以要去數(shù)據(jù)庫(kù)去查詢所有的商品,并遍歷出來(lái)讓用戶輸入商品的ID,把遍歷商品的代碼 封裝起來(lái),會(huì)很方便,要用時(shí)調(diào)用遍歷的方法就可以了
?三.購(gòu)物車?
先假設(shè)這個(gè)商品是存在的。 如果商品存在的話,需要就將這件商品放入到購(gòu)物車中,
購(gòu)物車其實(shí)也可以設(shè)置一張數(shù)據(jù)庫(kù)表代替,但是現(xiàn)在為了方便直接聲明一個(gè)集合當(dāng)做購(gòu)物車?
四.連接數(shù)據(jù)庫(kù),將Java語(yǔ)句和SQL語(yǔ)句聯(lián)系起來(lái)
1.首先要有數(shù)據(jù)源,所以先創(chuàng)建數(shù)據(jù)庫(kù)表和內(nèi)容。 (Class.forName() 方法的含義是:加載參數(shù)指定的類,并且初始化它。)
2.導(dǎo)入包,即導(dǎo)入說(shuō)明書,即導(dǎo)入jar包。
3.建立鏈接對(duì)象?
4.建立執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句的對(duì)象?
5.執(zhí)行SQL語(yǔ)句,得到結(jié)果集,即數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表
?6.遍歷結(jié)果集,即遍歷數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表 五.增加商品等 提示用戶輸入增加商品的名稱,價(jià)格,類型,介紹
用戶名和數(shù)據(jù)庫(kù)的名字要改對(duì)
*/
public static void main(String[] args) throws Exception {
/* 下面用了循環(huán): */
while (true) {
System.out.println("請(qǐng)輸入你要執(zhí)行的操作:1.商品管理? 2.購(gòu)買? ?3.購(gòu)物車");
/* CaoZuo為操作: */
int CaoZuo = s.nextInt();
if (CaoZuo == 1) {
/* 下面是調(diào)用了封裝的方法,shangpinguanli()為封裝“商品管理”的方法 */
shangpinguanli();
} else if (CaoZuo == 2) {
System.out.println("---執(zhí)行購(gòu)買商品操作---");
System.out.println("請(qǐng)輸入你要購(gòu)買的商品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òu)買哪個(gè)商品的ID,ID就是編號(hào): */
int ShangPinID = s.nextInt();
/* 下面是SQL查找單件商品的語(yǔ)句: */
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ù)庫(kù)結(jié)果集的數(shù)據(jù)表: */
ResultSet res_ShuJuKu_JieGuoJi_ShuJvBiao = diaoyongSQL
.Select(sql_select_DianJian);
/*
* 下面是調(diào)用SQL查找的封裝方法,放在右邊賦值給被命名為左邊的數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表, 將查詢單件商品的內(nèi)容也轉(zhuǎn)為ArrayList
*/
ArrayList<shangpin> arr_sp_DianGe = getSp(res_ShuJuKu_JieGuoJi_ShuJvBiao);
/*arr_sp_DianGe.size()是內(nèi)容的大小,長(zhǎng)度,為字節(jié)等的數(shù)值長(zhǎng)度, 字節(jié)等的數(shù)值長(zhǎng)度>0,說(shuō)明有字節(jié)等,判斷這個(gè)集合中有沒(méi)有數(shù)據(jù),有則用戶輸入的編號(hào)是正確的,沒(méi)有則商品不存在
*/
if (arr_sp_DianGe.size() > 0) {
/* 下面是一個(gè)被命名為sp_GouWuChe的集合: */
shangpin sp_GouWuChe = new shangpin();
/* 下面將購(gòu)買內(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ì)幾乎每一行都寫注釋,就算前面講過(guò)我也寫,方便理解,防遺忘而重新找和看注釋,編程有很多將意思進(jìn)行縮寫,省事,省數(shù)據(jù)的表達(dá)和執(zhí)行的方式。很多抽象的東西也是一種 省事,省數(shù)據(jù)的表達(dá)和執(zhí)行的方式:
*/
GouWuChe.add(sp_GouWuChe);
System.out.println("商品添加購(gòu)物車成功!");
} else {
System.out.println("商品ID不存在");
}
} else if (CaoZuo == 3) {
/* 下面是購(gòu)物車,遍歷打印購(gòu)物車集合中的內(nèi)容,查詢購(gòu)物車有沒(méi)有商品 */
System.out.println("---執(zhí)行遍歷購(gòu)物車功能---");
System.out.println("購(gòu)物車現(xiàn)在的商品有:");
for (shangpin i : GouWuChe) {
System.out.println(i);
}
}
}
}
/* 下面是一個(gè)拋出找不到指定類和SQL語(yǔ)句執(zhí)行時(shí)出現(xiàn)錯(cuò)誤的異常的封裝的方法: */
private static void shangpinguanli() throws ClassNotFoundException,
SQLException {
System.out.println("執(zhí)行商品操作");
System.out.println("1.查詢商品? ?2.根據(jù)用戶輸入編號(hào)修改商品信息? ? ?3.增加商品? ? 4.刪除商品");
int ShangPinCaoZuo = s.nextInt();
if (ShangPinCaoZuo == 1) {
System.out.println("執(zhí)行查詢所有商品操作");
/* 下面是創(chuàng)建數(shù)據(jù)庫(kù)表和內(nèi)容,導(dǎo)入包,即導(dǎo)入說(shuō)明書,即導(dǎo)入jar包: */
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 下面是建立鏈接對(duì)象: */
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/* 下面是建立執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句的對(duì)象: */
Statement sta = con.createStatement();
/* 下面是SQL語(yǔ)句: */
String sql = "select * from shangpin a inner "
+ "join sp_Type b on a.sp_TypeID=b.sp_TypeID";
/* 執(zhí)行SQL語(yǔ)句,得到結(jié)果集,即數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表: */
ResultSet res = sta.executeQuery(sql);
/* 下面是提示的話語(yǔ),即列名: */
System.out.println("商品編號(hào)\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("請(qǐng)輸入你要修改的商品編號(hào)");
/* 下面是創(chuàng)建數(shù)據(jù)庫(kù)表和內(nèi)容,導(dǎo)入包,即導(dǎo)入說(shuō)明書,即導(dǎo)入jar包: */
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 下面是建立鏈接對(duì)象: */
Connection con_LianJieDuiXiang = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/* 下面是建立執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句的對(duì)象: */
Statement sta_ShuJuKuYuJuDuiXiang = con_LianJieDuiXiang
.createStatement();
/* 下面是SQL語(yǔ)句: */
String sql = "select * from shangpin a inner join sp_Type b on a.sp_TypeID=b.sp_TypeID";
/* 執(zhí)行SQL語(yǔ)句,得到結(jié)果集,即數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表: */
ResultSet res = sta_ShuJuKuYuJuDuiXiang.executeQuery(sql);
/*
下面是打印商品內(nèi)容,這樣“下面是......:”的注釋方式很嚴(yán)謹(jǐn)和容易一行一行地讀懂, 感受到編程的嚴(yán)謹(jǐn)和邏輯性強(qiáng)等:
*/
/* 下面是提示的話語(yǔ),即列名: */
System.out.println("商品編號(hào)\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查詢語(yǔ)句: */
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ù)庫(kù)結(jié)果集的數(shù)據(jù)表: */
res_Select_Dantiao = sta_ShuJuKuYuJuDuiXiang
.executeQuery(sql_Select_one);
/*
結(jié)果集,即數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表判斷不出里面有沒(méi)有內(nèi)容,所以將這個(gè)數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表 轉(zhuǎn)換為集合,寫一個(gè)轉(zhuǎn)換集合的方法,下面將數(shù)據(jù)庫(kù)結(jié)果集的數(shù)據(jù)表 轉(zhuǎn)換為集合:
*/
ArrayList<shangpin> arr_sp_DianGe = getSp(res_Select_Dantiao);
/*
?arr_sp_DianGe.size()是內(nèi)容的大小,長(zhǎng)度,為字節(jié)等的數(shù)值長(zhǎng)度, 字節(jié)等的數(shù)值長(zhǎng)度>0,說(shuō)明有字節(jié)等,判斷這個(gè)集合中有沒(méi)有數(shù)據(jù),有則用戶輸入的編號(hào)是正確的,沒(méi)有則商品不存在?,下面是一個(gè)條件語(yǔ)句的片段,如果有數(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("請(qǐng)選擇你要修改該商品的哪項(xiàng)內(nèi)容");
System.out.println("1.名稱? ?2.價(jià)格? ?3.類別? ? 4.介紹");
/* 下面可能只能一個(gè)一個(gè)判斷,可能無(wú)法循環(huán) */
int XiuGaiDeShangpinNeiRong = s.nextInt();
if (XiuGaiDeShangpinNeiRong == 1) {
System.out.println("請(qǐng)輸入該商品的新名稱");
String sp_NewName = s.next();
/* 下面是SQL修改語(yǔ)句: */
String sql_Update_spName = "update shangpin set sp_Name='"
+ sp_NewName + "' where sp_ID="
+ XiuGaiShangpinDeID + "";
/*
* 下面的executeUpdate是“執(zhí)行(execute)修改(Update)”
* sta_ShuJuKuYuJuDuiXiang為執(zhí)行 數(shù)據(jù)庫(kù)語(yǔ)句的對(duì)象
*/
if (sta_ShuJuKuYuJuDuiXiang
.executeUpdate(sql_Update_spName) > 0) {
System.out.println("商品名稱修改成功");
} else {
System.out.println("修改失敗,請(qǐng)重試");
}
} else if (XiuGaiDeShangpinNeiRong == 2) {
System.out.println("請(qǐng)輸入該商品的新價(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("修改失敗,請(qǐng)重試");
}
} 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ù)庫(kù)語(yǔ)句的對(duì)象: */
/* 執(zhí)行SQL語(yǔ)句,得到結(jié)果集,即數(shù)據(jù)庫(kù)結(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("修改失敗,請(qǐng)重試");
}
} else if (XiuGaiDeShangpinNeiRong == 4) {
System.out.println("請(qǐng)輸入該商品的新介紹");
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("修改失敗,請(qǐng)重試");
}
}
} else {
System.out.println("編號(hào)輸入錯(cuò)誤");
}
} else if (ShangPinCaoZuo == 3) {
System.out.println("---執(zhí)行增加商品操作---");
System.out.println("請(qǐng)輸入商品的名稱");
String sp_Name = s.next();
System.out.println("請(qǐng)輸入商品的價(jià)格");
double sp_Price = s.nextDouble();
System.out.println("請(qǐng)輸入商品的類型ID");
/* 下面是去數(shù)據(jù)庫(kù)中查詢一下有哪些類型 */
/* 1.下面是用Class.forName()創(chuàng)建數(shù)據(jù)庫(kù)表和內(nèi)容,導(dǎo)入包,導(dǎo)入說(shuō)明書,導(dǎo)入jar包: */
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 2.下面是建立鏈接對(duì)象: */
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/* 3.下面是建立執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句的對(duì)象: */
Statement sta = con.createStatement();
/* 4.下面是SQL語(yǔ)句 */
String sql = "select * from sp_Type";
/* 5.執(zhí)行sql語(yǔ)句,得到數(shù)據(jù)庫(kù)結(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("請(qǐng)輸入商品的介紹");
String sp_jieshao = s.next();
/* 下面是SQL增加語(yǔ)句,增加輸入的商品內(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("添加失敗,請(qǐng)重試!");
}
} else if (ShangPinCaoZuo == 4) {
System.out.println("---執(zhí)行刪除商品操作---");
System.out.println("請(qǐng)輸入你要?jiǎng)h除的商品ID");
/*
* 先創(chuàng)建數(shù)據(jù)庫(kù)表和內(nèi)容,導(dǎo)入包,即導(dǎo)入說(shuō)明書,即導(dǎo)入jar包, Class.forName()
* 方法的含義是:加載參數(shù)指定的類,并且初始化它。:
*/
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
/* 然后建立鏈接對(duì)象賦值給左邊的Connection(連接): */
Connection con = DriverManager.getConnection(
"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",
"1234abcd");
/*然后用用con.獲取數(shù)據(jù)的createStatement(),建立執(zhí)行數(shù)據(jù)庫(kù)語(yǔ)句的對(duì)象再右邊被賦值給左邊的被命名為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ù)庫(kù)結(jié)果集(Result)的數(shù)據(jù)表(set),通過(guò)"東西被命名的名字."的形式來(lái)獲取東西的數(shù)據(jù) */
ResultSet res = sta.executeQuery(sql);
System.out.println("商品編號(hào)\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刪除語(yǔ)句 */
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("刪除失敗,請(qǐng)重試");
}
} else {
System.out.println("編號(hào)輸入錯(cuò)誤");
}
}
}


package JDBC;
public class shangpin {
private int sp_ID;
private String sp_Jieshao;
private String sp_Name;
private double sp_Price;
private sp_Type sp_TypeID;
public shangpin() {
}
public shangpin(int sp_ID, String sp_Name, double sp_Price,
sp_Type sp_TypeID, String sp_Jieshao) {
this.sp_ID = sp_ID;
this.sp_Name = sp_Name;
this.sp_Price = sp_Price;
this.sp_TypeID = sp_TypeID;
this.sp_Jieshao = sp_Jieshao;
}
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 + "]";
}
}


package JDBC;
public class sp_Type {
private int sp_TypeID;
private String sp_TypeName;
public sp_Type() {
}
public sp_Type(int sp_TypeID, String sp_TypeName) {
this.sp_TypeID = sp_TypeID;
this.sp_TypeName = sp_TypeName;
}
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 + "]";
}
}


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;
}
}

