JDBC

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

本文就介绍了JDBC的几种连接方式。


提示:以下是本篇文章正文内容,下面案例可供参考

一、JDBC是什么?

JDBC的全称是Java数据库连接(Java Database connect),它是一套用于执行SQL语句的Java API。应用程序可通过这套API连接到关系数据库,并使用SQL语句来完成对数据库中数据的查询、更新和删除等操作。
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。

二、使用步骤

1.引入库

代码如下(示例)1:

public  static void test01(){ // 获取数据库连接的方式1

        try {
            //创建驱动对象
            Driver driver = new Driver();
            //注册驱动
            DriverManager.registerDriver(driver);

            /**
             * 获取连接   getConnection(String url,String user ,String password)
             * url:    http://www.baidu.com
             * 参数1:   数据库连接的地址
             *         统一资源定位符  jdbc:mysql://localhost:3306/数据库名
             *          jdbc:mysql:  通信的协议
             *          localhost:3306: 数据库的地址和端口号
             * 参数2: 数据库的用户名  root
             * 参数3:数据库的密码    root
             * */

            String url = "jdbc:mysql://localhost:3306/java2215?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
            String user = "root";
            String password = "root";
            Connection connection = DriverManager.getConnection(url,user,password);
            System.out.println("connection = " + connection);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2

代码如下(示例):

public  static void test02() { // 获取数据库连接的方式2   推荐

        try {
            //通过反射完成驱动的注册
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/java2215?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
            String user = "root";
            String password = "root";
            Connection connection = DriverManager.getConnection(url,user,password);
            System.out.println("connection = " + connection);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

3

 public  static void test03() { // 获取操作数据库的对象 操作数据库  crud
        Connection connection = null;
        ResultSet resultSet = null;
        Statement statement = null;
        try {
            //通过反射完成驱动的注册
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/java2215?useSSL=false&characterEncoding=utf8&serverTimezone=UTC";
            String user = "root";
            String password = "root";
            connection = DriverManager.getConnection(url,user,password);

            //获取操作数据库的对象
            statement = connection.createStatement();
            //数据库的查询操作
            String sql = "select *  from  usr";
            // 发送sql 接收结果集
            resultSet = statement.executeQuery(sql);
            //处理结果集  resultSet.next()  ->  boolean  true  ->处理结果
            ArrayList<User> users = new ArrayList<>();
            while (resultSet.next()){ // 获取当前记录的每一个字段
                //根据当前字段的数据类型以及字段名获取字段值
                int id = resultSet.getInt("id");
                //通过列的索引和数据类型获取字段值   1
                String uname = resultSet.getString(2);
                String pwd = resultSet.getString(3);

              /*  System.out.println("id = " +id);
                System.out.println("uname = " + uname);
                System.out.println("pwd = " + pwd);*/

                User usr = new User(id, uname, pwd);
                users.add(usr);
                System.out.println("usr = " + usr);
                System.out.println("---------------------------------------");
            }



        } catch (Exception e) {
            e.printStackTrace();
        }finally { // 释放资源
            if (resultSet!=null){
                try {
                    resultSet.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (statement!=null){

                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
            if (connection!=null){
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值