java中CRUD(增删查改)底层代码的实现:

 package com.station.dao;

 import com.station.model.Product;

 import java.sql.*;

 public class ProductDao {
     //增加产品:
     public void add(String product_name, double sale_price, double cost_price) {
         System.out.println("product_name=" + product_name + "," + "sale_price=" + sale_price + "," + "cost_price=" + cost_price);
         try {
         //加载:
             Class.forName("com.mysql.jdbc.Driver");
             //连接:
             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
             //创建预编译语句:
             String sql = "INSERT INTO product(product_name,sale_price,cost_price) VALUE  (?,?,?)";
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             preparedStatement.setString(1, product_name);
             preparedStatement.setDouble(2, sale_price);
             preparedStatement.setDouble(3, cost_price);
             //执行语句:
             preparedStatement.executeUpdate();
             //释放资源:
             preparedStatement.close();
             connection.close();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }

     //删除产品:
     public void delete(int id) {
         System.out.println("id=" + id);
         try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
             String sql = "DELETE FROM product WHERE id=?";
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             preparedStatement.setInt(1, id);
             preparedStatement.executeUpdate();
             preparedStatement.close();
             connection.close();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }

     //修改产品:
     public void update(int id, String product_name, double cutoff, double cost_price) {
         System.out.println("id=" + id + "," + "product_name=" + product_name);
         try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
             String sql = "UPDATE product SET product_name=?,cutoff=?,cost_price=? WHERE id=?";
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             preparedStatement.setString(1, product_name);
             preparedStatement.setDouble(2, cutoff);
             preparedStatement.setDouble(3, cost_price);
             preparedStatement.setInt(4, id);
             preparedStatement.executeUpdate();
             preparedStatement.close();
             connection.close();
         } catch (ClassNotFoundException e) {
             e.printStackTrace();
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }

     //查询产品:
     public Product select(int id) {
         Product product = new Product();
         try {
             Class.forName("com.mysql.jdbc.Driver");
             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin");
             String sql = "SELECT id,product_name,sale_price FROM product WHERE id=?";
             PreparedStatement preparedStatement = connection.prepareStatement(sql);
             preparedStatement.setInt(1,id);
             ResultSet resultSet = preparedStatement.executeQuery();
             while (resultSet.next()){
                 int id1 = resultSet.getInt("id");
                 String product_name = resultSet.getString("product_name");
                 double sale_price = resultSet.getDouble("sale_price");
                 product.setId(id1);
                 product.setProductName(product_name);
                 product.setSalePrice(sale_price);
             }
             resultSet.close();
             preparedStatement.close();
             connection.close();
         } catch (Exception e) {
             e.printStackTrace();

         }
         return product;
     }
 }
05-02 03:49