JDBC——Java DataBase Connectivity
一个独立于特定数据库的管理系统,通用的SQL数据库存取和操作的公共接口。
定义了一组标准,为访问不同的数据库提供了统一途径。类似于一种中间层,在Java程序和数据库中间建立了 一个JDBC,如果在项目中如果需要更换数据库的话那么直接对JDBC进行编程即可。
JDBC体系结构
JDBC接口包括两个层面
- 面向应用的API,供程序员调用。
- 面向数据库的API,面向数据库厂商。
第一层接口是JDBC API,向上给程序员提供一系列接口,在程序中使用。第二层接口是JDBC Driver manager,向下给数据库提供一系列JDBC驱动,用来操作不同类型的数据库,可以实现代码后期的高效维护。
JDBC API
- 提供者:Java官方
- 内容:供开发者调用的接口
- 包含的包:java.sql和javax.sql
其中包含的类和接口有:
- DriverManager类
- Connection接口
- Statement接口
- ResultSet接口
DriverManager
-
提供者:java官方
-
作用:管理不同的JDBC驱动
JDBC驱动
- 提供者:数据库厂商
- 作用:负责连接不同的数据库
JDBC的使用
1、加载数据库驱动,Java程序和数据库之间的桥梁。
2、获取Connection,Java程序与数据库的一次连接。
3、创建Statement对象,由Connection产生,执行SQL语句。
4、如果需要接受返回值,创建ResultSet对象,保存Statement执行之后所查询的结果。
public class Test {
public static void main(String[] args) {
try {
// 通过Class对象调用forname属性,用反射机制将运行时类加载到代码中
/*
老版本的话使用这个,8.0之后的新版本推荐使用cj包下的Driver
*/
// Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
//spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String user = "root";
String password = "guoyuhang";
Connection conn = DriverManager.getConnection(url,user,password);
System.out.println(conn);
// 添加数据
// String sql = "insert into student(id, name) values(1, '张三')";
// 修改数据
// String sql = "update student set name ='李四'";
// 增删改的操作调用executeUpdate()
// Statement statement = conn.createStatement();
// int i = statement.executeUpdate(sql);
// 查询调用executeQuery
String sql = "select * from student";
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println(id+name);
}
// System.out.println(i);
} catch (ClassNotFoundException e) {
System.out.println("JDBC驱动加载错误");
} catch (SQLException e ){
System.out.println(e);
System.out.println("数据库连接异常");
}
}
}
PrepareStatement
Statement的子类,提供了SQL占位符的功能。
使用Statement进行开发有两个问题:
- 需要频繁拼接String 字符串,出错率较高。
- 存在SQL注入的风险。
public class Test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String user = "root";
String password = "guoyuhang";
Connection conn = DriverManager.getConnection(url,user,password);
String sql = "select * from student where id = ? and name = ?";
PreparedStatement preparedStatement = conn.prepareStatement(sql);
Integer id = 1;
String name = "李四";
preparedStatement.setInt(1,id);
preparedStatement.setString(2,name);
ResultSet resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println(id+name);
}
} catch (ClassNotFoundException e) {
System.out.println("JDBC驱动加载错误");
} catch (SQLException e ){
System.out.println(e);
System.out.println("数据库连接异常");
}
}
}