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

JDBC商品管理系统(无登录注册功能完善版),知识和单词收集整理

2020-03-27 21:36 作者:诗书画唱  | 我要投稿

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')


,('机器人'),('装备')




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


,('《诗书画唱传》',7,1, '一本人物自传' )


,('TOP1曲CD',6,2,'好听!' )


,('热曲CD!',8,2,'好听!点赞!' )


,('女性机器人',99,3,'魅力无限' )


,('男性机器人',77,3,'可满足幻想!' )




insert into shangpin values('上天装备',19,4,'炫酷体验!' )


,('盔甲装备',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 {

/* 这个GouWuChe集合为购物车: */

public static ArrayList<shangpin> GouWuChe = new ArrayList<shangpin>();

/* ResultSet,数据库结果集的数据表,通常通过执行查询数据库的语句生成: */

public static ResultSet res_Select_Dantiao = null;


/* 下面是为了给全局能够接受输入的数据: */


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


/* 下面是封装打印商品内容的方法 */

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;

}


/*

下面是主函数,写重要的程序:(主要设计的思路:

一.购买

购买的时候要做的事: 1.提示用户输入购买的商品ID并遍历商品

 2.接收用户输入的商品的ID 3.去数据库里去查一下这个商品的ID的商品是否存在,存在就把这件商品放入到购物车中,不存在就提示用户商品ID输入错误) 

二.商品管理 

用户暂时是不知道有那些商品,所以要去数据库去查询所有的商品,并遍历出来让用户输入商品的ID,把遍历商品的代码 封装起来,会很方便,要用时调用遍历的方法就可以了


 三.购物车 

先假设这个商品是存在的。 如果商品存在的话,需要就将这件商品放入到购物车中,

购物车其实也可以设置一张数据库表代替,但是现在为了方便直接声明一个集合当做购物车 


四.连接数据库,将Java语句和SQL语句联系起来


1.首先要有数据源,所以先创建数据库表和内容。 (Class.forName() 方法的含义是:加载参数指定的类,并且初始化它。)

2.导入包,即导入说明书,即导入jar包。

3.建立链接对象 

4.建立执行数据库语句的对象 

5.执行SQL语句,得到结果集,即数据库结果集的数据表

 6.遍历结果集,即遍历数据库结果集的数据表 五.增加商品等 提示用户输入增加商品的名称,价格,类型,介绍


用户名和数据库的名字要改对

*/

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

/* 下面用了循环: */

while (true) {

System.out.println("请输入你要执行的操作:1.商品管理  2.购买   3.购物车");

/* CaoZuo为操作: */

int CaoZuo = s.nextInt();

if (CaoZuo == 1) {

/* 下面是调用了封装的方法,shangpinguanli()为封装“商品管理”的方法 */

shangpinguanli();

} else if (CaoZuo == 2) {


System.out.println("---执行购买商品操作---");

System.out.println("请输入你要购买的商品ID");


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

+ " sp_Type b on a.sp_TypeID=b.sp_TypeID";

/* 下面是调用SQL查找的封装方法: */

ResultSet res_sp = diaoyongSQL.Select(sql);

/*

* 调用getSp(res_sp)这个封装打印商品内容的方法, 在右边赋值给左边的被命名为arr_sp的

* 只接收shangpin类内容的ArrayList集 ,遍历了商品内容:

*/

ArrayList<shangpin> arr_sp = getSp(res_sp);


/* 下面将得到的集合遍历了一遍: */

for (shangpin a : arr_sp) {

System.out.println(a);

}

/* 接受用户要购买哪个商品的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

+ "";

/* 下面是调用SQL查找的封装方法,放在右边赋值给被命名为左边的数据库结果集的数据表: */

ResultSet res_ShuJuKu_JieGuoJi_ShuJvBiao = diaoyongSQL

.Select(sql_select_DianJian);

/*

* 下面是调用SQL查找的封装方法,放在右边赋值给被命名为左边的数据库结果集的数据表, 将查询单件商品的内容也转为ArrayList

*/

ArrayList<shangpin> arr_sp_DianGe = getSp(res_ShuJuKu_JieGuoJi_ShuJvBiao);

/*arr_sp_DianGe.size()是内容的大小,长度,为字节等的数值长度, 字节等的数值长度>0,说明有字节等,判断这个集合中有没有数据,有则用户输入的编号是正确的,没有则商品不存在

*/

if (arr_sp_DianGe.size() > 0) {

/* 下面是一个被命名为sp_GouWuChe的集合: */

shangpin sp_GouWuChe = new shangpin();

/* 下面将购买内容加到了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集合中,我会几乎每一行都写注释,就算前面讲过我也写,方便理解,防遗忘而重新找和看注释,编程有很多将意思进行缩写,省事,省数据的表达和执行的方式。很多抽象的东西也是一种 省事,省数据的表达和执行的方式:

*/

GouWuChe.add(sp_GouWuChe);

System.out.println("商品添加购物车成功!");


} else {


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

}


} else if (CaoZuo == 3) {

/* 下面是购物车,遍历打印购物车集合中的内容,查询购物车有没有商品 */

System.out.println("---执行遍历购物车功能---");

System.out.println("购物车现在的商品有:");

for (shangpin i : GouWuChe) {

System.out.println(i);

}

}

}

}


