zoukankan      html  css  js  c++  java
  • MYSQL 之 JDBC(三): 增删改查(一)通过Statement执行更新操作

    Statement测试

    /**
     * 通过JDBC向指定的数据表中插入一条记录
     * 1. Statement:用于执行sql语句的对象
     * 1.1 通过Connection的createStatement()方法来获取
     * 1.2 通过executeUpdate(sql)可以执行SQL语句
     * 1.3 传入的sql可以是insert, update或delete,但不能是select
     * 2. Connection、Statement都是应用程序和数据库服务器的连接资源。使用后一定要关闭。
     * 2.1 需要再finally中关闭
     * 3. 关闭顺序:先获取的后关,后获取的先关
     */
    public void testStatement() {
        Connection conn = null;
        Statement statement = null;
        try {
            // 1. 获取数据库连接
            conn = getConnection2();
            // 2. 准备插入的SQL语句
            String sql = "insert into t_user (username, pwd) values('测试', 3352)";
            String sql2 = "update t_user set username='傻瓜' where id = 20017";
            // 3. 执行插入
            // 3.1 获取操作sql语句的Statement对象
            statement = conn.createStatement();
            // 3.2 调用Statement对象的executeUpdate(sql)执行SQL语句
            statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    // 4. 关闭Statement对象
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (statement != null) {
                    // 5. 关闭Connection对象
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    insert/update/delete封装

    /**
     * 通用的更新的方法:insert/update/delete
     * 版本1
     */
    public void update(String sql){
        Connection conn = null;
        Statement statement = null;
    
        try {
            conn = getConnection2();
            statement = conn.createStatement();
            statement.executeUpdate(sql);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (statement != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    创建JDBC的工具类,封装方法

    • 工具类

    package com.litian.jdbc;
    
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Properties;
    
    /**
     * @author: Li Tian
     * @contact: litian_cup@163.com
     * @software: IntelliJ IDEA
     * @file: JDBCUtils.java
     * @time: 2020/3/21 15:23
     * @desc: |操作JDBC的工具类,其中封装了一些工具方法
     * Version1
     */
    
    public class JDBCTools {
    
        /**
         * 关闭Statement和Connection的方法
         */
        public static void release(Statement statement, Connection conn) {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (statement != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        public static void release(ResultSet rs, Statement statement, Connection conn) {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (statement != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        /**
         * 1. 获取连接的方法
         * 通过读取配置文件从数据库服务器获取一个连接。
         *
         * @return
         */
        public static Connection getConnection() throws Exception {
            // 1. 准备连接数据库的4个字符串。
            // 1.1 创建Properties对象
            Properties properties = new Properties();
            // 1.2 获取jdbc.properties对应的输入流
            InputStream in = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties");
            // 1.3 加载1.2对应的输入流
            properties.load(in);
            // 1.4 具体决定user,password等4个字符串。
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String jdbcUrl = properties.getProperty("jdbcUrl");
            String driver = properties.getProperty("driver");
            // 2. 加载数据库驱动程序
            Class.forName(driver);
            // 3. 通过DriverManager的getConnection()方法获取数据库连接。
            return DriverManager.getConnection(jdbcUrl, user, password);
        }
    
    }

    修改后的insert/update/delete封装

    public void update(String sql) {
        Connection conn = null;
        Statement statement = null;
    
        try {
            conn = getConnection2();
            statement = conn.createStatement();
            statement.executeUpdate(sql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTools.release(statement, conn);
        }
    }

    ————————————————
    版权声明:本文为CSDN博主「李英俊小朋友」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_21579045/article/details/105386353

  • 相关阅读:
    20145209预备作业01
    GDB调试汇编堆栈过程分析
    20145209 《信息安全系统设计基础》第14周学习总结
    20145209 《信息安全系统设计基础》第13周学习总结
    20145209 《信息安全系统设计基础》第13周学习总结
    2018-2019-2 20175320实验一《Java开发环境的熟悉》实验报告
    20175320 2018-2019-2 《Java程序设计》第5周学习总结
    20175320 2018-2019-2 《Java程序设计》第4周学习总结
    记2017问鼎杯预赛的wp---来自一个小菜鸡的感想
    利用python多线程实现多个客户端与单个服务端的远程ssh
  • 原文地址:https://www.cnblogs.com/qiu-hua/p/13199535.html
Copyright © 2011-2022 走看看