如何用idea数据库编写快递e站
作者:今晚不想睡沙发 时间:2024-01-23 08:43:56
目录
一、IDEA如何连接数据库
第一种方法:直接在方法体中增加连接信息
方法二:
二、方法代码的实现
1.快递员增加快递
2.快递员删除快递
用数据库编写快递e站(本文只写了idea方面的代码)
随着快递业的不断发展,快递e站也越来越多,我们来编写一个简单的快递e站小程序,本文就介绍了如何用数据库知识编写快递e站。
##成品如下:
一、IDEA如何连接数据库
第一种方法:直接在方法体中增加连接信息
优点:如果仅使用一次数据库操作可选择
缺点:多次数据库操作每次都需要写,麻烦
public void select() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//1.注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2.定义sql
String sql = "select * from kuaidi";
//3.获取conn
conn = DriverManager.getConnection("jdbc:mysql:///kuaidi", "root", "123");
//4.获取执行sql对象Statement
stmt = conn.createStatement();
//5.执行sql
rs = stmt.executeQuery(sql);
//6处理结果
while (rs.next()) {
int danhao = rs.getInt(1);
int qujianma = rs.getInt(2);
String gongsi = rs.getString("gongsi");
String guizi = rs.getString(4);
System.out.println("单号为:" + danhao + " " + "取件码为:" + qujianma + " " + "公司是:" + gongsi + " " + "柜子在第" + guizi + "个");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
方法二:
建立一个JDBCHelper和一个存储数据库账号密码的Properties,来帮助快速加载驱动以及释放内存
优点:只需要写一次,用的时候调用即可
缺点:一次要写很多
释放内存的时候可能传入两个或者三个参数需要释放,所以用重载形式来解决
private static String url;
private static String user;
private static String password;
private static String driver;
/**
* 文件的读取,只需要读取一次即可
*/
static {
//读取资源文件,并获取值
try {
//1.创建properties集合类
Properties pro = new Properties();
//2.加载文件
//获取src路径下的文件的方式--->ClassLoader类加载器
ClassLoader classLoader = JDBCHelper.class.getClassLoader();
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
//pro.load(new FileReader("src/jdbc.properties"));
pro.load(new FileReader(path));
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
driver = pro.getProperty("driver");
Class.forName(driver);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 获取连接
*
* @return连接对象
*/
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, user, password);
}
/**
* 释放资源
*
* @param stmt
* @param conn
*/
public static void close(Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//释放内存
public static void close(ResultSet rs, Statement stmt, Connection conn) {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
二、方法代码的实现
1.快递员增加快递
代码如下:
public class Add {
Connection conn = null;
Statement stmt = null;
Random r= new Random();
public void addq(int danhao,String gongsi){
try {
conn = JDBCHelper.getConnection();
int qujianma = r.nextInt((999999)+1);
String guizi = Integer.toString(r.nextInt(100)+1);
String sql = "insert into kuaidi values(" + danhao + "," + qujianma+",'"+gongsi+"','"+guizi+"')";
stmt = conn.createStatement();
int a = stmt.executeUpdate(sql);
System.out.println("取件码为:"+qujianma);
if(a>0){
System.out.println("添加成功");
}
else {
System.out.println("添加失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
2.快递员删除快递
代码如下:
public class Delete {
Connection conn = null;
Statement stmt = null;
public void delete(int danhao) {
try {
conn = JDBCHelper.getConnection();
String sql = "delete from kuaidi where danhao = "+danhao;
stmt = conn.createStatement();
int a = stmt.executeUpdate(sql);
if(a>0){
System.out.println("删除成功");
}
else {
System.out.println("删除失败");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
finally {
JDBCHelper.close(stmt,conn);
}
}
主要内容代码:
public class Imp {
public static void main(String[] args) {
kuaidi();
}
public static void kuaidi() {
System.out.println("====欢迎使用新职课快递柜====");
Scanner sc = new Scanner(System.in);
Random r = new Random();
while (true) {
System.out.println("请输入你的身份:1-快递员,2-用户");
try {
int a = sc.nextInt();
if (a < 1 || a > 2) {
System.out.println("输入有误,请重新输入");
} else if (a == 1) {
System.out.println("====欢迎使用新职课快递柜====");
System.out.println("请选择操作:1-存快递 2-删除快递 3-修改快递信息 4-查看所有快递");
int b = sc.nextInt();
switch (b) {
case 1: {
System.out.println("====欢迎使用新职课快递柜====");
System.out.println("请输入快递单号:");
int danhao = sc.nextInt();
System.out.println("请输入公司名称:");
String gongsi = sc.next();
Add add = new Add();
add.addq(danhao, gongsi);
break;
}
case 2: {
System.out.println("====欢迎使用新职课快递柜====");
System.out.print("请输入要删除的订单号:");
int dingdan = sc.nextInt();
Delete delete = new Delete();
delete.delete(dingdan);
break;
}
case 3: {
System.out.println("====欢迎使用新职课快递柜====");
System.out.print("请输入要修改的订单号:");
int danhao = sc.nextInt();
Alter al = new Alter();
boolean q = al.select(danhao);
if (q = true) {
System.out.println("请输入更改后的单号");
int afdanhao = sc.nextInt();
al.alter(afdanhao, danhao);
} else {
System.out.println("请核实订单号");
}
break;
}
case 4: {
System.out.println("====欢迎使用新职课快递柜====");
SelectAll s = new SelectAll();
s.select();
}
}
} else if (a == 2) {
System.out.println("====欢迎使用新职课快递柜====");
System.out.println("请输入取件码");
int qujianma = sc.nextInt();
Take t = new Take();
t.take(qujianma);
}
} catch (InputMismatchException e) {
System.out.println();
System.out.println("请输入数字序号!!!!!!!!");
System.out.println();
kuaidi();
}
}
}
}
别的几个代码块都大同小异,只需要修改sql语句即可
如有需要全部代码,或者有疑问的同学私信我就可以了
来源:https://blog.csdn.net/weixin_48305172/article/details/112304491
标签:idea,数据库,快递e站


猜你喜欢
Python命令行参数解析模块optparse使用实例
2023-11-04 08:09:08
python3.9之你应该知道的新特性详解
2021-09-01 12:13:42
python实现用户名密码校验
2022-12-14 07:48:33

MySQL性能优化的一些技巧帮助你的数据库
2024-01-20 12:44:22
Django安装配置mysql的方法步骤
2024-01-21 19:18:08

Python bisect模块原理及常见实例
2023-01-12 07:22:15
MySQL数据库性能优化的八大“妙手”
2009-07-30 08:58:00
Python爬虫实例爬取网站搞笑段子
2022-11-29 17:14:33
用python实现的可以拷贝或剪切一个文件列表中的所有文件
2022-09-11 11:28:23
Python实现像awk一样分割字符串
2022-07-29 18:42:07

golang 输出重定向:fmt Log,子进程Log,第三方库logrus的详解
2024-04-27 15:40:14
Python 26进制计算实现方法
2022-12-09 03:26:36
python实现文件快照加密保护的方法
2022-08-21 20:01:29
在网页设计中,如何使用图标来支持内容?[译]
2009-03-16 16:35:00

utf8_unicode_ci与utf8_general_ci的区别
2010-03-03 15:54:00
golang中package is not in GOROOT报错的真正解决办法
2024-04-28 10:45:29

Python条件语句与循环语句
2023-01-03 05:07:21

python 爬虫爬取京东ps4售卖情况
2022-05-19 01:35:28

python基于celery实现异步任务周期任务定时任务
2021-06-14 05:20:26

在VScode中创建你的代码模板的方法
2022-08-28 10:52:09
