java怎么在花店管理系统里增加增删查改的代码
时间: 2024-03-26 16:36:43 浏览: 54
java实现增删查改
4星 · 用户满意度95%
下面是一个简单的示例代码,可以作为参考:
1. 定义数据库表结构,例如:
CREATE TABLE flower (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
price DECIMAL(10,2) NOT NULL,
stock INT NOT NULL
);
2. 使用Java JDBC API连接数据库,进行数据的增删查改操作,例如:
import java.sql.*;
public class FlowerDAO {
private static final String URL = "jdbc:mysql://localhost:3306/flower_shop";
private static final String USER = "root";
private static final String PASSWORD = "123456";
public void addFlower(Flower flower) {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = conn.prepareStatement("INSERT INTO flower(name, price, stock) VALUES (?, ?, ?)")) {
statement.setString(1, flower.getName());
statement.setBigDecimal(2, flower.getPrice());
statement.setInt(3, flower.getStock());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateFlower(Flower flower) {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = conn.prepareStatement("UPDATE flower SET name=?, price=?, stock=? WHERE id=?")) {
statement.setString(1, flower.getName());
statement.setBigDecimal(2, flower.getPrice());
statement.setInt(3, flower.getStock());
statement.setInt(4, flower.getId());
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteFlower(int id) {
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = conn.prepareStatement("DELETE FROM flower WHERE id=?")) {
statement.setInt(1, id);
statement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Flower getFlowerById(int id) {
Flower flower = null;
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = conn.prepareStatement("SELECT * FROM flower WHERE id=?")) {
statement.setInt(1, id);
ResultSet rs = statement.executeQuery();
if (rs.next()) {
flower = new Flower();
flower.setId(rs.getInt("id"));
flower.setName(rs.getString("name"));
flower.setPrice(rs.getBigDecimal("price"));
flower.setStock(rs.getInt("stock"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return flower;
}
public List<Flower> getAllFlowers() {
List<Flower> flowers = new ArrayList<>();
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
PreparedStatement statement = conn.prepareStatement("SELECT * FROM flower")) {
ResultSet rs = statement.executeQuery();
while (rs.next()) {
Flower flower = new Flower();
flower.setId(rs.getInt("id"));
flower.setName(rs.getString("name"));
flower.setPrice(rs.getBigDecimal("price"));
flower.setStock(rs.getInt("stock"));
flowers.add(flower);
}
} catch (SQLException e) {
e.printStackTrace();
}
return flowers;
}
}
3. 定义Java Bean类,例如:
public class Flower {
private int id;
private String name;
private BigDecimal price;
private int stock;
// getter and setter methods
}
4. 在业务逻辑层中使用DAO类进行数据的操作,例如:
public class FlowerService {
private FlowerDAO flowerDAO = new FlowerDAO();
public void addFlower(Flower flower) {
flowerDAO.addFlower(flower);
}
public void updateFlower(Flower flower) {
flowerDAO.updateFlower(flower);
}
public void deleteFlower(int id) {
flowerDAO.deleteFlower(id);
}
public Flower getFlowerById(int id) {
return flowerDAO.getFlowerById(id);
}
public List<Flower> getAllFlowers() {
return flowerDAO.getAllFlowers();
}
}
5. 在前端界面中进行相应的操作,例如:
public class FlowerShopGUI {
private FlowerService flowerService = new FlowerService();
public void addFlower() {
Flower flower = new Flower();
flower.setName("Rose");
flower.setPrice(new BigDecimal("19.99"));
flower.setStock(100);
flowerService.addFlower(flower);
}
public void updateFlower() {
Flower flower = flowerService.getFlowerById(1);
flower.setPrice(new BigDecimal("29.99"));
flowerService.updateFlower(flower);
}
public void deleteFlower() {
flowerService.deleteFlower(1);
}
public void getFlowerById() {
Flower flower = flowerService.getFlowerById(1);
System.out.println(flower.getName() + ": " + flower.getPrice());
}
public void getAllFlowers() {
List<Flower> flowers = flowerService.getAllFlowers();
for (Flower flower : flowers) {
System.out.println(flower.getName() + ": " + flower.getPrice());
}
}
}
阅读全文