【1-13】概述
2022-02-18 15:00:00 # JDBC

JDBC是什么

  • Java DataBase Connectivity(Java语言连接数据库)

JDBC的本质是什么

  • JDBC是SUN公司指定的一套接口(interface)
  • 在 java.sql.* 下;
  • 可以面向接口编程(降低程序的耦合度,提高程序扩展力)

为什么SUN制定一套JDBC接口

  • 因为每个数据库实现原理不一样
  • 详细如下图…
    对JDBC本质的理解

JDBC编程六步

  1. 注册驱动(告诉Java程序,要连接的是哪个品牌的数据库)
  2. 获取连接(标识JVM的进程和数据库进程之间通道打开,属于进程之间的通讯)
  3. 获取数据库操作对象(专门执行sql语句的对象)
  4. 执行SQL语句(DQL DML…)
  5. 处理查询结果集(select之后执行)
  6. 释放资源(使用完关闭)

完整步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.sql.*;
public class tt4{
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
ResultSet res = null;
try{
// 1. register driver
Class.forName("com.mysql.cj.jdbc.Driver");

// 2. get connection
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sunlie",
"root",
"root"
);

// 3. create database operation object
stmt = conn.createStatement();

// 4. execute SQL statement
String sql = "select empno, ename, sal as xxx from emp";
res = stmt.executeQuery(sql);

// 5. process query result set
int rows = 0;
while(res.next()){
rows++;
/* JDBC index start with 1
String empno = res.getString(1);
String ename = res.getString(2);
String sal = res.getString(3);
*/
// res.getString(这里的列名称是结果集的列名称)
String empno = res.getString("empno");
String ename = res.getString("ename");
String sal = res.getString("xxx");
System.out.println(empno+"\t"+ename+"\t"+sal);
}
System.out.println("rows = " + rows);
/*
7369 SMITH 800.0
7499 ALLEN 1600.0
7521 WARD 1250.0
7566 JONES 2975.0
7654 MARTIN 1250.0
7698 BLAKE 2850.0
7782 CLARK 2450.0
7788 SCOTT 3000.0
7839 KING 5000.0
7844 TURNER 1500.0
7876 ADAMS 1100.0
7900 JAMES 950.0
7902 FORD 3000.0
7934 MILLER 1300.0
rows = 14
*/

}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{
// 6. close resource
try{
if(res != null) res.close();
}catch(Exception e){
e.printStackTrace();
}
try{
if(stmt != null) stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
try{
if(conn != null) conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}

}
}

关于遍历Resultset

遍历Resultset

注册驱动的两种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.sql.*;

public class tt2{
public static void main(String[] args){
Connection conn = null;
Statement stmt = null;
try{
// 1. 注册驱动
/* 1.1 第一种方法
多态:左边是java中的接口,右边是mysql实现类
Driver dirver = new com.mysql.cj.jdbc.Driver();
DriverManager.registerDriver(dirver);
*/
/* 1.2 第二种方法(常用)参数是字符串可以写到配置文件中
com.mysql.cj.jdbc.Driver() 类中静态代码块有相关代码
利用类加载执行静态代码块动作
Class.forName("com.mysql.cj.jdbc.Driver");
*/
Class.forName("com.mysql.cj.jdbc.Driver");


// 2. 获取连接
String url = "jdbc:mysql://127.0.0.1:3306/sunlie";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
System.out.println("数据库连接对象 = " + conn);
// 数据库连接对象 = com.mysql.cj.jdbc.ConnectionImpl@3f200884

}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{

}





}
}

将连接数据库的所有信息放入配置文件中

properties file

1
2
3
4
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/sunlie
user=root
password=root

java file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.sql.*;
import java.util.*;
public class tt3{
public static void main(String[] args){
try{
// 0. bind resource file
ResourceBundle bundle = ResourceBundle.getBundle("tt3");

// 1. register driver
String driver = bundle.getString("driver");
System.out.println("driver = " + driver);

Class.forName(driver);

// 2. get connection
String url = bundle.getString("url");
String user = bundle.getString("user");
String password = bundle.getString("password");
System.out.println("url = " + url);
System.out.println("user = " + user);
System.out.println("password = " + password);

Connection conn = DriverManager.getConnection(url, user, password);
System.out.println("Database connection object = " + conn);
// Database connection object = com.mysql.cj.jdbc.ConnectionImpl@4d339552

}catch(SQLException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}

}
}
Prev
2022-02-18 15:00:00 # JDBC
Next