当前位置:网站首页>Understand JDBC in one article
Understand JDBC in one article
2022-07-30 06:34:00 【P4nic】
JDBC概念:
Java数据库连接(Java DataBase Connectivity)
JDBC是sun公司发布的一个javaAn interface for communication between programs and databases(规范)
各大数据库厂商去实现JDBC规范,And package the implementation class into jar包

我们可以使用这套接口编程,真正执行的代码是驱动jar包中的实现类
JDBC的几个类:
0x01 DriverManager:
驱动管理对象
1.注册驱动:Tell the program which database to use
Class.forName("com.mysql.cj.jdbc.Driver")Class.forNamefor reflection technology,要求JVM查找并加载指定的类
When a class is loaded, the static code of the class is executed
查看com.mysql.cj.jdbc.Driver源码,It is found that the static code is calledDriverManager类的registerDriver方法
package com.mysql.cj.jdbc;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Driver extends NonRegisteringDriver implements java.sql.Driver {
public Driver() throws SQLException {
}
static {
try {
// 使用DriverManager类的registerDriverStatic method to register the driver
DriverManager.registerDriver(new Driver());
} catch (SQLException var1) {
throw new RuntimeException("Can't register driver!");
}
}
}注:mysql5以上版本的驱动jar包可以省略注册驱动的步骤
That is, it can be omitted in the programClass.forName("com.mysql.cj.jdbc.Driver");这句话
驱动jar包下的META-INF下的services文件夹中的java.sql.Driver文件中已经把DriverThe classpath is recorded(SPI技术)
If the driver is not registered in the program,会先读取这个文件,自动注册驱动
# SPI:Service Provide Interface 一种服务发现机制
它通过在ClassPath路径下的META-INF/services文件夹查找文件

2.获取数据库连接
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");参数1:url 指定连接路径
jdbc:mysql://ip(域名):端口号/数据库名称
注:If the connection is the local machinemysql服务器,并且mysql服务默认端口是3306,则url可以简写为:
jdbc:mysql:///数据库名 (默认localhost和端口号3306)
If there is a Chinese garbled problem later,可在url后加上 ?useUnicode=true&characterEncoding=utf-8
参数2:user:用户名
参数3:password:密码
0x02 Connection
数据库连接对象
1.获取执行sql的对象Statement
Statement stmt = con.createStatement();2.管理事务
a. 开启事务 调用该方法设置参数为false,即开始事务
void setAutoCommit(boolean autoCommit)b. 提交事务
void commit()c. 回滚事务
void rollback()0x03 Statment
执行sql的对象
用于执行静态sql并返回其执行结果
1. int executeUpdate(String sql)
执行DML语句(insert、update、delete)、DDL语句(create、alter、drop)
返回值为影响的行数
2. ResultSet executeQuery(String sql)
执行DQL语句(select),返回结果集对象ResultSet
int count = stmt.executeUpdate(sql);0x04 ResultSet
结果集对象,封装查询结果
Similar to an array of pointers,There is a cursor pointing to each row
next() 方法:游标向下移动一行(Just start on the first line,字段行),判断该行是否有数据,返回true
getXxx(参数) 方法:获取数据,Only one column can be fetched at a time
Xxx表示数据类型 如getInt()、getString()
参数类型:Int:代表列的编号,从1开始 String:Represents the field name
利用next()You can iterate through the query results
ResultSet res = stmt.executeQuery();
while(res.next()) {
// 处理查询结果
}0x05 PreparedStatement
PreparedStatement是Statement的子类
执行预编译sql的对象,for defensesql注入
预编译sql:参数用?作为占位符 如:
select * from user where username = ? and password = ?
Get execute precompilesql的对象: PreparedStatement prepareStatement(String sql)
给?赋值 ---> setXxx(参数1,参数2)
参数1:?的位置,从1开始
参数2:?的值
PreparedStatement pstm = conn.prepareStatement(sql);
pstm = conn.prepareStatement(sql);
pstm.setString(1,username);
pstm.setString(2,password);
// 执行查询,与Statement不同,不需要再次传入sql
res = pstm.executeQuery(); JDBC操作数据库:
1.导入驱动jar包
2.注册驱动
3.Get the object to connect to the database
4.获取sql执行对象
5.执行sql
6.释放资源
导入驱动jar包:
新建一个libs文件夹,将驱动jar包复制进去

右击libs文件夹,选择 add as library
import com.mysql.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Jdbc_Demo {
public static void main(String[] args) throws Exception {
// 导入驱动jar包,在工程下的libs文件夹(自己命名),add as libraries
// 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取数据库连接对象
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
// 定义sql语句
String sql = "update account set balance = 555500 where id = 1";
// 获取执行sql的对象Statement
Statement stmt = con.createStatement();
// 执行sql
int count = stmt.executeUpdate(sql);
// 处理对象
System.out.println(count);
// 释放资源
stmt.close();
con.close();
}
}边栏推荐
猜你喜欢

别找了,你要的C语言“数组”在这里
awd——waf部署

uni-app installs components using npm commands

Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)

JDBC一文搞懂

强国杯初赛WP

Qt实现单击或双击QTableWidge/View表头进行排序

C语言:通过函数实现一个整形有序数组的二分查找
文件上传漏洞的绕过
![[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)
[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app
随机推荐
自定义异常类的使用
【小程序项目开发-- 京东商城】uni-app之分类导航区域
Sql操作
最新版redis6.3.2下载安装
mysql处理insert冲突的解决方案
CTF之misc-日志分析
【墨者学院】身份认证失效漏洞实战
一类SMS漏洞的防御思路
Connect to Mysql in the cloud server Docker detailed graphic and text operations (full)
Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System
torch分布式训练
FastAPI 快速入门
封装Cookie API
jsonpath
jsonpath
运算符和交互基础
SSTI靶场
Misc of CTF - other types of steganography
CTF之misc-其他类型隐写
认识虚拟dom