目录

快速入门

# 快速入门

# 准备阶段

  1. MySQL数据库正在运行
  2. 下载数据库驱动的JAR包(Download mysql-connector-java JAR file with all dependencies (jar-download.com) (opens new window)
  3. 在IDE中导入下载好的驱动包进项目

# 小试牛刀

原数据库表 ![[Pasted image 20220606230651.png]]

结果数据库表 ![[Pasted image 20220606230804.png]]

# 执行代码
public static void main(String[] args) throws ClassNotFoundException, SQLException {  
    String url,username,password;  
    url = "jdbc:mysql://127.0.0.1:3306/testing";  
    username = "root";  
    password = "Lam1134312514";  
  
    Class.forName("com.mysql.cj.jdbc.Driver"); //注册数据库驱动
    Connection connection = DriverManager.getConnection(url,username,password); //建立数据库链接 
    Statement statement = connection.createStatement();  
    statement.executeUpdate("update mytable set personID = 10 where personID = 1");

	// 释放资源
	connection.close();
	statement.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 类说明

  1. DriverManager
  2. Connection
# DriverManager

管理一组JDBC驱动程序的基本服务 作用:

  1. 注册驱动
  2. 获取数据库连接

注册驱动DriverManager中提供了一个registerDriver()函数用于注册驱动,但我们看到上述代码中明明是用Class.forName("com.mysql.cj.jdbc.Driver");注册驱动的,但你点击Driver类后,就能看到其实底层就是调用该方法注册,

在MySQL5以后,开发人员甚至可以不写这行注册代码,因为在驱动JAR文件包下的META-INF -> services -> java.sql.Driver文件中,已经默认帮我们注册了驱动。

获取数据库连接DriverManager中还提供了一个getConnection(String url, String username, String password)函数用于获取连接。

URL - 连接路径 语法: jdbc:mysql://[ip地址(域名)]:[端口]/[数据库名称]?[参数键值对1]&[参数键值对2]... 例子: jdbc:mysql://127.0.0.1:3306/myTable 细节:

  • 如果连接的是本机MySQL服务器,并且MySQL服务器默认端口是3306,则URL可以简写为 jdbc:mysql://[数据库名]?[键值对1]...
  • 数据库配置useSSL=false参数,禁用安全连接方式,解决警告提示
# Connection

作用:

  • 获取执行SQL的对象
  • 管理事务

获取执行SQL的对象 ![[Pasted image 20220606233806.png]]

事务管理 ![[Pasted image 20220606234125.png]]

代码样例

public static void main(String[] args) throws ClassNotFoundException, SQLException {  
    String url,username,password;  
    int a,b;  
    url = "jdbc:mysql://127.0.0.1:3306/testing";  
    username = "root";  
    password = "Lam1134312514";  
  
    Connection connection = DriverManager.getConnection(url,username,password);  
    Statement statement = connection.createStatement();  
    try {  
        connection.setAutoCommit(false); //默认情况下开启的是自动提交,为显示效果,我们选择手动  
        a = statement.executeUpdate("update mytable set personID = 133 where personID = 1");  
        b = statement.executeUpdate("update mytable set personID = 122 where personID = 12");  
        int k = 1/0;  
        connection.commit(); //提交事务  
    } catch (Exception e) {  
        connection.rollback();  
        throw new RuntimeException(e);  
    }  
    System.out.println(a+""+b);  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Statement

作用:

  • 执行SQL语句

执行SQL语句 ![[Pasted image 20220606235541.png]]

代码样例

public static void main(String[] args) throws ClassNotFoundException, SQLException {  
    String url,username,password;  
    int a;  
    url = "jdbc:mysql://127.0.0.1:3306/testing";  
    username = "root";  
    password = "Lam1134312514";  
  
    Connection connection = DriverManager.getConnection(url,username,password);  
    Statement statement = connection.createStatement();  
    a = statement.executeUpdate("insert into mytable values (3,17160826003)");  
  
    System.out.println(a);  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
# ResultSet

![[Pasted image 20220607000515.png]]

代码样例

public static void main(String[] args) throws ClassNotFoundException, SQLException {  
    String url,username,password;  
    url = "jdbc:mysql://127.0.0.1:3306/testing";  
    username = "root";  
    password = "Lam1134312514";  
  
    Connection connection = DriverManager.getConnection(url,username,password);  
    Statement statement = connection.createStatement();  
  
    ResultSet rs = statement.executeQuery("SELECT * FROM mytable");  
    rs.next();  
    System.out.println(rs.getInt(1));  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
# PreparedStatement

预编译SQL语句并执行,用来预防SQL注入问题。

![[Pasted image 20220607004557.png]]

代码样例

public static void main(String[] args) throws ClassNotFoundException, SQLException {  
    String url,username,password,sql;  
    int a;  
    url = "jdbc:mysql://127.0.0.1:3306/testing";  
    username = "root";  
    password = "Lam1134312514";  
    sql = "select * from mytable where personID = ?";  
  
    Connection connection = DriverManager.getConnection(url,username,password);  
    PreparedStatement ps = connection.prepareStatement(sql);  
    ps.setInt(1,1);  
    ResultSet rs = ps.executeQuery();  
  
    while(rs.next()){  
        System.out.println(rs.getInt(2));  
    }  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# 数据库连接池Druid

  • 数据库连接池是个容器,负责分配、管理数据库连接(Connection)
  • 它允许应用程序反复使用一个现有的数据库连接,而不是再重新建立一个
  • 释放空闲时间超过最大空闲时间的数据库连接来避免因为没有释放数据库连接而引起的数据库连接遗漏
  • 好处:
    • 资源重用
    • 提升系统响应速度
    • 避免数据库连接遗漏

# 注意

Connection、Statement、ResultSet的资源需要在使用后用close()释放!

最近更新
01
基本知识
07-18
02
卷积神经网络识别图像
07-18
03
损失函数
07-18
更多文章>