第一次博客 有错误可以私信我 谢谢大家的指正。
如果写的可以希望大家可以关注一下,会继续更新学习历程 一起学习进步。
源码在最后
DataSouce.properties 配置类信息文件
# 数据库配置信息
jdbc_url=jdbc:mysql://localhost:3306/tables
jdbc_driver=com.mysql.cj.jdbc.Driver
jdbc_user=123
jdbc_password=123
Env类 读取属性文件的组件类
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* 读取属性文件加载类
*/
public final class Env extends Properties {
public static final String JDBC_URL;
public static final String JDBC_DRIVER;
public static final String JDBC_USER;
public static final String JDBC_PASSWORD;
public static Env env;
/*
数据库属性文件的路径和名称
*/
public static final String CONF_FILE="com\\sjtest\\conf\\DataSouce.properties";
static {
if(env==null){//当Env为空的时候赋值
env=new Env();
}
//获取属性文件的文件流
InputStream input = env.getClass().getClassLoader().getResourceAsStream(CONF_FILE);
try {
env.load(input);//加载文件流
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//赋值
JDBC_URL=env.getProperty("jdbc_url");
JDBC_DRIVER=env.getProperty("jdbc_driver");
JDBC_USER=env.getProperty("jdbc_user");
JDBC_PASSWORD=env.getProperty("jdbc_password");
}
}
DataSourPool类 数据源管理组件类
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DataSourPool {
private static ComboPooledDataSource c3p0;
/**
* 创建 ComboPooledDataSource 数据源
*/
private static void createComboPooledDataSource(){
if(c3p0==null){//如果c3p0是空的 赋值
c3p0=new ComboPooledDataSource();
}
/*
数据源相关属性
*/
try {
c3p0.setDriverClass(Env.JDBC_DRIVER);
c3p0.setUser(Env.JDBC_USER);
c3p0.setPassword(Env.JDBC_PASSWORD);
c3p0.setJdbcUrl(Env.JDBC_URL);
} catch (PropertyVetoException e) {
e.printStackTrace();
}
}
/**
* 获取Connection 接口的方法
* @return
*/
public static Connection getConnection(){
Connection conn=null;
createComboPooledDataSource();//创建数据源
try {
conn=c3p0.getConnection();//有数据源获取打开一个连接
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return conn;
}
/*
方法重载
*/
/**
* 关闭方法
* @param conn
*/
public static void close(Connection conn) {
try {
if(conn!=null) {
conn.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(Statement state) {
try {
if (state!=null) {
state.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(ResultSet resu) {
try {
if (resu != null) {
resu.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
Goods类 商品的实体类
/**
* 映射商品表
*/
public class Goods {
//商品编号
private String id;
//商品名称
private String name;
//商品价格
private int price;
//商品数量
private int number;
/*
定义有参 无参的构造器
*/
public Goods() {
}
public Goods(String id, String name, int price, int number) {
this.id = id;
this.name = name;
this.price = price;
this.number = number;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
/*
重写toString方法
*/
@Override
public String toString() {
return "Goods{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", price=" + price +
", number=" + number +
'}';
}
}
Util 类 工具类 注意
import com.sjtest.Exception.NoneException;
import com.sjtest.pojo.Goods;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class Util {
/**
* 获取全部商品列表
*
* @return 全部商品信息
*/
public static List<Goods> AllGoods() {
List<Goods> list = new ArrayList<>();
String sql = "select id 商品编号 ,name 商品名称,price 商品价格, number 商品数量 from goods";
Connection conn = DataSourPool.getConnection();//获取数据库连接
PreparedStatement ps = null;//创建 PreparedStatement 接口
ResultSet set = null; //创建 ResultSet 接口
try {
ps = conn.prepareStatement(sql);//获取PreparedStatement 接口对象
set = ps.executeQuery(); //查询返回ResultSet 结果集对象
/*
处理结果集 封装结果集中的对象
*/
while (set.next()) {
Goods goods = new Goods();
goods.setId(set.getString(1));
goods.setName(set.getString(2));
goods.setPrice(set.getInt(3));
goods.setNumber(set.getInt(4));
list.add(goods);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {//关闭连接
DataSourPool.close(conn);
DataSourPool.close(ps);
DataSourPool.close(set);
}
return list;
}
/**
* 给一个id 查询 商品
*
* @param id 商品编号
* @return
*/
public static Goods findIDOutGoods(String id) {
List<Goods> list = Util.AllGoods();
for (Goods goods : list) {
if (goods.getId().equals(id)) {
return goods;
}
}
//如果目标不存在抛异常
try {
throw new NoneException("查找目标不存在");//自定义异常类
} catch (NoneException e) {
e.printStackTrace();
}
return null;
}
/**
* 显示所有id和名称
*/
public static void showIDAndName(){
List<Goods> list = Util.AllGoods();
for (Goods goods : list) {
System.out.println(goods.getId()+"---"+goods.getName());
}
}
}
GoodsDao接口 定义方法
import com.sjtest.pojo.Goods;
public interface GoodsDao {
/**
* 商品添加(输入商品编码、名称、价格、数量)
*
* @param goods 商品
*/
void addGoods(Goods goods);
/**
* 商品展示
*/
void showGoods();
/**
商品入库(输入商品编号,然后提示用户输入入库数量,进行入库)
* @param id 商品编号
*/
void inGoods(String id);
/**
* 商品出库(输入商品编号,提示用户输入出库数量,进行出库)
* @param id 商品编号
*/
void outGoods(String id);
/**
* 修改商品价格(提示用户输入编号,录入新价格)
* @param id 商品编号
*/
void setGoodsPrice(String id);
/**
* 删除商品
* @param id 商品编号
*/
void deleteGoods(String id);
}
public class ExistExcetion extends Exception{
public ExistExcetion() {
}
public ExistExcetion(String message) {
super(message);
}
}
其他两个一样
import com.sjtest.Exception.ExistExcetion;
import com.sjtest.Exception.NotNumberException;
import com.sjtest.dao.GoodsDao;
import com.sjtest.pojo.Goods;
import com.sjtest.util.DataSourPool;
import com.sjtest.util.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;
public class GoodsDaoImp implements GoodsDao {
Scanner scan = new Scanner(System.in);
//获取接口对象
Connection conn = null;
PreparedStatement ps = null;
ResultSet set = null;
@Override
public void addGoods(Goods goods) {
int res = 0;
List<Goods> list = Util.AllGoods();//获取所有商品
for (Goods goods1 : list) {
if(goods1.getId().equals(goods.getId())){
try {
throw new ExistExcetion("已经存在商品");
} catch (ExistExcetion existExcetion) {
existExcetion.printStackTrace();
}
}
}
String sql = " insert into goods(id,name,price,number) values(?,?,?,?)";
conn = DataSourPool.getConnection();
try {
ps = conn.prepareStatement(sql); //获取PreparedStatement 接口对象
/*
替换sql中的占位符
*/
ps.setString(1, goods.getId());
ps.setString(2, goods.getName());
ps.setInt(3, goods.getPrice());
ps.setInt(4, goods.getNumber());
res = ps.executeUpdate();//执行sql语句
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(conn);
DataSourPool.close(ps);
}
System.out.println("添加成功");
System.out.println("添加行数: " + res + "行");
}
@Override
public void showGoods() {
List<Goods> list = Util.AllGoods();
System.out.println("商品编号\t商品名称\t商品价格\t\t商品数量");
for (Goods goods : list) {
System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t"
+ goods.getPrice() + "\t\t\t" + goods.getNumber());
}
}
@Override
public void inGoods(String id) {
int res = 0;
String sql = "update goods set number =? where id=?";
try {
conn = DataSourPool.getConnection();//获取连接
ps = conn.prepareStatement(sql);
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println("商品 " + goods.getName() + "还有" + goods.getNumber() + "个");
/*
替换占位符
*/
System.out.println("输入想要入库的数量:");
int number = scan.nextInt();
number = number + goods.getNumber();
ps.setInt(1, number);
ps.setString(2, id);
res = ps.executeUpdate();
System.out.println("入库成功");
System.out.println("商品 " + goods.getName() + "还有" + number + "个");
System.out.println("修改行数: " + res + "行");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(conn);
DataSourPool.close(ps);
}
}
@Override
public void outGoods(String id) {
int res = 0;
String sql = "update goods set number =? where id=?";
try {
conn = DataSourPool.getConnection();//获取连接
ps = conn.prepareStatement(sql);
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println("商品 " + goods.getName() + "还有" + goods.getNumber() + "个");
/*
替换占位符
*/
System.out.println("输入想要出库的数量:");
int numberOut = scan.nextInt();
int numberNow = goods.getNumber();//现有的数量
if (numberNow > numberOut) {
ps.setInt(1, numberNow - numberOut);
} else {
try {
throw new NotNumberException("库存不够");
} catch (NotNumberException e) {
e.printStackTrace();
}
}
ps.setString(2, id);
res = ps.executeUpdate();
System.out.println("出库成功");
System.out.println("商品 " + goods.getName() + "还有" + (numberNow - numberOut) + "个");
System.out.println("修改行数: " + res + "行");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(conn);
DataSourPool.close(ps);
}
}
@Override
public void setGoodsPrice(String id) {
int res = 0;
String sql = "update goods set price =? where id=?";
try {
conn = DataSourPool.getConnection();//获取数据库的连接
ps = conn.prepareStatement(sql);
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println("商品 " + goods.getName() + "价格" + goods.getPrice() + "元");
System.out.println("想要修改的价格:");
int priceNew = scan.nextInt();
ps.setInt(1, priceNew);
ps.setString(2, id);
res = ps.executeUpdate();
System.out.println("修改成功");
System.out.println("商品 " + goods.getName() + "价格" + priceNew + "元");
System.out.println("修改行数: " + res + "行");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(ps);
DataSourPool.close(conn);
}
}
@Override
public void deleteGoods(String id) {
int res = 0;
String sql = "delete from goods where id=?";
try {
conn = DataSourPool.getConnection();//获取数据库的连接
ps = conn.prepareStatement(sql);
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println(goods);
System.out.println("是否删除 输入1确认 其他键跳过 回车确认");
if (scan.nextInt() == 1) {
ps.setString(1, id);
res = ps.executeUpdate();
System.out.println("删除成功");
System.out.println("修改行数: " + res + "行");
}else {
System.out.println("删除失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(ps);
DataSourPool.close(conn);
}
}
}
每一个功能在下边详细
用到了上边的Util类的方法
public void addGoods(Goods goods) {
int res = 0;//记录修改数据库的行数
/*
判断商品是否已存在
*/
List<Goods> list = Util.AllGoods();//获取所有商品
for (Goods goods1 : list) {
if(goods1.getId().equals(goods.getId())){
try {
throw new ExistExcetion("已经存在商品");//自定义的异常类
} catch (ExistExcetion existExcetion) {
existExcetion.printStackTrace();
}
}
}
String sql = " insert into goods(id,name,price,number) values(?,?,?,?)";
Connection conn = DataSourPool.getConnection();//获取数据库的连接
PreparedStatement ps = null;// 创建PreparedStatement 接口对象
try {
ps = conn.prepareStatement(sql); //获取PreparedStatement 接口对象
/*
替换sql中的占位符
*/
ps.setString(1, goods.getId());
ps.setString(2, goods.getName());
ps.setInt(3, goods.getPrice());
ps.setInt(4, goods.getNumber());
res = ps.executeUpdate();//执行sql语句
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {//关闭资源
DataSourPool.close(conn);
DataSourPool.close(ps);
}
System.out.println("添加成功");
System.out.println("添加行数: " + res + "行");
}
@Override
public void showGoods() {
List<Goods> list = Util.AllGoods();
System.out.println("商品编号\t商品名称\t商品价格\t\t商品数量");
for (Goods goods : list) {
System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t"
+ goods.getPrice() + "\t\t\t" + goods.getNumber());
}
}
public void inGoods(String id) {
int res = 0;
String sql = "update goods set number =? where id=?";
try {
Connection conn = DataSourPool.getConnection();//获取数据库连接
PreparedStatement ps = conn.prepareStatement(sql); //获取PreparedStatement 接口对象
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println("商品 " + goods.getName() + "还有" + goods.getNumber() + "个");
/*
替换占位符
*/
System.out.println("输入想要入库的数量:");
int number = scan.nextInt();
number = number + goods.getNumber();
ps.setInt(1, number);
ps.setString(2, id);
res = ps.executeUpdate();
System.out.println("入库成功");
System.out.println("商品 " + goods.getName() + "还有" + number + "个");
System.out.println("修改行数: " + res + "行");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(conn);
DataSourPool.close(ps);
}
}
public void outGoods(String id) {
int res = 0;
String sql = "update goods set number =? where id=?";
try {
Connection conn = DataSourPool.getConnection();//获取数据库连接
PreparedStatement ps = conn.prepareStatement(sql); //获取PreparedStatement 接口对象
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println("商品 " + goods.getName() + "还有" + goods.getNumber() + "个");
/*
替换占位符
*/
System.out.println("输入想要出库的数量:");
int numberOut = scan.nextInt();
int numberNow = goods.getNumber();//现有的数量
if (numberNow > numberOut) {
ps.setInt(1, numberNow - numberOut);
} else {
try {
throw new NotNumberException("库存不够");
} catch (NotNumberException e) {
e.printStackTrace();
}
}
ps.setString(2, id);
res = ps.executeUpdate();
System.out.println("出库成功");
System.out.println("商品 " + goods.getName() + "还有" + (numberNow - numberOut) + "个");
System.out.println("修改行数: " + res + "行");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(conn);
DataSourPool.close(ps);
}
}
public void setGoodsPrice(String id) {
int res = 0;
String sql = "update goods set price =? where id=?";
try {
Connection conn = DataSourPool.getConnection();//获取数据库的连接
PreparedStatement ps = conn.prepareStatement(sql);//获取PreparedStatement 接口对象
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println("商品 " + goods.getName() + "价格" + goods.getPrice() + "元");
System.out.println("想要修改的价格:");
int priceNew = scan.nextInt();
ps.setInt(1, priceNew);
ps.setString(2, id);
res = ps.executeUpdate();
System.out.println("修改成功");
System.out.println("商品 " + goods.getName() + "价格" + priceNew + "元");
System.out.println("修改行数: " + res + "行");
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(ps);
DataSourPool.close(conn);
}
}
public void deleteGoods(String id) {
int res = 0;
String sql = "delete from goods where id=?";
try {
Connection conn = DataSourPool.getConnection();//获取数据库的连接
PreparedStatement ps = conn.prepareStatement(sql);//获取PreparedStatement 接口对象
Goods goods = Util.findIDOutGoods(id);//id对应的商品
System.out.println(goods);
System.out.println("是否删除 输入1确认 其他键跳过 回车确认");
if (scan.nextInt() == 1) {
ps.setString(1, id);
res = ps.executeUpdate();
System.out.println("删除成功");
System.out.println("修改行数: " + res + "行");
}else {
System.out.println("删除失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
DataSourPool.close(ps);
DataSourPool.close(conn);
}
}
import com.sjtest.dao.imp.GoodsDaoImp;
import com.sjtest.pojo.Goods;
import com.sjtest.util.Util;
import java.util.Scanner;
/**
* 项目要求:
* 1.能根据项目需求设计合理的实体类
* 2.规范的编码
* 3.合理选择集合类型
* 4.关闭程序后,录入的数据下次打开程序还存在
* 商品添加(输入商品编码、名称、价格、数量)
* 商品列表展示
* 商品入库(输入商品编号,然后提示用户输入入库数量,进行入库)
* 商品出库(输入商品编号,提示用户输入出库数量,进行出库)
* 修改商品价格(提示用户输入编号,录入新价格)。
* 删除商品
*/
public class Demo {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
GoodsDaoImp goodsManager = new GoodsDaoImp();
while (true) {
System.out.println("-------------------");
System.out.println("欢迎来到商品管理后台");
System.out.println("选择你的操作:");
System.out.println("1.商品添加");
System.out.println("2.商品列表展示");
System.out.println("3.商品入库");
System.out.println("4.商品出库");
System.out.println("5.修改商品价格");
System.out.println("6.删除商品");
System.out.println("7.退出");
System.out.println("-------------------");
switch (scan.nextInt()) {
case 1:
System.out.println("依次输入 商品编码、名称、价格、数量");
Goods goods = new Goods(scan.next(), scan.next(), scan.nextInt(), scan.nextInt());
goodsManager.addGoods(goods);
break;
case 2:
goodsManager.showGoods();
break;
case 3:
//获取商品编号
Util.showIDAndName();
System.out.println("请输入商品编号:");
goodsManager.inGoods(scan.next());
break;
case 4:
//获取商品编号
Util.showIDAndName();
System.out.println("请输入商品编号:");
goodsManager.outGoods(scan.next());
break;
case 5:
//获取商品编号
Util.showIDAndName();
System.out.println("请输入商品编号:");
goodsManager.setGoodsPrice(scan.next());
break;
case 6:
//获取商品编号
Util.showIDAndName();
System.out.println("请输入商品编号:");
goodsManager.deleteGoods(scan.next());
case 7:
System.exit(0);
break;
default:
System.out.println("输入错误");
break;
}
}
}
}
项目到这里结束了,有更好的建议欢迎评论,指正。
源码点击这里