java中CRUD(增刪查改)底層代碼的實現:java
1 package com.station.dao; 2 3 import com.station.model.Product; 4 5 import java.sql.*; 6 7 public class ProductDao { 8 //增長產品: 9 public void add(String product_name, double sale_price, double cost_price) { 10 System.out.println("product_name=" + product_name + "," + "sale_price=" + sale_price + "," + "cost_price=" + cost_price); 11 try { 12 //加載: 13 Class.forName("com.mysql.jdbc.Driver"); 14 //鏈接: 15 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin"); 16 //建立預編譯語句: 17 String sql = "INSERT INTO product(product_name,sale_price,cost_price) VALUE (?,?,?)"; 18 PreparedStatement preparedStatement = connection.prepareStatement(sql); 19 preparedStatement.setString(1, product_name); 20 preparedStatement.setDouble(2, sale_price); 21 preparedStatement.setDouble(3, cost_price); 22 //執行語句: 23 preparedStatement.executeUpdate(); 24 //釋放資源: 25 preparedStatement.close(); 26 connection.close(); 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } catch (SQLException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 //刪除產品: 35 public void delete(int id) { 36 System.out.println("id=" + id); 37 try { 38 Class.forName("com.mysql.jdbc.Driver"); 39 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin"); 40 String sql = "DELETE FROM product WHERE id=?"; 41 PreparedStatement preparedStatement = connection.prepareStatement(sql); 42 preparedStatement.setInt(1, id); 43 preparedStatement.executeUpdate(); 44 preparedStatement.close(); 45 connection.close(); 46 } catch (ClassNotFoundException e) { 47 e.printStackTrace(); 48 } catch (SQLException e) { 49 e.printStackTrace(); 50 } 51 } 52 53 //修改產品: 54 public void update(int id, String product_name, double cutoff, double cost_price) { 55 System.out.println("id=" + id + "," + "product_name=" + product_name); 56 try { 57 Class.forName("com.mysql.jdbc.Driver"); 58 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin"); 59 String sql = "UPDATE product SET product_name=?,cutoff=?,cost_price=? WHERE id=?"; 60 PreparedStatement preparedStatement = connection.prepareStatement(sql); 61 preparedStatement.setString(1, product_name); 62 preparedStatement.setDouble(2, cutoff); 63 preparedStatement.setDouble(3, cost_price); 64 preparedStatement.setInt(4, id); 65 preparedStatement.executeUpdate(); 66 preparedStatement.close(); 67 connection.close(); 68 } catch (ClassNotFoundException e) { 69 e.printStackTrace(); 70 } catch (SQLException e) { 71 e.printStackTrace(); 72 } 73 } 74 75 //查詢產品: 76 public Product select(int id) { 77 Product product = new Product(); 78 try { 79 Class.forName("com.mysql.jdbc.Driver"); 80 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_online", "root", "admin"); 81 String sql = "SELECT id,product_name,sale_price FROM product WHERE id=?"; 82 PreparedStatement preparedStatement = connection.prepareStatement(sql); 83 preparedStatement.setInt(1,id); 84 ResultSet resultSet = preparedStatement.executeQuery(); 85 while (resultSet.next()){ 86 int id1 = resultSet.getInt("id"); 87 String product_name = resultSet.getString("product_name"); 88 double sale_price = resultSet.getDouble("sale_price"); 89 product.setId(id1); 90 product.setProductName(product_name); 91 product.setSalePrice(sale_price); 92 } 93 resultSet.close(); 94 preparedStatement.close(); 95 connection.close(); 96 } catch (Exception e) { 97 e.printStackTrace(); 98 99 } 100 return product; 101 } 102 }