JDBC的API详解

8 篇文章 0 订阅

🍎道阻且长,行则将至。🍓


 目录

一、DriverManager 驱动管理类

1.注册驱动

2.获取数据库连接

二、Connection 数据库连接对象

1.获取执行对象

2.事务管理

三、Statement

1.执行DDL、DML语句

2.执行DQL语句

四、ResultSet


JDBC快速入门的例子为引,使用JDBC有驱动管理,连接对象,执行SQL、查询语句等,下面将对JDBC中常见API进行解释。

public static void main(String[] args) throws Exception {
        //1. 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //2. 获取连接
        String url = "jdbc:mysql://127.0.0.1:3306/db1";
        String username = "root";
        String passd = "1234";
        Connection conn = DriverManager.getConnection(url, username, passd);
        //3. 定义sql
        String sql = "update account set money = 2000 where id = 1";
        //4. 获取执行sql的对象 Statement
        Statement stmt = conn.createStatement();
        //5. 执行sql
        int count = stmt.executeUpdate(sql);//受影响的行数
        //6. 处理结果
        System.out.println(count);
        //7. 释放资源
        stmt.close();
        conn.close();
    }

一、DriverManager 驱动管理类

1.注册驱动

在入门案例中我们使用的是:

Class.forName("com.mysql.jdbc.Driver");

先跳转到Driver具体来看,在mysql.jdbc中的Driver类中:

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    public Driver() throws SQLException {
    }
    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}

静态代码块执行 DriverManager 对象的 registerDriver() 方法进行驱动的注册,只要加载 Driver 类,该静态代码块就会执行案例中的Class.forName("com.mysql.jdbc.Driver"); 就会加载 Driver 类。

p:在MySQL 5之后的驱动包,可以省略注册驱动的步骤,

 自动加载jar包中META-INF/services/java.sql.Driver文件中的驱动类。

2.获取数据库连接

使用的是DriverManager的getConnection方法,方法有三个参数:String url, String user, String password,分别是数据库连接路径、用户名及密码。

    @CallerSensitive
    public static Connection getConnection(String url,
        String user, String password) throws SQLException {
        java.util.Properties info = new java.util.Properties();

        if (user != null) {
            info.put("user", user);
        }
        if (password != null) {
            info.put("password", password);
        }

        return (getConnection(url, info, Reflection.getCallerClass()));
    }

连接路径url,通常我们这样写:

jdbc:mysql://ip地址(或域名):端口号/数据库名称?参数键值对1&参数键值对2

  • 对于本地mysql服务器,且默认端口3306,可以简写:jdbc:mysql:///数据库名称?参数键值对 
  • 添加参数键值对:useSSL=false,禁用安全连接方式,可以解决警告提示。对于多个键值对使用&连接。

二、Connection 数据库连接对象

作用是获取执行 SQL 的对象、管理事务。

1.获取执行对象

普通执行SQL对象—Statement

//4. 获取执行sql的对象 Statement
Statement stmt = conn.createStatement();

还有不是普通的:

预编译SQL的执行SQL对象

Creates a CallableStatement object for calling database stored procedures.The CallableStatement object provides methods for setting up its IN and OUT parameters, and methods for executing the call to a stored procedure.

创建用于调用数据库存储过程的CallableStatement对象。CallableStatement对象提供了设置其IN和OUT参数的方法,以及执行对存储过程的调用的方法。

注意:此方法针对处理存储过程调用语句进行了优化。当prepareCall方法完成时,某些驱动程序可能会将call语句发送到数据库;其他的可以等到CallableStatement对象执行完。这对用户没有直接影响;但是,它确实会影响哪个方法抛出某些SQL Exceptions。

执行存储过程的

Converts the given SQL statement into the system's native SQL grammar. A driver may convert the JDBC SQL grammar into its system's native SQL grammar prior to sending it. This method returns the native form of the statement that the driver would have sent.

将给定的SQL语句转换成系统的本机SQL语法。驱动程序可以在发送之前将JDBC SQL语法转换成其系统的本地SQL语法。

p:存储过程在MySQL中不常用。

2.事务管理

MySQL有以下事务管理的操作:

开启事务 : BEGIN;   START TRANSACTION;

提交事务 : COMMIT;

回滚事务 : ROLLBACK;

JDBC在Connection接口中定义了3个对应的事务管理方法:

void setAutoCommit(boolean autoCommit) throws SQLException;
//开启事务需要将该参数设为为false
void commit() throws SQLException;
void rollback() throws SQLException;

执行一个简单事务例子:

//定义了两个要执行的sql
String sql = "update account set money = 2000 where id = 2";
String sql2 = "update account set money = 2000 where id = 1";

// 开启事务 【连接conn、执行sql的对象 Statement stmt】
try {
    conn.setAutoCommit(false);
    int count = stmt.executeUpdate(sql);//受影响的行数
    System.out.println(count);
    int count2 = stmt.executeUpdate(sql2);//受影响的行数
    System.out.println(count2);
}catch (Exception throwables){
    conn.rollback();
}

三、Statement

作用就是执行SQL语句。

1.执行DDL、DML语句

例如上一个例子中的 int count = stmt.executeUpdate(sql);   ,executeUpdate就是用于执行DDL和DML的。

2.执行DQL语句

对于一个DQL查询语句使用是executeQuery

String sql = "select * from account";

ResultSet rs=stmt.executeQuery(sql);

这个方法使用到了 ResultSet结果集 对象,封装了SQL查询语句的结果。

四、ResultSet

上一节提到ResultSet,封装了SQL查询语句的结果。我们打开ResultSet接口,其中定义了next和一些getXxx方法来操作查询结果:

boolean next() throws SQLException;

String getString(int columnIndex) throws SQLException;
boolean getBoolean(int columnIndex) throws SQLException;
byte getByte(int columnIndex) throws SQLException;
int getInt(int columnIndex) throws SQLException;
double getDouble(int columnIndex) throws SQLException;
......
String getString(String columnLabel) throws SQLException;
......
  • next();

将光标从当前位置向前移动一行,判断当前行是否为有效行。(就是找数据库表下一行)

方法返回值:

true :当前行有数据

false :当前行没有数据

  • getXxx(参数);

获取数据。

参数有两种类型:

int类型的参数:列的编号,从1开始

String类型的参数: 列的名称

  • 例,定义查询sql并显示查询内容;

String sql = "select * from account";

 

  • close  最后也是需要释放资源。

void close() throws SQLException;

rs.close();


☕物有本末,事有终始,知所先后。🍭

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Super algorithm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值