JDBC programming implements operations such as insert, delete, modify, search, etc. on the database

Six steps of JDBC programming

Six steps of JDBC programming:
1. Create data source
2. Establish connection with database
3. Input information
4. Splice sql statement
5. Execute sql
6. Recycle and release resources

JDBC programming to realize insert data operation on database

1. Insert data into the database through JDBC programming

public class TestJDBC {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);//控制台输入
       
        //1.创建数据源
        
        DataSource dataSource = new MysqlDataSource();
        //DataSource JDBC 带的接口... MysqlDataSource来自于刚下载好的那个 mysql 的jar包
        //设置数据库所在地址
        ((MysqlDataSource) dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        //设置登录数据库的用户名
        ((MysqlDataSource) dataSource).setUser("root");//mysql默认自带的管理员用户
        //这个是设置登录数据库的密码 安装mysql的时候设置的密码
        ((MysqlDataSource) dataSource).setPassword("707703");//向下转型~

        //2.让代码和数据库服务器建立连接 这里的Connection jdbc里的 sql ~~相当于达到了菜鸟驿站
        
        Connection connection = dataSource.getConnection();

        //3. 让用户通过控制台输入 一下待插入的数据
        System.out.println("请输入学号: ");
        int id = scanner.nextInt();
        System.out.println("请输入姓名: ");
        String name = scanner.next();

        // 4..操作数据库,以插入数据为例
        // 关键所在 就是构造一个 sql 语句
        // 在 JDBC 中构建的 SQL 不必带上 ;
        // ; 只是在命名行中 用来区分不同的语句,现在是直接在代码中操作
        String sql = "insert into student values(?,?)";//? 相当于告诉java程序,这两个字段的值,还不确定
        // 此处 光是一个 String 类型的 sql 还不行,需要把这个 String 包装成一个 "语句对象"
        PreparedStatement statement = connection.prepareStatement(sql);
        //Prepared: 准备/预备 Statement: 语句, 通过 connection里面的prepareStatement 这个方法来构造语句对象的
        //这个操作是 把字符串 风格的 sql 转换成一个 jdbc 里面的对象

        //进行替换操作
        statement.setInt(1,id);
        statement.setString(2,name);
        System.out.println("statement: " + statement);


        // 5. 执行 sql, 相当于 扫码取件
        // SQL 里面如果是 insert ,update , delete,都是用 executeUpdate
        // SQ  里面如果是 select 则视同 executeQuery 方法
        // 返回值 就表示这个操作,影响到了几行,就相当于在控制台里输入sql之后,得到的数字
        int  ret = statement.executeUpdate();

        System.out.println(ret);

        // 56 此时 SQL 已经执行完毕,然后还需要释放资源
        //当创建好相关的连接之后,JVM就会从系统这里申请到一些硬件资源.不用了就得急的释放
        // 先创建的 后释放 比如这里 我们先创建的 connection 后创建的 statement 就相当于 开冰箱开抽屉
        statement.close();
        connection.close();

    }
}

JDBC programming implements delete operation on database pair

2. Implement the delete operation on the database through JDBC programming

public class TestJDBCDelete {
    public static void main(String[] args) throws SQLException {
        // 删除数据库 中的记录
        // 让用户输入一个 id,根据 id 来删除

        //1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("707703");

        //2. 建立连接
        Connection connection = dataSource.getConnection();


        //3. 用户输入 id

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个要删除的id: ");
        int id = scanner.nextInt();


        //4. 拼接 sql 语句
        String sql = "delete from student where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);

        //5. 执行 sql
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);

        //6. 回收释放资源
        statement.close();
        connection.close();


    }
}

JDBC programming implements modification operations on the database

3. JDBC programming implements modification operations on the database

public class TestJDBCUpdate {
    public static void main(String[] args) throws SQLException {
        // 根据 id 修改学生姓名,让用户 输入 要修改的 id,以及对应的修改后的姓名

        //1. 创建 数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("707703");

        //2. 和数据库建立连接
        Connection connection = dataSource.getConnection();

        //3. 输入 信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要修改的学生id: ");
        int id = scanner.nextInt();
        System.out.println("请输入要修改的学生名字: ");
        String name = scanner.next();


        //4. 拼装 sql
        String sql = "update student set name = ? where id = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,name);
        statement.setInt(2,id);
        System.out.println("statement: " + statement);


        //5. 执行 sql
        int ret = statement.executeUpdate();
        System.out.println("ret = " + ret);

        //6.回收资源
        statement.close();
        connection.close();

    }




}

JDBC programming to realize the search operation on the database

public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException {
        //1. 创建数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setURL("jdbc:mysql://127.0.0.1:3306/java102?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("707703");

        //2.建立连接
        Connection connection = dataSource.getConnection();

        //3. 拼装 SQL
        String sql = "select * from student";
        PreparedStatement statement = connection.prepareStatement(sql);

        //4. 执行 sql
        // 查询操作 返回值 不是 int,而是一个 "临时表 "
        // 使用 ResultSet 表示这个表
        ResultSet resultSet = statement.executeQuery();

        //5. 遍历结果集合(返回临时表),先获取每一行,在获取这一行的或干列
        // next 方法表示获取到一行记录,同事把光标 往后移动一行
        // 如果遍历表结束为止,此处的 next 直接返回 false
        //mysql> select * from student;
        //        +------+--------+
        //        | id   | name   |
        //        +------+--------+
        //        |    2 | 李四    |
//                |    3 | 王五    |
        //        +------+--------+
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            System.out.println("id: " + id + ", name=" + name);

        }

        // 6. 释放资源
        resultSet.close();
        statement.close();
        connection.close();
    }
}






Guess you like

Origin blog.csdn.net/Biteht/article/details/123535317