• 03_java基础(六)之CRUD实现


    1.简单实现

      1 package com.day01.station.dao;
      2 
      3 /**
      4  * Created by Administrator on 2018/2/1.
      5  */
      6 
      7 import java.sql.Connection;
      8 import java.sql.DriverManager;
      9 import java.sql.ResultSet;
     10 import java.sql.Statement;
     11 
     12 /**
     13  * jdbc 连接数据库   加 连 语  执 释
     14  * 1.加载
     15  * 2.连接
     16  * 3.创建编译语句
     17  * 4.执行语句
     18  * 5.释放资源
     19  */
     20 public class ProductDao {
     21     //增加 一个产品 包括 名称 和  卖价
     22     public void save(String productName, int salePrice) {
     23         System.out.println("--------我是增加方法---------");
     24         System.out.println(" productName = " + productName + " , salePrice =" + salePrice);
     25 
     26         try {
     27             //1. 加载
     28             Class.forName("com.mysql.jdbc.Driver");
     29             // * 2. 连接
     30               // int  age =18
     31             //static Connection getConnection(String url, String user, String password)
     32             //试图建立到给定数据库 URL 的连接。
     33             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
     34             // * 3. 创建编译语句
     35            // Statement createStatement()
     36            // 创建一个 Statement 对象来将 SQL 语句发送到数据库。
     37             Statement statement = connection.createStatement();
     38             // * 4. 执行语句
     39            // int executeUpdate(String sql)
     40            // 执行给定 SQL 语句,该语句可能为 INSERT、UPDATE 或 DELETE 语句,或者不返回任何内容的 SQL 语句(如 SQL DDL 语句)。
     41             String sql="INSERT INTO product (product_name,sale_price) VALUES ('小米手机',1010)";
     42             statement.executeUpdate(sql);
     43             // * 5. 释放资源
     44             statement.close();
     45             connection.close();
     46 
     47         } catch (Exception e) {
     48             e.printStackTrace();
     49         }
     50 
     51 
     52     }
     53 
     54     //删除   根据id删除产品
     55     public void delete(int id) {
     56         System.out.println(" --------我是删除方法--------- ");
     57         System.out.println("---id=" + id);
     58 
     59         try {
     60             //1.加载
     61             Class.forName("com.mysql.jdbc.Driver");
     62             //2.连接
     63             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
     64             //3.创建编译语句
     65             Statement statement = connection.createStatement();
     66             //4.执行语句
     67             String sql="DELETE FROM product WHERE id=31";
     68             statement.executeUpdate(sql);
     69             //5.释放资源
     70             statement.close();
     71             connection.close();
     72 
     73         } catch (Exception e) {
     74             e.printStackTrace();
     75         }
     76 
     77     }
     78 
     79     //修改  根据id 修改产品的名称
     80     public void update(int id, String productName) {
     81         System.out.println("--------我是修改方法---------");
     82         System.out.println(" productName = " + productName + " , id =" + id);
     83         try {
     84             //1.加载
     85             Class.forName("com.mysql.jdbc.Driver");
     86             //2.连接
     87             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
     88             //3.创建编译语句
     89             Statement statement = connection.createStatement();
     90             //4.执行语句
     91             String sql="UPDATE product SET sale_price=500,cost_price=200 WHERE id= 26 ";
     92             statement.executeUpdate(sql);
     93             //5.释放资源
     94             statement.close();
     95             connection.close();
     96 
     97         } catch (Exception e) {
     98             e.printStackTrace();
     99         }
    100 
    101 
    102     }
    103 
    104     //查询
    105     public String query() {
    106         System.out.println("------我是查询方法----------");
    107         try {
    108             //1.加载
    109             Class.forName("com.mysql.jdbc.Driver");
    110             //2.连接
    111             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
    112             //3.创建编译语句
    113             Statement statement = connection.createStatement();
    114             //4.执行语句
    115            // ResultSet executeQuery(String sql)
    116           //  执行给定的 SQL 语句,该语句返回单个 ResultSet 对象。
    117             String sql = "SELECT id,product_name,sale_price FROM product WHERE id=9";
    118             ResultSet resultSet = statement.executeQuery(sql);
    119               //解析结果
    120              while (resultSet.next()){//如果有在执行里面
    121                //  int getInt(int columnIndex)
    122                 // 以 Java 编程语言中 int 的形式获取此 ResultSet 对象的当前行中指定列的值。
    123                  int id = resultSet.getInt("id");
    124                  System.out.println("  id = "+id);
    125                 // String getString(String columnLabel)
    126                  //以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。
    127                  String productName = resultSet.getString("product_name");
    128                  System.out.println(" productName ="+productName);
    129                  //获取卖价
    130                  int salePrice = resultSet.getInt("sale_price");
    131                  System.out.println(" salePrice = "+salePrice);
    132              }
    133             //5.释放资源
    134             resultSet.close();
    135             statement.close();
    136             connection.close();
    137 
    138         } catch (Exception e) {
    139             e.printStackTrace();
    140         }
    141         return "苹果手机";
    142     }
    143 
    144 }
    View Code

    2.动态传递参数之拼接字符串

     

     3.sql注入问题

          

          dao语句

         

          最终拼接完成后相当于执行的sql语句是:  SELECT * FROM product WHERE id=24 OR 1=1 ,必然是条件无效

    4.使用预编译语句防止sql注入

      1 package com.day01.station.dao;
      2 
      3 /**
      4  * Created by Administrator on 2018/2/1.
      5  */
      6 
      7 import java.sql.*;
      8 
      9 /**
     10  * jdbc 连接数据库   加 连 语  执 释
     11  * 1.加载
     12  * 2.连接
     13  * 3.创建编译语句
     14  * 4.执行语句
     15  * 5.释放资源
     16  */
     17 public class Product2Dao {
     18     //增加 一个产品 包括 名称 和  卖价
     19     public void save(String productName, int salePrice) {
     20         System.out.println("--------我是增加方法---------");
     21         System.out.println(" productName = " + productName + " , salePrice =" + salePrice);
     22         try {
     23             //1. 加载
     24             Class.forName("com.mysql.jdbc.Driver");
     25             // * 2. 连接
     26             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
     27            // PreparedStatement prepareStatement(String sql)
     28            // 创建一个 PreparedStatement 对象来将参数化的 SQL 语句发送到数据库。
     29             String sql="INSERT INTO product (product_name,sale_price) VALUES (?,?)";
     30             //3.创建 预编译语句
     31             PreparedStatement preparedStatement = connection.prepareStatement(sql);
     32            // void setString(int parameterIndex, String x)将指定参数设置为给定 Java String 值。
     33             preparedStatement.setString(1,productName);
     34             preparedStatement.setInt(2,salePrice);
     35             //添加参数
     36             //preparedStatement.set
     37             //4.执行
     38             preparedStatement.executeUpdate();//重要提醒 不需要传入sql语句
     39             //5.释放资源
     40             preparedStatement.close();
     41             connection.close();
     42 
     43 
     44         } catch (Exception e) {
     45             e.printStackTrace();
     46         }
     47     }
     48 
     49     //删除   根据id删除产品
     50     public void delete(int id) {
     51         System.out.println(" --------我是删除方法--------- ");
     52         System.out.println("---id=" + id);
     53 
     54         try {
     55             //1.加载
     56             Class.forName("com.mysql.jdbc.Driver");
     57             //2.连接
     58             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
     59             //3.创建 预 编译语句
     60             String sql="DELETE FROM product WHERE id=?";
     61             PreparedStatement preparedStatement = connection.prepareStatement(sql);
     62                 //设定参数
     63                  preparedStatement.setInt(1,id);
     64             //4.执行语句
     65             preparedStatement.executeUpdate();
     66             //5.释放资源
     67             preparedStatement.close();
     68             connection.close();
     69 
     70         } catch (Exception e) {
     71             e.printStackTrace();
     72         }
     73 
     74     }
     75 
     76     //修改  根据id 修改产品的名称
     77     public void update(int id,int salePrice,int costPrice) {
     78         System.out.println("--------我是修改方法---------");
     79         try {
     80             //1.加载
     81             Class.forName("com.mysql.jdbc.Driver");
     82             //2.连接
     83             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
     84             //3.创建编译语句
     85             String sql="UPDATE product SET sale_price=?,cost_price=? WHERE id= ? ";
     86             PreparedStatement preparedStatement = connection.prepareStatement(sql);
     87            preparedStatement.setInt(1,salePrice);
     88             preparedStatement.setInt(2,costPrice);
     89             preparedStatement.setInt(3,id);
     90             //4.执行语句
     91             preparedStatement.executeUpdate();
     92             //5.释放资源
     93            preparedStatement.close();
     94             connection.close();
     95 
     96         } catch (Exception e) {
     97             e.printStackTrace();
     98         }
     99 
    100 
    101     }
    102 
    103     //查询
    104     public String query(int id) {
    105         System.out.println("------我是查询方法----------");
    106         try {
    107             //1.加载
    108             Class.forName("com.mysql.jdbc.Driver");
    109             //2.连接
    110             Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/station_demo", "root", "admin");
    111             //3.创建编译语句
    112             String sql = "SELECT id,product_name,sale_price FROM product WHERE id=?";
    113             PreparedStatement preparedStatement = connection.prepareStatement(sql);
    114             preparedStatement.setInt(1,id);
    115             //4.执行语句
    116             ResultSet resultSet = preparedStatement.executeQuery();
    117             //解析结果
    118              while (resultSet.next()){//如果有在执行里面
    119                //  int getInt(int columnIndex)
    120                 // 以 Java 编程语言中 int 的形式获取此 ResultSet 对象的当前行中指定列的值。
    121                  int id1 = resultSet.getInt("id");
    122                  System.out.println("  id = "+id1);
    123                 // String getString(String columnLabel)
    124                  //以 Java 编程语言中 String 的形式获取此 ResultSet 对象的当前行中指定列的值。
    125                  String productName = resultSet.getString("product_name");
    126                  System.out.println(" productName ="+productName);
    127                  //获取卖价
    128                  int salePrice = resultSet.getInt("sale_price");
    129                  System.out.println(" salePrice = "+salePrice);
    130              }
    131             //5.释放资源
    132             resultSet.close();
    133             preparedStatement.close();
    134             connection.close();
    135 
    136         } catch (Exception e) {
    137             e.printStackTrace();
    138         }
    139         return "苹果手机";
    140     }
    141 
    142 }
    View Code

     到此jdbc简单实现CRUD完成!

    5.测试与评估

       需求:以购买车票为案例完成

       1.设计车票数据库表

      2.完成对车票的列表查询(打印所有车票到控制台)

      3.可以实现新增车票

      4.实现根据车票班次调整车票价格

      5.实现根据班次删除该班次列车

      6.实现根据班次购买一张车票(选做)

      

  • 相关阅读:
    截取UIImagePickerController的拍照事件
    Xcode报错:run custom shell script '[cp] copy pods resource
    XCode报错:Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang failed with exit code 1
    Mac环境下实现alias的重命名命令(永久生效)
    Swift 3.0在集合类数据结构上的一些新变化
    iOS几种简单有效的数组排序方法
    二分法查找、快速排序思想与实现
    iOS10 相册权限
    ios应用版本号设置规则
    iOS白名单设置
  • 原文地址:https://www.cnblogs.com/newAndHui/p/8413373.html
Copyright © 2020-2023  润新知