旅游路线的增删改查带分页java
时间: 2023-10-18 20:24:04 浏览: 80
以下是一个基本的旅游路线的增删改查带分页的 Java 示例:
```java
public class TourRoute {
private int id;
private String name;
private String description;
private double price;
private int days;
// getters and setters
}
public class TourRouteDao {
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/tourdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public List<TourRoute> getTourRoutes(int page, int pageSize) throws SQLException {
List<TourRoute> routes = new ArrayList<>();
int offset = (page - 1) * pageSize;
try (Connection conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM tour_routes LIMIT ?, ?")) {
stmt.setInt(1, offset);
stmt.setInt(2, pageSize);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
TourRoute route = new TourRoute();
route.setId(rs.getInt("id"));
route.setName(rs.getString("name"));
route.setDescription(rs.getString("description"));
route.setPrice(rs.getDouble("price"));
route.setDays(rs.getInt("days"));
routes.add(route);
}
}
}
return routes;
}
public void addTourRoute(TourRoute route) throws SQLException {
try (Connection conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO tour_routes (name, description, price, days) VALUES (?, ?, ?, ?)")) {
stmt.setString(1, route.getName());
stmt.setString(2, route.getDescription());
stmt.setDouble(3, route.getPrice());
stmt.setInt(4, route.getDays());
stmt.executeUpdate();
}
}
public void updateTourRoute(TourRoute route) throws SQLException {
try (Connection conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("UPDATE tour_routes SET name=?, description=?, price=?, days=? WHERE id=?")) {
stmt.setString(1, route.getName());
stmt.setString(2, route.getDescription());
stmt.setDouble(3, route.getPrice());
stmt.setInt(4, route.getDays());
stmt.setInt(5, route.getId());
stmt.executeUpdate();
}
}
public void deleteTourRoute(int id) throws SQLException {
try (Connection conn = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
PreparedStatement stmt = conn.prepareStatement("DELETE FROM tour_routes WHERE id=?")) {
stmt.setInt(1, id);
stmt.executeUpdate();
}
}
}
```
以上是一个简单的 Java 类,用于实现旅游路线的增删改查带分页。我们使用了 MySQL 数据库,以及 JDBC 驱动程序来连接数据库。其中,`TourRouteDao` 类包含了一些基本的方法,如 `getTourRoutes()` 方法用于查询旅游路线列表,`addTourRoute()` 方法用于添加新路线,`updateTourRoute()` 方法用于更新指定路线,`deleteTourRoute()` 方法用于删除指定路线。`getTourRoutes()` 方法使用了分页技术,将查询结果分为每页指定数量的项,并返回指定页数的结果列表。
需要注意的是,以上代码仅为示例,实际应用中需要根据业务需求进行适当的修改和完善。
阅读全文