目录
1. JDBC概念
1.1 什么是JDBC
1.2 JDBC实现形式
1.3 jdbc操作API介绍
1.4 jdbc操作步骤(固定)
2. JDBC实现对数据库crud操作
2.1 第一步 加载数据库驱动
2.2 代码实现查询
2.3 代码实现添加操作
2.4 代码实现修改操作
2.5 代码实现删除操作
1. JDBC概念
1.1 什么是JDBC
(1)Java Database Connectivity,java数据库连接
(2)java里面针对操作数据库,提供一套规范,好比是一些接口,比如操作mysql数据库,mysql提供这些接口实现类
1.2 JDBC实现形式
jdbc是java操作数据库一套规范(接口),由具体数据库提供这些规范实现,以jar包形式提供
1.3 jdbc操作API介绍
(1)DriverManager:注册驱动,创建连接
(2)Connection:数据库连接对象
(3)Statement、PreparedStatement、CallableStatment:执行sql语句对象
(4)Result:查询之后结果
1.4 jdbc操作步骤(固定)
第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
(2)通过反射代码加载驱动对象
第二步 创建数据库连接
第三步 编写sql语句,执行sql语句
第四步 如果执行查询操作,得到查询之后结果集,遍历结果集得到内容
第五步 关闭资源
2. JDBC实现对数据库crud操作
2.1 第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
* 根目录下创建文件夹lib
复制mysql的jar包到lib目录下
到jar包导入项目中
右键点击jar包,点击 Add as library
(2)通过反射代码加载驱动对象
2.2 代码实现查询
//1 查询操作
@Test
public void testSelect() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
//1 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
//第一个参数 url:数据库地址 jdbc:mysql://数据库ip:端口号/数据库名称
/// 如果连接数据库是本地数据库,并且端口号是3306 简写 jdbc:mysql:///db2
//第二个参数 user:用户名
//第三参数 password:密码
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
//3 编写sql语句,创建Statement,执行sql语句
String sql = "select did,dname from dept";
//创建Statement
statement = conn.createStatement();
//statement执行sql语句
rs = statement.executeQuery(sql);
//4 查询返回结果集,遍历显示内容
while(rs.next()) {
// int did = rs.getInt("did");
String did = rs.getString("did");
String dname = rs.getString("dname");
System.out.println("did: "+did);
System.out.println("dname: "+dname);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
rs.close();
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.3 代码实现添加操作
//2 添加操作
@Test
public void testInsert() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写sql语句,创建statement对象执行sql语句
String sql = "insert into dept values(4,'外交部')";
statement = connection.createStatement();
//执行 executeUpdate方法进行添加操作,返回影响行数
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.4 代码实现修改操作
//3 修改操作
@Test
public void testUpdate() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "update dept set dname='对外交流部' where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 代码实现删除操作
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
(1)Java Database Connectivity,java数据库连接
(2)java里面针对操作数据库,提供一套规范,好比是一些接口,比如操作mysql数据库,mysql提供这些接口实现类
1.2 JDBC实现形式
jdbc是java操作数据库一套规范(接口),由具体数据库提供这些规范实现,以jar包形式提供
1.3 jdbc操作API介绍
(1)DriverManager:注册驱动,创建连接
(2)Connection:数据库连接对象
(3)Statement、PreparedStatement、CallableStatment:执行sql语句对象
(4)Result:查询之后结果
1.4 jdbc操作步骤(固定)
第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
(2)通过反射代码加载驱动对象
第二步 创建数据库连接
第三步 编写sql语句,执行sql语句
第四步 如果执行查询操作,得到查询之后结果集,遍历结果集得到内容
第五步 关闭资源
2. JDBC实现对数据库crud操作
2.1 第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
* 根目录下创建文件夹lib
复制mysql的jar包到lib目录下
到jar包导入项目中
右键点击jar包,点击 Add as library
(2)通过反射代码加载驱动对象
2.2 代码实现查询
//1 查询操作
@Test
public void testSelect() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
//1 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
//第一个参数 url:数据库地址 jdbc:mysql://数据库ip:端口号/数据库名称
/// 如果连接数据库是本地数据库,并且端口号是3306 简写 jdbc:mysql:///db2
//第二个参数 user:用户名
//第三参数 password:密码
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
//3 编写sql语句,创建Statement,执行sql语句
String sql = "select did,dname from dept";
//创建Statement
statement = conn.createStatement();
//statement执行sql语句
rs = statement.executeQuery(sql);
//4 查询返回结果集,遍历显示内容
while(rs.next()) {
// int did = rs.getInt("did");
String did = rs.getString("did");
String dname = rs.getString("dname");
System.out.println("did: "+did);
System.out.println("dname: "+dname);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
rs.close();
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.3 代码实现添加操作
//2 添加操作
@Test
public void testInsert() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写sql语句,创建statement对象执行sql语句
String sql = "insert into dept values(4,'外交部')";
statement = connection.createStatement();
//执行 executeUpdate方法进行添加操作,返回影响行数
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.4 代码实现修改操作
//3 修改操作
@Test
public void testUpdate() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "update dept set dname='对外交流部' where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 代码实现删除操作
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
jdbc是java操作数据库一套规范(接口),由具体数据库提供这些规范实现,以jar包形式提供
(1)DriverManager:注册驱动,创建连接
(2)Connection:数据库连接对象
(3)Statement、PreparedStatement、CallableStatment:执行sql语句对象
(4)Result:查询之后结果
1.4 jdbc操作步骤(固定)
第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
(2)通过反射代码加载驱动对象
第二步 创建数据库连接
第三步 编写sql语句,执行sql语句
第四步 如果执行查询操作,得到查询之后结果集,遍历结果集得到内容
第五步 关闭资源
2. JDBC实现对数据库crud操作
2.1 第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
* 根目录下创建文件夹lib
复制mysql的jar包到lib目录下
到jar包导入项目中
右键点击jar包,点击 Add as library
(2)通过反射代码加载驱动对象
2.2 代码实现查询
//1 查询操作
@Test
public void testSelect() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
//1 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
//第一个参数 url:数据库地址 jdbc:mysql://数据库ip:端口号/数据库名称
/// 如果连接数据库是本地数据库,并且端口号是3306 简写 jdbc:mysql:///db2
//第二个参数 user:用户名
//第三参数 password:密码
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
//3 编写sql语句,创建Statement,执行sql语句
String sql = "select did,dname from dept";
//创建Statement
statement = conn.createStatement();
//statement执行sql语句
rs = statement.executeQuery(sql);
//4 查询返回结果集,遍历显示内容
while(rs.next()) {
// int did = rs.getInt("did");
String did = rs.getString("did");
String dname = rs.getString("dname");
System.out.println("did: "+did);
System.out.println("dname: "+dname);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
rs.close();
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.3 代码实现添加操作
//2 添加操作
@Test
public void testInsert() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写sql语句,创建statement对象执行sql语句
String sql = "insert into dept values(4,'外交部')";
statement = connection.createStatement();
//执行 executeUpdate方法进行添加操作,返回影响行数
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.4 代码实现修改操作
//3 修改操作
@Test
public void testUpdate() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "update dept set dname='对外交流部' where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 代码实现删除操作
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
(2)通过反射代码加载驱动对象
第二步 创建数据库连接
第三步 编写sql语句,执行sql语句
第四步 如果执行查询操作,得到查询之后结果集,遍历结果集得到内容
第五步 关闭资源
2.1 第一步 加载数据库驱动
(1)把数据库实现jar包导入项目中
* 根目录下创建文件夹lib
复制mysql的jar包到lib目录下
到jar包导入项目中
右键点击jar包,点击 Add as library
(2)通过反射代码加载驱动对象
2.2 代码实现查询
//1 查询操作
@Test
public void testSelect() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
//1 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
//第一个参数 url:数据库地址 jdbc:mysql://数据库ip:端口号/数据库名称
/// 如果连接数据库是本地数据库,并且端口号是3306 简写 jdbc:mysql:///db2
//第二个参数 user:用户名
//第三参数 password:密码
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
//3 编写sql语句,创建Statement,执行sql语句
String sql = "select did,dname from dept";
//创建Statement
statement = conn.createStatement();
//statement执行sql语句
rs = statement.executeQuery(sql);
//4 查询返回结果集,遍历显示内容
while(rs.next()) {
// int did = rs.getInt("did");
String did = rs.getString("did");
String dname = rs.getString("dname");
System.out.println("did: "+did);
System.out.println("dname: "+dname);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
rs.close();
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.3 代码实现添加操作
//2 添加操作
@Test
public void testInsert() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写sql语句,创建statement对象执行sql语句
String sql = "insert into dept values(4,'外交部')";
statement = connection.createStatement();
//执行 executeUpdate方法进行添加操作,返回影响行数
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.4 代码实现修改操作
//3 修改操作
@Test
public void testUpdate() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "update dept set dname='对外交流部' where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 代码实现删除操作
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
(1)把数据库实现jar包导入项目中
* 根目录下创建文件夹lib
复制mysql的jar包到lib目录下
到jar包导入项目中
右键点击jar包,点击 Add as library
(2)通过反射代码加载驱动对象
//1 查询操作
@Test
public void testSelect() {
Connection conn = null;
Statement statement = null;
ResultSet rs = null;
try {
//1 加载数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
//第一个参数 url:数据库地址 jdbc:mysql://数据库ip:端口号/数据库名称
/// 如果连接数据库是本地数据库,并且端口号是3306 简写 jdbc:mysql:///db2
//第二个参数 user:用户名
//第三参数 password:密码
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db2","root","root");
//3 编写sql语句,创建Statement,执行sql语句
String sql = "select did,dname from dept";
//创建Statement
statement = conn.createStatement();
//statement执行sql语句
rs = statement.executeQuery(sql);
//4 查询返回结果集,遍历显示内容
while(rs.next()) {
// int did = rs.getInt("did");
String did = rs.getString("did");
String dname = rs.getString("dname");
System.out.println("did: "+did);
System.out.println("dname: "+dname);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭资源
try {
rs.close();
statement.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.3 代码实现添加操作
//2 添加操作
@Test
public void testInsert() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写sql语句,创建statement对象执行sql语句
String sql = "insert into dept values(4,'外交部')";
statement = connection.createStatement();
//执行 executeUpdate方法进行添加操作,返回影响行数
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.4 代码实现修改操作
//3 修改操作
@Test
public void testUpdate() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "update dept set dname='对外交流部' where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 代码实现删除操作
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//2 添加操作
@Test
public void testInsert() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写sql语句,创建statement对象执行sql语句
String sql = "insert into dept values(4,'外交部')";
statement = connection.createStatement();
//执行 executeUpdate方法进行添加操作,返回影响行数
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//3 修改操作
@Test
public void testUpdate() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "update dept set dname='对外交流部' where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.5 代码实现删除操作
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//5 删除操作
@Test
public void testDelete() {
Connection connection = null;
Statement statement = null;
try {
//1 加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2 创建数据库连接
connection = DriverManager.getConnection("jdbc:mysql:///db2", "root", "root");
//3 编写修改语句,创建statement对象执行修改语句
String sql = "delete from dept where did=4";
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}catch (Exception e) {
}finally {
//4 关闭资源
try {
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}



