使用java连接数据库还有jdbc的代码编写一个快递管理系统
时间: 2023-07-25 13:12:37 浏览: 66
好的,我可以帮你写一个简单的快递管理系统,使用Java和JDBC连接MySQL数据库。
首先,我们需要创建一个MySQL数据库和表。假设我们创建了一个名为`express`的数据库,并且在其中创建了一个名为`package`的表,用于存储快递包裹的信息。
表结构如下:
```
CREATE TABLE package (
id INT NOT NULL AUTO_INCREMENT,
sender VARCHAR(50) NOT NULL,
recipient VARCHAR(50) NOT NULL,
status VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
```
接下来,我们需要编写Java代码来连接数据库,并实现快递管理系统的功能。以下是一个简单的示例:
```java
import java.sql.*;
public class ExpressManager {
private static final String URL = "jdbc:mysql://localhost:3306/express";
private static final String USER = "root";
private static final String PASSWORD = "password";
public static void main(String[] args) {
Connection conn = null;
try {
// 连接数据库
conn = DriverManager.getConnection(URL, USER, PASSWORD);
System.out.println("连接数据库成功!");
// 插入数据
insertPackage(conn, "张三", "李四", "已发货");
insertPackage(conn, "王五", "赵六", "已签收");
// 查询数据
queryPackage(conn);
// 更新数据
updatePackage(conn, 1, "已送达");
// 再次查询数据
queryPackage(conn);
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 插入数据
public static void insertPackage(Connection conn, String sender, String recipient, String status) throws SQLException {
String sql = "INSERT INTO package(sender, recipient, status) VALUES(?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, sender);
ps.setString(2, recipient);
ps.setString(3, status);
ps.executeUpdate();
System.out.println("已插入数据:sender=" + sender + ", recipient=" + recipient + ", status=" + status);
}
// 查询数据
public static void queryPackage(Connection conn) throws SQLException {
String sql = "SELECT * FROM package";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
System.out.println("查询到以下数据:");
while (rs.next()) {
int id = rs.getInt("id");
String sender = rs.getString("sender");
String recipient = rs.getString("recipient");
String status = rs.getString("status");
System.out.println("id=" + id + ", sender=" + sender + ", recipient=" + recipient + ", status=" + status);
}
}
// 更新数据
public static void updatePackage(Connection conn, int id, String status) throws SQLException {
String sql = "UPDATE package SET status=? WHERE id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, status);
ps.setInt(2, id);
ps.executeUpdate();
System.out.println("已更新数据:id=" + id + ", status=" + status);
}
}
```
以上代码实现了以下功能:
1. 连接数据库
2. 插入数据
3. 查询数据
4. 更新数据
5. 关闭数据库连接
你可以根据实际需求,修改以上代码实现更多功能,例如删除数据、根据条件查询数据等。
相关推荐
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)