(一)JDBC 入门

一、JDBC 概述

(1)JDBC 为访问不同的数据库提供了统一的接口,为使用者屏蔽了细节问题

(2)Java 程序员使用 JDBC,可以连接任何提供了 JDBC 驱动程序的数据库系统,从而完成对数据库的各种操作

(3)JDBC API 是一系列的接口,它统一和规范了应用程序与数据库的连接、执行 SQL 语句,并到得到返回结果等各类操作,相关类和接口在 java.sql 与 javax.sql 包中

二、JDBC 快速入门

    public static void main(String[] args) throws Exception {

        // 1. 注册驱动
        Driver driver = new Driver();

        // 2. 连接
        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");

        Connection connect = driver.connect(url, properties);

        // 3. 执行 sql
        String sql = "insert into actor values(null,'刘德华','男','1997-11-11','110')";
        Statement statement = connect.createStatement();
        // 如果是 dml 语句,返回的是影响的行数
        int rows = statement.executeUpdate(sql);
        System.out.println(rows > 0 ? "成功" : "失败");

        // 4. 关闭连接资源
        statement.close();
        connect.close();
    }

三、获取数据库连接的5种方式

    // 使用 Driver 进行统一管理
    public void getDriver01() throws Exception {
        Driver driver = new Driver();

        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
        Connection connect = driver.connect(url, properties);
    }

    // 反射加载Driver,动态加载
    public void getDriver02() throws Exception {
        Class<?> clazz = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) clazz.newInstance();

        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
        Connection connect = driver.connect(url, properties);
    }

    // 使用 DriverManager 进行统一管理
    public void getDriver03() throws Exception {
        Driver driver = new Driver();
        DriverManager.registerDriver(driver);

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
        String user = "root";
        String password = "root";

        Connection connection = DriverManager.getConnection(url, user, password);
    }

    // 使用 Class.forName 自动完成注册驱动
    public void getDriver04() throws Exception {
        // 在加载 Driver 类时,完成注册(静态代码块中)
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false";
        String user = "root";
        String password = "root";

        Connection connection = DriverManager.getConnection(url, user, password);
    }

    // Properties获取配置
    public void getDriver05() throws Exception {
        // 在加载 Driver 类时,完成注册(静态代码块中)
        Class.forName("com.mysql.jdbc.Driver");

        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        Connection connection = DriverManager.getConnection(url, user, password);
    }

四、Resultset【结果集】

基本介绍

(1)

1.表示数据库结果集的数据表,通常通过执行查询数据库的语句生成
2.Resultset对象保持一个光标指向其当前的数据行。最初,光标位于第一行之前
3.next方法将光标移动到下一行,并且由于在Resultset对象中没有更多行时返回
false,因此可以在while循环中使用循环来遍历结果集 

五、Statement


基本介绍
1.Statement对象用于执行静态SQL语句并返回其生成的结果的对象
2.在连接建立后,需要对数据库进行访问,执行命名或是SQL语句,可以通过
Statement[存在SQL注入]
Preparedstatement[预处理]
CallableStatement【存储过程]
3.Statement对象执行SQL语句,存在SQL注入风险
中品
SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输
入数据中注入非法的SQL语句段或命令,恶意攻击数据库。solinjection.sol
5.要防范SQL注入,只要用Preparedstatement(从Statement扩展而来)取
代Statement就可以了

六、预处理

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值