/* 下面是一个抛出找不到指定类和SQL语句执行时出现错误的异常的封装的方法: */

private static void shangpinguanli() throws ClassNotFoundException,

SQLException {

System.out.println("执行商品操作");

System.out.println("1.查询商品   2.根据用户输入编号修改商品信息     3.增加商品    4.删除商品");

int ShangPinCaoZuo = s.nextInt();

if (ShangPinCaoZuo == 1) {


System.out.println("执行查询所有商品操作");

/* 下面是创建数据库表和内容,导入包,即导入说明书,即导入jar包: */

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

/* 下面是建立链接对象: */

Connection con = DriverManager.getConnection(

"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",

"1234abcd");

/* 下面是建立执行数据库语句的对象: */

Statement sta = con.createStatement();

/* 下面是SQL语句: */

String sql = "select * from shangpin a inner "

+ "join sp_Type b on a.sp_TypeID=b.sp_TypeID";

/* 执行SQL语句,得到结果集,即数据库结果集的数据表: */

ResultSet res = sta.executeQuery(sql);


/* 下面是提示的话语,即列名: */

System.out.println("商品编号\t商品名称\t价格\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"));

}

} else if (ShangPinCaoZuo == 2) {


System.out.println("---执行修改商品操作---");

System.out.println("请输入你要修改的商品编号");

/* 下面是创建数据库表和内容,导入包,即导入说明书,即导入jar包: */

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

/* 下面是建立链接对象: */

Connection con_LianJieDuiXiang = DriverManager.getConnection(

"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",

"1234abcd");

/* 下面是建立执行数据库语句的对象: */

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

/* 执行SQL语句,得到结果集,即数据库结果集的数据表: */

ResultSet res = sta_ShuJuKuYuJuDuiXiang.executeQuery(sql);

/*

下面是打印商品内容,这样“下面是......:”的注释方式很严谨和容易一行一行地读懂, 感受到编程的严谨和逻辑性强等:

*/

/* 下面是提示的话语,即列名: */

System.out.println("商品编号\t商品名称\t价格\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,查询单条商品数据的数据库结果集的数据表: */

res_Select_Dantiao = sta_ShuJuKuYuJuDuiXiang

.executeQuery(sql_Select_one);

/*

结果集,即数据库结果集的数据表判断不出里面有没有内容,所以将这个数据库结果集的数据表 转换为集合,写一个转换集合的方法,下面将数据库结果集的数据表 转换为集合:

*/

ArrayList<shangpin> arr_sp_DianGe = getSp(res_Select_Dantiao);


/*

 arr_sp_DianGe.size()是内容的大小,长度,为字节等的数值长度, 字节等的数值长度>0,说明有字节等,判断这个集合中有没有数据,有则用户输入的编号是正确的,没有则商品不存在 ,下面是一个条件语句的片段,如果有数据就继续执行修改内容程序的代码

*/

if (arr_sp_DianGe.size() > 0) {


for (shangpin sp : arr_sp_DianGe) {

System.out.println(sp);

}

System.out.println("请选择你要修改该商品的哪项内容");

System.out.println("1.名称   2.价格   3.类别    4.介绍");

/* 下面可能只能一个一个判断,可能无法循环 */

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是“执行(execute)修改(Update)”

* sta_ShuJuKuYuJuDuiXiang为执行 数据库语句的对象

*/

if (sta_ShuJuKuYuJuDuiXiang

.executeUpdate(sql_Update_spName) > 0) {

System.out.println("商品名称修改成功");

} else {

System.out.println("修改失败,请重试");

}

} else if (XiuGaiDeShangpinNeiRong == 2) {


System.out.println("请输入该商品的新价格");

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("商品价格修改成功");

} else {

System.out.println("修改失败,请重试");

}

} else if (XiuGaiDeShangpinNeiRong == 3) {


System.out.println("---执行修改商品类型的操作---");


String sql_select_sp_Type = "select * from sp_Type";

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

/* 下面 sta_ShuJuKuYuJuDuiXiang是建立执行数据库语句的对象: */

/* 执行SQL语句,得到结果集,即数据库结果集的数据表: */

ResultSet res_type = sta_ShuJuKuYuJuDuiXiang

.executeQuery(sql_select_sp_Type);

System.out.println("类型ID\t类型名称");

/* 下面是打印商品类型内容: */

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("编号输入错误");

}


} else if (ShangPinCaoZuo == 3) {


System.out.println("---执行增加商品操作---");

System.out.println("请输入商品的名称");

String sp_Name = s.next();

System.out.println("请输入商品的价格");

double sp_Price = s.nextDouble();

System.out.println("请输入商品的类型ID");

/* 下面是去数据库中查询一下有哪些类型 */

/* 1.下面是用Class.forName()创建数据库表和内容,导入包,导入说明书,导入jar包: */

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

/* 2.下面是建立链接对象: */

Connection con = DriverManager.getConnection(

"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",

"1234abcd");

/* 3.下面是建立执行数据库语句的对象: */

Statement sta = con.createStatement();

/* 4.下面是SQL语句 */

String sql = "select * from sp_Type";

/* 5.执行sql语句,得到数据库结果集的数据表 */

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增加语句,增加输入的商品内容 */

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("---执行删除商品操作---");

System.out.println("请输入你要删除的商品ID");


/*

*创建数据库表和内容,导入包,即导入说明书,即导入jar包, Class.forName()

* 方法的含义是:加载参数指定的类,并且初始化它。:

*/

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

/* 然后建立链接对象赋值给左边的Connection(连接): */

Connection con = DriverManager.getConnection(

"jdbc:sqlserver://localhost;databaseName=yonghu", "sa",

"1234abcd");

/*然后用用con.获取数据的createStatement(),建立执行数据库语句的对象再右边被赋值给左边的被命名为sta的Statement(声明):

*/

Statement sta = con.createStatement();


String sql = "select * from shangpin a inner join sp_Type b "

+ "on a.sp_TypeID=b.sp_TypeID";

/* 下面数据库结果集(Result)的数据表(set),通过"东西被命名的名字."的形式来获取东西的数据 */

ResultSet res = sta.executeQuery(sql);


System.out.println("商品编号\t商品名称\t价格\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"));

}


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("编号输入错误");

}

}

}


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;


}


}





JDBC商品管理系统(无登录注册功能完善版),知识和单词收集整理的评论 (共 条)

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