ToB企服应用市场:ToB评测及商务社交产业平台
标题:
Java操纵数据库 —— JDBC ① 底子篇
[打印本页]
作者:
惊雷无声
时间:
2024-8-9 20:46
标题:
Java操纵数据库 —— JDBC ① 底子篇
目次
一、JDBC简介
JDBC 概念
JDBC 本质:
JDBC 利益:
二、JDBC快速入门
步骤
三、JDBC 焦点API 详解
1.注册驱动
2.Connection
3.Statement(相识)
4.PreparedStatement
5.ResultSet
四、基于Preparedstatement实现CRUD
1.查询单行单列
2.查询单行多列
3.查询多行多列
4.新增
5.修改
6.删除
7.总结
五、常见题目
1.资源的管理
2.SQL语句题目
3.SQL语句未设置参数题目
4.用户名或密码错误题目
5.通讯异常
我走我的路,有人拦也走,没人陪也走
—— 24.6.7
JDBC
JDBC就是使用Java语言操纵关系型数据库的一套
API
一、JDBC简介
JDBC 概念
JDBC 就是使用
Java语言操纵关系型数据库
的一套API
全称
Java DataBase Connectivity)意为Java 数据库连接
JDBC 本质:
① 官方(sun公司)界说的一套操纵全部关系型数据库的规则,即接口
② 各个数据库厂商去实现这套接口,提供数据库驱动
jar包
③ 我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类
JDBC 利益:
① 各数据库厂商使用相同的接口,Java代码不必要针对不同数据库分别开辟
② 可随时替换底层数据库,访问数据库的Java代码根本不变
二、JDBC快速入门
步骤
package JavaJDBCBase;
import java.sql.*;
public class Demo1JDBCQuick {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.获取数据库连接
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
// 3.获取执行sql的对象,statement(把sql语句发送给MySQL)
Statement stmt = conn.createStatement();
// 4.编写sql语句并执行,以及接收返回的结果集
String sql = "select emp_id,emp_name,emp_salary,emp_age from t_tmp";
ResultSet result = stmt.executeQuery(sql);
// 5.处理结果,遍历result结果集 next方法,判断有没有下一行
while (result.next()) {
int empId = result.getInt("emp_id");
String empName = result.getString("emp_name");
double empSalary = result.getDouble("emp_salary");
int empAge = result.getInt("emp_age");
System.out.println(empId+"\t"+empName+"\t"+empSalary+"\t"+empAge);
}
// 6.释放资源 先开启后关闭原则
result.close();
stmt.close();
conn.close();
}
}
复制代码
三、JDBC 焦点API 详解
1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
复制代码
在 Java 中,当使用 JDBC(Java Database Connectivity)连接数据库时,必要加载数据库特定的驱动程序,以便与数据库举行通讯。加载驱动程序的目标是为了注册驱动程序使得 JDBC API可以或许识别并与特定的数据库举行交互。
// 1. 注册驱动
// Class.forName("com.mysql.cj.jdbc.Driver");
DriverManager.registerDriver(new Driver());
复制代码
从JDK6开始,
不再必要显式地调用 class.forName()
来加载JDBC 驱动程序,只要在类路径中集成了对应的jar文件,会
自动
在初始化时注册驱动程序。
2.Connection
Connection
接口是JDBC API的重要接口,用于建立与数据库的通讯通道。换而言之,Connection对象不为空则代表一次数据库连接。
在建立连接时,必要指定数据库URL、用户名、密码参数。
URL:jdbc:mysql://localhost:3306/atguigu"
jdbc:mysql://IP所在:端口号/数据库名称 ? 参数键值对1 & 参数键值对2
Connection 接口还负责管理事务,Connection 接口提供了 commit 和 rollback 方法,用于提交事务和回滚事务。
可以创建 statement 对象,用于执行 SQL语句并与数据库举行交互。
在使用JDBC技术时,必须要先获取Connection对象,在使用完毕后,要开释资源,制止资源占用浪费及泄漏。
3.Statement(相识)
Statement 接口用于执行 SQL语句并与数据库举行交互。它是 JDBC API 中的一个重要接口。通过Statement 对象,可以向数据库发送 SQL语句并获取执行结果。
结果可以是一个或多个结果。
增删改:受影响行数单个结果。
查询:单行单列、多行多列、单行多列等结果。
但是 Statement 接口在执行SQL语句时,会产生SQL注入攻击题目:
当使用 statement 执行动态构建的 SQL查询时,往往必要将查询条件与SQL语句拼接在一起,直接将参数和SQL语句一并生成,让SQL的查询条件始终为true得到结果。
4.PreparedStatement
Preparedstatement
是 Statement 接口的子接口,用于执行 预编译的 SQL查询,作用如下。
预编译SQL语句:在创建Preparedstatement时,就会预编译SQL语句,也就是SQL语句已经固定
防止SQL注入: Preparedstatement 支持参数化查询,将数据作为参数转达到SQL语句中,采用?占位符的方式,将传入的参数用一对单引号包裹起来",无论转达什么都作为值。有效防止传入关键字或值导致SQL注入题目。
性能提升
reparedstatement是预编译SQL语句,同一SQL语句多次执行的情况下,可以复用,不必每次重新编译和分析。
import java.sql.*;
import java.util.Scanner;
public class Demo2PreparedStatement {
public static void main(String[] args) throws Exception {
// 1.注册驱动
// 2.获取链接对象
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.获取执行sql语句对象
PreparedStatement preparedStatement = conn.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_id=?");
System.out.println("请输入员工编号:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
// 4.为?占位符赋值,并执行sql语句,并执行,接受返回的结果
preparedStatement.setString(1,name);
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果,遍历esultSet
while(resultSet.next()){
int emp_id = resultSet.getInt("emp_id");
String emp_name = resultSet.getString("emp_name");
int emp_age = resultSet.getInt("emp_age");
double emp_salary = resultSet.getDouble("emp_salary");
System.out.println(emp_id+"\t"+emp_name+"\t"+emp_age+"\t"+emp_salary);
}
// 6.释放资源
resultSet.close();
preparedStatement.close();
conn.close();
}
}
复制代码
package JavaJDBCBase;
import java.sql.*;
import java.util.Scanner;
public class Demo2PreparedStatement {
public static void main(String[] args) throws Exception {
// 1.注册驱动
// 2.获取链接对象
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.获取执行sql语句对象
PreparedStatement preparedStatement = conn.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_name=? || emp_salary=?");
System.out.println("请输入员工姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
System.out.println("请输入员工工资");
double salary = sc.nextDouble();
// 4.为?占位符赋值,并执行sql语句,并执行,接受返回的结果 int类型:参数的下标,从0开始,要替换的值为多少
preparedStatement.setString(1,name);
preparedStatement.setDouble(2,salary);
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果,遍历resultSet
while(resultSet.next()){
int emp_id = resultSet.getInt("emp_id");
String emp_name = resultSet.getString("emp_name");
int emp_age = resultSet.getInt("emp_age");
double emp_salary = resultSet.getDouble("emp_salary");
System.out.println(emp_id+"\t"+emp_name+"\t"+emp_age+"\t"+emp_salary);
String goal = resultSet.getString("emp_id");
System.out.println(emp_name+"id为:"+goal);
}
// 6.释放资源
resultSet.close();
preparedStatement.close();
conn.close();
}
}
复制代码
5.ResultSet
ResultSet 是
JDBC API
中的一个接口,用于表现从数据库中
执行査询语句所返回的结果集。
它提供了一种于遍历和访问查询结果的方式。
遍历结果:Resultset可以使用
next()方法
将游标移动到结果集的下一行,逐行遍历数据库查询的结果,返回值为boolean类型,true代表有下一行结果,false则代表没有。
获取单列结果:可以通过
get类型()
的方法获取单列的数据,该方法为重载方法,支持
索引和列名
举行获取。
String goal = resultSet.getString("emp_name");
System.out.println(goal);
复制代码
package JavaJDBCBase;
import java.sql.*;
import java.util.Scanner;
public class Demo2PreparedStatement {
public static void main(String[] args) throws Exception {
// 1.注册驱动
// 2.获取链接对象
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection conn = DriverManager.getConnection(url,username,password);
// 3.获取执行sql语句对象
PreparedStatement preparedStatement = conn.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_id=?");
System.out.println("请输入员工编号:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
// 4.为?占位符赋值,并执行sql语句,并执行,接受返回的结果
preparedStatement.setString(1,name);
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果,遍历esultSet
while(resultSet.next()){
int emp_id = resultSet.getInt("emp_id");
String emp_name = resultSet.getString("emp_name");
int emp_age = resultSet.getInt("emp_age");
double emp_salary = resultSet.getDouble("emp_salary");
System.out.println(emp_id+"\t"+emp_name+"\t"+emp_age+"\t"+emp_salary);
String goal = resultSet.getString("emp_name");
System.out.println(goal);
}
// 6.释放资源
resultSet.close();
preparedStatement.close();
conn.close();
}
}
复制代码
四、基于Preparedstatement实现CRUD
1.查询单行单列
// 单行单列查询
@Test
public void testQuerySingleRowAndCol() throws SQLException {
// 1.注册驱动
// 2.获取连接
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection connection = DriverManager.getConnection(url, username, password);
// 3.预编译SQL语句得到PreparedStatement对象
PreparedStatement preparedStatement = connection.prepareStatement("select count(*) as count from t_tmp");
// 4.执行语句 获取结果
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果,进行遍历,如果明确只有一个结果,那么resultSet至少要做一次next的判断,才能拿到我们要的列的结果
while (resultSet.next()) {
// 用下标获取
int anInt = resultSet.getInt(1);
System.out.println(anInt);
}
// 6.释放资源
resultSet.close();
preparedStatement.close();
connection.close();
}
复制代码
2.查询单行多列
// 单行多列查询
@Test
public void testQuerySingleRowAndCol2() throws SQLException {
// 1.注册驱动
// 2.获取链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBC", "root", "954926928lcl");
// 3.预编译SQL语句获得PreparedStatement对象
PreparedStatement preparedStatement = connection.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_id = ?");
// 4.为占位符赋值,然后执行,并接受结果
preparedStatement.setInt(1,5);
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果
while (resultSet.next()) {
int empId = resultSet.getInt("emp_id");
String empName = resultSet.getString("emp_name");
double empSalary = resultSet.getDouble("emp_salary");
int empAge = resultSet.getInt("emp_age");
System.out.println(empId+"\t"+empName+"\t"+empSalary+"\t"+empAge);
}
// 6.资源释放
resultSet.close();
preparedStatement.close();
connection.close();
}
复制代码
3.查询多行多列
// 多行多列查询
@Test
public void testQueryMoreRow() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement preparedStatement = conn.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_age > ?");
preparedStatement.setInt(1,25);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int empId = resultSet.getInt("emp_id");
String empName = resultSet.getString("emp_name");
double empSalary = resultSet.getDouble("emp_salary");
int empAge = resultSet.getInt("emp_age");
System.out.println(empId+"\t"+empName+"\t"+empSalary+"\t"+empAge);
}
resultSet.close();
preparedStatement.close();
conn.close();
}
复制代码
4.新增
// 新增
@Test
public void testInsert() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement preparedStatement = conn.prepareStatement("insert into t_tmp(emp_name,emp_salary,emp_age) values(?,?,?)");
preparedStatement.setString(1,"rose");
preparedStatement.setDouble(2,345.67);
preparedStatement.setInt(3,36);
int result = preparedStatement.executeUpdate();
// 根据受影响行数做判断,得到成功或失败
if (result>0){
System.out.println("成功!");
}else{
System.out.println("失败!");
}
preparedStatement.close();
conn.close();
}
复制代码
5.修改
// 修改
@Test
public void testUpdate() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement preparedStatement = conn.prepareStatement("update t_tmp set emp_salary = ? where emp_id=?");
preparedStatement.setDouble(1,888.88);
preparedStatement.setInt(2,6);
int result = preparedStatement.executeUpdate();
if (result>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
preparedStatement.close();
conn.close();
}
复制代码
6.删除
// 删除
@Test
public void testDelete() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
// 手写SQL语句
PreparedStatement preparedStatement = conn.prepareStatement("delete from t_tmp where emp_id=?");
preparedStatement.setInt(1,5);
int result = preparedStatement.executeUpdate();
if (result>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
preparedStatement.close();
conn.close();
}
复制代码
7.总结
package JavaJDBCBase;// 在类中写入@Test,自动导入org.junit.Test,便不用重复写main函数import org.junit.Test;import java.sql.*;public class Demo3JDBCOperation { // 单行单列查询
@Test
public void testQuerySingleRowAndCol() throws SQLException {
// 1.注册驱动
// 2.获取连接
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection connection = DriverManager.getConnection(url, username, password);
// 3.预编译SQL语句得到PreparedStatement对象
PreparedStatement preparedStatement = connection.prepareStatement("select count(*) as count from t_tmp");
// 4.执行语句 获取结果
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果,进行遍历,如果明确只有一个结果,那么resultSet至少要做一次next的判断,才能拿到我们要的列的结果
while (resultSet.next()) {
// 用下标获取
int anInt = resultSet.getInt(1);
System.out.println(anInt);
}
// 6.释放资源
resultSet.close();
preparedStatement.close();
connection.close();
}
// 单行多列查询
@Test
public void testQuerySingleRowAndCol2() throws SQLException {
// 1.注册驱动
// 2.获取链接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBC", "root", "954926928lcl");
// 3.预编译SQL语句获得PreparedStatement对象
PreparedStatement preparedStatement = connection.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_id = ?");
// 4.为占位符赋值,然后执行,并接受结果
preparedStatement.setInt(1,5);
ResultSet resultSet = preparedStatement.executeQuery();
// 5.处理结果
while (resultSet.next()) {
int empId = resultSet.getInt("emp_id");
String empName = resultSet.getString("emp_name");
double empSalary = resultSet.getDouble("emp_salary");
int empAge = resultSet.getInt("emp_age");
System.out.println(empId+"\t"+empName+"\t"+empSalary+"\t"+empAge);
}
// 6.资源释放
resultSet.close();
preparedStatement.close();
connection.close();
} // 多行多列查询
@Test
public void testQueryMoreRow() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement preparedStatement = conn.prepareStatement("select emp_id,emp_name,emp_salary,emp_age from t_tmp where emp_age > ?");
preparedStatement.setInt(1,25);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int empId = resultSet.getInt("emp_id");
String empName = resultSet.getString("emp_name");
double empSalary = resultSet.getDouble("emp_salary");
int empAge = resultSet.getInt("emp_age");
System.out.println(empId+"\t"+empName+"\t"+empSalary+"\t"+empAge);
}
resultSet.close();
preparedStatement.close();
conn.close();
}
// 新增
@Test
public void testInsert() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement preparedStatement = conn.prepareStatement("insert into t_tmp(emp_name,emp_salary,emp_age) values(?,?,?)");
preparedStatement.setString(1,"rose");
preparedStatement.setDouble(2,345.67);
preparedStatement.setInt(3,36);
int result = preparedStatement.executeUpdate();
// 根据受影响行数做判断,得到成功或失败
if (result>0){
System.out.println("成功!");
}else{
System.out.println("失败!");
}
preparedStatement.close();
conn.close();
} // 修改
@Test
public void testUpdate() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
PreparedStatement preparedStatement = conn.prepareStatement("update t_tmp set emp_salary = ? where emp_id=?");
preparedStatement.setDouble(1,888.88);
preparedStatement.setInt(2,6);
int result = preparedStatement.executeUpdate();
if (result>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
preparedStatement.close();
conn.close();
} // 删除
@Test
public void testDelete() throws SQLException {
String url = "jdbc:mysql://localhost:3306/JDBC";
// 用户名
String username = "root";
// 密码
String password = "954926928lcl";
// 获取链接对象
Connection conn = DriverManager.getConnection(url,username,password);
// 手写SQL语句
PreparedStatement preparedStatement = conn.prepareStatement("delete from t_tmp where emp_id=?");
preparedStatement.setInt(1,5);
int result = preparedStatement.executeUpdate();
if (result>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
preparedStatement.close();
conn.close();
}}
复制代码
五、常见题目
1.资源的管理
2.SQL语句题目
3.SQL语句未设置参数题目
4.用户名或密码错误题目
5.通讯异常
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4