MySQL~什么是Java的JDBC编程、如何利用JDBC实现数据库的增删改查

必备条件

Java的数据库编程JDBC

概念

  • JDBC 是一种用于执行sql语句的Java API, 他是Java中的数据库连接规范,这个API由 java.sql.,javax.sql. 包中的一些类和接口组成,它为Java开发人员操作数据库提供了一个标准的API,可以为多种关系数据库提供统一访问。
  • 本质就是通过代码自己实现一个MySQL客户端,通过网络和服务器进行数据的交互, 客户端不能凭空实现,所以数据库提供了一组API来方便我们实现.
  • 数据库的种类很多,不同数据库提供的API都不太一样,而且有些地方差异很大, 所以在java中为解决这个问题提供了JDBC, 可以理解为是java自带的一种数据库操作API 这种API覆盖所有数据库操作的操作方式,
  • 实质就是java自身完成了JDBC API 和 数据库 API之间的转换
    在这里插入图片描述

JDBC优势

  1. Java语言访问数据库操作完全面向抽象接口编程
  2. 开发数据库应用不用限定在特定数据库厂商的API
  3. 程序的可移植性大大增强

JDBC使用步骤

  1. 创建DataSource对象(准备工作)
  2. 通过Connection连接数据库(输入密码连接成功)
    在这里插入图片描述
  3. 拼接sql语句(写入sql语句)
  4. 执行sql语句(按下回车执行sql语句)
  • 执行 update delete insert 使用 executeUpdate() 方法
  • 执行 select 使用 executeQuery() 方法
  • 使用 executeQuery() 方法 会返回一个resultSet集合, 包含查找到的数据, 初始情况下resultSet不指向任一行记录, 使用next,让他指向第一条记录, 再使用next指向下一条记录
  1. 释放资源

利用JDBC实现增加(insert)

public static void testJDBCInsert(int id, String name, int calssId) throws SQLException {
        //1创建DataSource对象
        DataSource dataSource = new MysqlDataSource();
        //2连接数据库
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_5_31?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("listen");
        Connection connection = dataSource.getConnection();
        // 3 拼接sql语句
        String sql = "insert into student values(?, ?, ?)";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        statement.setString(2, name);
        statement.setInt(3, calssId);
        // 4 执行sql语句
        int ret = statement.executeUpdate();
        if (ret == 1) {
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }
        // 5 关闭连接
        statement.close();
        connection.close();
    }

利用JDBC实现删除(delete)

public static void testJDBCDelete(int id) throws SQLException {
        //1创建DataSource对象
        DataSource dataSource = new MysqlDataSource();
        //2连接数据库
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_5_31?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("listen");
        Connection connection = dataSource.getConnection();
        //3拼接sql
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1, id);
        //4执行sql
        int ret = statement.executeUpdate();
        if (ret == 1) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }
        //5关闭资源
        statement.close();
        connection.close();
    }

利用JDBC实现修改(update)

public static void testJDBCUpdate(int id, String name) throws SQLException {
        //1创建DataSource对象
        DataSource dataSource = new MysqlDataSource();
        //2连接数据库
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_5_31?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("listen");
        Connection connection = dataSource.getConnection();
        //3拼接sql
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, name);
        statement.setInt(2, id);
        //4执行sql
        int ret = statement.executeUpdate();
        if (ret == 1) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        //5关闭资源
        statement.close();
        connection.close();
    }

利用JDBC实现查找(select)

public static void testJDBCSelect() throws SQLException {
        //1创建DataSource对象
        DataSource dataSource = new MysqlDataSource();
        //2连接数据库
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java_5_31?characterEncoding=utf-8&useSSL=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("listen");
        Connection connection = dataSource.getConnection();
        //3拼接sql
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);
        //4执行sql
        ResultSet resultSet = statement.executeQuery();
        //5遍历得到的集合
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            int classId = resultSet.getInt("classId");
            System.out.println("id " + id + " name " + name + " classId " + classId);
        }
        //6关闭资源
        resultSet.close();
        statement.close();
        connection.close();
    }

猜你喜欢

转载自blog.csdn.net/Shangxingya/article/details/106460289