快递管理系统代码链接mysql
时间: 2023-10-02 07:08:27 浏览: 43
以下是一个简单的快递管理系统Java连接MySQL的代码示例:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ExpressDaoImpl implements ExpressDao {
private static final String URL = "jdbc:mysql://localhost:3306/express_db";
private static final String USER = "root";
private static final String PASSWORD = "password";
private static final String INSERT_SQL = "INSERT INTO express (id, sender, receiver, address, phone) VALUES (?, ?, ?, ?, ?)";
private static final String SELECT_ALL_SQL = "SELECT * FROM express";
private static final String SELECT_BY_ID_SQL = "SELECT * FROM express WHERE id = ?";
private static final String UPDATE_SQL = "UPDATE express SET sender=?, receiver=?, address=?, phone=? WHERE id=?";
private static final String DELETE_SQL = "DELETE FROM express WHERE id=?";
public Connection getConnection() throws SQLException {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public void closeConnection(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public List<Express> findAll() throws SQLException {
List<Express> list = new ArrayList<>();
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(SELECT_ALL_SQL);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Express express = new Express();
express.setId(rs.getInt("id"));
express.setSender(rs.getString("sender"));
express.setReceiver(rs.getString("receiver"));
express.setAddress(rs.getString("address"));
express.setPhone(rs.getString("phone"));
list.add(express);
}
rs.close();
ps.close();
closeConnection(conn);
return list;
}
public Express findById(int id) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(SELECT_BY_ID_SQL);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
Express express = null;
if (rs.next()) {
express = new Express();
express.setId(rs.getInt("id"));
express.setSender(rs.getString("sender"));
express.setReceiver(rs.getString("receiver"));
express.setAddress(rs.getString("address"));
express.setPhone(rs.getString("phone"));
}
rs.close();
ps.close();
closeConnection(conn);
return express;
}
public void save(Express express) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(INSERT_SQL);
ps.setInt(1, express.getId());
ps.setString(2, express.getSender());
ps.setString(3, express.getReceiver());
ps.setString(4, express.getAddress());
ps.setString(5, express.getPhone());
ps.executeUpdate();
ps.close();
closeConnection(conn);
}
public void update(Express express) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(UPDATE_SQL);
ps.setString(1, express.getSender());
ps.setString(2, express.getReceiver());
ps.setString(3, express.getAddress());
ps.setString(4, express.getPhone());
ps.setInt(5, express.getId());
ps.executeUpdate();
ps.close();
closeConnection(conn);
}
public void delete(int id) throws SQLException {
Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(DELETE_SQL);
ps.setInt(1, id);
ps.executeUpdate();
ps.close();
closeConnection(conn);
}
}
```
在这个示例中,我们使用了Java的JDBC API来连接MySQL数据库,并实现了快递管理系统的基本CRUD操作。我们定义了数据库连接的URL、用户名和密码,然后使用`DriverManager.getConnection()`方法创建连接。我们还使用了`Class.forName()`方法来加载MySQL的JDBC驱动程序。
注意,这只是一个简单的示例。在实际应用中,您可能需要更复杂的代码来处理连接和异常。您还需要确保您的MySQL数据库已正确安装和配置,并且您的Java项目中已经包含了MySQL的JDBC驱动程序。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)