16JavaSEJDBC初体验

一、JDBC常用类和接口

JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API。JDBC是Java访问数据库的标准规范,可以为不同的关系型数据库提供统一访问,它由一组用Java语言编写的接口和类组成。

JDBC**与数据库驱动的关系:接口与实现类的关系。**

二、JDBC常用类和接口

JDBC有关的类:都在java.sql 和 javax.sql 包下.

接口在Java中是用来定义 行为规范的. 接口必须有实现类.

JDBC规范(四个核心对象):

DriverManager:用于注册驱动

Connection: 表示与数据库创建的连接

Statement: 操作数据库sql语句的对象

ResultSet: 结果集或一张虚拟表

特别注意:

Mysql驱动的driverClass 和url 的写法

com.mysql.jdbc.Driver


jdbc:mysql://localhost:3306/text

1.JDBC 初体验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// JDBC 初体验
@Test
public void demo01() throws SQLException {
// 1. 装载驱动
DriverManager.registerDriver(new Driver());
// 2. 建立连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "111");
// 3. 操作数据
String sql = "select * from user;";
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
int id = rs.getInt("id");
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
System.out.println(id + " : " + username + " : " + password + " : " + email);
}
// 4. 释放资源
rs.close();
stmt.close();
conn.close();
}

2.JDBC工具类的提取()

方式一、

–src下放连接数据库的配置文件

|--properties
1
2
3
4
5
6
7
 // 配置文件的名字     jdbc.properties

#mysql
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb
user=root
password=111

–util包

|--JDBCUtils 类   

|
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
84
85
public class JDBCUtils {

// 属性
private static String driverClass;
private static String url;
private static String username;
private static String password;

// 什么时候加载外部配置文件最合适 ???
// 特点1 : 随着类的加载而加载.
// 特点2 : 静态代码块只在类加载的被执行一次. 仅一次.
static {
Properties prop = new Properties();

try {
prop.load(new FileReader("jdbc.properties"));

// 如果程序执行到这里, 说明外部资源文件加载成功, 需要给我们的静态属性赋值
driverClass = prop.getProperty("driverClass");
url = prop.getProperty("url");
username = prop.getProperty("username");
password = prop.getProperty("password");

// 直接执行加载驱动
loadDriver();

} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("文件资源加载失败!");
}
}

// 加载驱动
public static void loadDriver() {
try {
// 1. 加载驱动
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
// e.printStackTrace();
// 驱动加载失败!
throw new RuntimeException("驱动加载失败!");
}
}

// 建立连接
public static Connection getConnection() throws SQLException {
// 2. 建立连接
return DriverManager.getConnection(url, username, password);
}

// 释放资源
public static void release(Connection conn, Statement stmt, ResultSet rs) {
// 4. 释放资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
// 将 rs 清空
rs = null;
}
// 直接调用
release(conn, stmt);
}
public static void release(Connection conn, Statement stmt) {
// 4. 释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
}

3.测试

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	@Test
public void test_update() {
Connection conn = null;
Statement stmt = null;
try {
// 2. 建立连接
conn = JDBCUtils.getConnection();
// 3. 操作数据
String sql = "update user set username = 'zhaoliu', password = '123', email = 'zhaoliu@youjian.cn' where id = 4;";
stmt = conn.createStatement();
int affectedRowNum = stmt.executeUpdate(sql);
System.out.println(affectedRowNum);
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 4. 释放资源
JDBCUtils.release(conn, stmt);
}
}

@Test
public void test_delete() {
Connection conn = null;
Statement stmt = null;

try {
// 1. 建立连接
conn = JDBCUtils.getConnection();
// 2. 操作数据
String sql = "delete from user where id = 5;";
stmt = conn.createStatement();
int affectedRowNum = stmt.executeUpdate(sql);
System.out.println(affectedRowNum);
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 4. 释放资源
JDBCUtils.release(conn, stmt);
}
}

@Test
public void test_insert() {

Connection conn = null;
Statement stmt = null;
try {
// 1. 建立连接
conn = JDBCUtils.getConnection();
// 2. 操作数据
String sql = "insert into user values(null, 'xiaoqi', '123', 'xiaoqi@youjian.cn');";
stmt = conn.createStatement();
int affectedRowNumber = stmt.executeUpdate(sql);
System.out.println(affectedRowNumber);
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 4. 释放资源
JDBCUtils.release(conn, stmt);
}
}


// 以上使用时 在进行查询的操作时 有可能会出现 sql注入问题
// 解决SQL注入:使用PreparedStatement 取代 Statement
// PreparedStatement 解决SQL注入原理,运行在SQL中参数以?占位符的方式表示
// select * from user where username = ? and password = ? ;
// 将带有?的SQL 发送给数据库完成编译 (不能执行的SQL 带有?的SQL 进行编译 叫做预编译),在SQL编译后发现缺少两个参数
// PreparedStatement 可以将? 代替参数 发送给数据库服务器,因为SQL已经编译过,参数中特殊字符不会当做特殊字符编译,无法达到SQL注入的目的



/************ JDBC 数据库连接操作 ***************/
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;

try {
// 1. 建立连接
conn = JDBCUtils.getConnection();
// 2. 操作数据
String sql = "select * from user where username = ? and password = ?;";
stmt = conn.prepareStatement(sql);
// 设置sql语句的参数
stmt.setString(1, username);
stmt.setString(2, password);
// 执行sql语句
rs = stmt.executeQuery();
// 判断返回的结果
if (rs.next()) {
// 登录成功
int id = rs.getInt("id");
String u_name = rs.getString("username");
String u_pwd = rs.getString("password");
String email = rs.getString("email");
System.out.println(id + " : " + u_name + " : " + u_pwd + " : " + email);
System.out.println("登录成功!");
} else {
// 登录失败
System.out.println("登录失败! 用户名或密码错误!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 3. 释放资源
JDBCUtils.release(conn, stmt, rs);
}
}
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2016 - 2018 Francis的个人博客 All Rights Reserved.

UV : | PV :