当前位置:网站首页>JDBC技术(一)——一个简单的JDBC测试
JDBC技术(一)——一个简单的JDBC测试
2022-08-09 01:30:00 【--流星。】
什么是JDBC
JDBC 指 Java 数据库连接,是一种标准Java应用编程接口( JAVA API),用来连接 Java 编程语言和广泛的数据库。
从根本上来说,JDBC 是一种规范,它提供了一套完整的接口,允许便携式访问到底层数据库,因此可以用 Java 编写不同类型的可执行文件
JDBC架构
JDBC 的 API 支持两层和三层处理模式进行数据库访问,但一般的 JDBC 架构由两层处理模式组成:
- JDBC API: 使用驱动程序管理器和数据库特定的驱动程序来提供异构(heterogeneous)数据库的透明连接。
- JDBC Driver API: JDBC 驱动程序管理器可确保正确的驱动程序来访问每个数据源。该驱动程序管理器能够支持连接到多个异构数据库的多个并发的驱动程序。
常用的JDBC组件
- DriverManager :这个类管理一系列数据库驱动程序。匹配连接使用通信子协议从 JAVA 应用程序中请求合适的数据库驱动程序。识别 JDBC 下某个子协议的第一驱动程序将被用于建立数据库连接。
- Driver : 这个接口处理与数据库服务器的通信。你将很少直接与驱动程序互动。相反,你使用 DriverManager 中的对象,它管理此类型的对象。它也抽象与驱动程序对象工作相关的详细信息。
- Connection : 此接口具有接触数据库的所有方法。该连接对象表示通信上下文,即,所有与数据库的通信仅通过这个连接对象进行。
- Statement : 使用创建于这个接口的对象将 SQL 语句提交到数据库。除了执行存储过程以外,一些派生的接口也接受参数。
- ResultSet : 在你使用语句对象执行 SQL 查询后,这些对象保存从数据获得的数据。它作为一个迭代器,让您可以通过它的数据来移动。
- SQLException : 这个类处理发生在数据库应用程序的任何错误。
前期准备
新建java项目,创建lib目录并导入mysql驱动
项目结构

jdbc的编写流程
- 导入mysql驱动,注册驱动
- 创建与数据库之间的连接(tcp)
- 封装sql并传输给mysql
- 数据库响应并处理
- 释放资源关闭连接
jdbc的代码编写
实体类
public class Emp1 {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private String hiredate;
private BigDecimal sal;
private BigDecimal comm;
private Integer deptno;
public Emp1() {
}
public Emp1(Integer empno, String ename, String job, Integer mgr, String hiredate, BigDecimal sal, BigDecimal comm, Integer deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + hiredate +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public Integer getEmpno() {
return empno;
}
public void setEmpno(Integer empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Integer getMgr() {
return mgr;
}
public void setMgr(Integer mgr) {
this.mgr = mgr;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public BigDecimal getSal() {
return sal;
}
public void setSal(BigDecimal sal) {
this.sal = sal;
}
public BigDecimal getComm() {
return comm;
}
public void setComm(BigDecimal comm) {
this.comm = comm;
}
}
工具类
package com.lx.jdbc01.util;
import com.lx.jdbc01.entity.Emp1;
import java.sql.*;
/** * description TODO * * @author 流星 * @date 2022/8/4 9:49 */
public class JDBCUtil1 {
private static Connection getConnection() {
Connection connection;
try {
// 1.注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2.建立连接
connection = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test?useSSL=true&serverTimezone=GMT%2B8",
"root",
"123456"
);
} catch (ClassNotFoundException | SQLException e) {
throw new RuntimeException("创建连接失败!!!");
}
return connection;
}
public static void query(String ename) throws SQLException {
Connection connection = getConnection();
/** * 3.封装sql并传给mysql处理 * 用PreparedStatement而不用Statement是为了避免sql注入的问题,PreparedStatement对sql和参数进行了预处理 * sql注入问题:比如传ename的值为 xx or 1 = 1,无论xx是什么都能查出所有的信息 */
String sql = "select * from emp where ename = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, ename);
ResultSet resultSet = preparedStatement.executeQuery();
// 4.数据库响应并处理响应
while (resultSet.next()) {
int empno = resultSet.getInt("empno");
String ename1 = resultSet.getNString("ename");
System.out.println(empno + "***" + ename1);
}
// 5.关闭连接并释放资源
close(connection, preparedStatement, resultSet);
}
private static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void insert(Emp1 emp1) throws SQLException {
Connection connection = getConnection();
String sql = "insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) values(?,?,?,?,?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, emp1.getEmpno());
preparedStatement.setString(2, emp1.getEname());
preparedStatement.setString(3, emp1.getJob());
preparedStatement.setInt(4, emp1.getMgr());
preparedStatement.setString(5, emp1.getHiredate());
preparedStatement.setBigDecimal(6, emp1.getSal());
preparedStatement.setBigDecimal(7, emp1.getComm());
preparedStatement.setInt(8, emp1.getDeptno());
int count = preparedStatement.executeUpdate();
if (count == 1) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
}
close(connection, preparedStatement, null);
}
public static void delete(Integer empno) throws SQLException {
Connection connection = getConnection();
String sql = "delete from emp where empno = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, empno);
int count = preparedStatement.executeUpdate();
if (count == 1) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
close(connection, preparedStatement, null);
}
public static void update(Integer empno, String ename) throws SQLException {
Connection connection = getConnection();
String sql = "update emp set ename = ? where empno = ? ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, ename);
preparedStatement.setInt(2, empno);
int count = preparedStatement.executeUpdate();
if (count == 1) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
close(connection, preparedStatement, null);
}
}
测试类
package com.lx.jdbc01.test;
import com.lx.jdbc01.entity.Emp1;
import com.lx.jdbc01.util.JDBCUtil1;
import org.junit.Test;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** * description TODO * * @author 流星 * @date 2022/8/4 9:50 */
public class MainTest1 {
@Test
public void query() throws SQLException {
String ename="LI";
JDBCUtil1.query(ename);
}
@Test
public void insert() throws SQLException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date createTime = new Date();
JDBCUtil1.insert(new Emp1(6666,"YUN","CLERK",7698,simpleDateFormat.format(createTime),new BigDecimal(1500),new BigDecimal(200),20));
}
@Test
public void update() throws SQLException {
JDBCUtil1.update(6666,"YING");
}
@Test
public void delete() throws SQLException {
JDBCUtil1.delete(6666);
}
}
测试结果

附:参考资料
边栏推荐
- 33. 分别谈谈联合索引生效和失效的条件
- 使用百度EasyDL实现智能垃圾箱
- 4-11 Matplotlib 配置
- 轻量化网络ChannelNet--channel-wize Conv在channel维度卷积
- 德语翻译-德语在线批量翻译软件
- 425 Can‘t open data connection for transfer of “/“
- Use Ehcache distributed cache to easily create commercial-grade high-concurrency, high-performance API interfaces!
- 低代码开发创新企业应用构建模式
- Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
- LeetCode每日一题:搜索插入位置 (均1200道)方法:二分查找
猜你喜欢

Transformer前言:self-attention与Multi-Headed Attention的详细理解

4-7 Matplotlib库 箱线图

轻量级学习网络--ShuffleNet v1:Group conv的改进与channel shuffle的提出

在特征通道提升网络性能 --SENet网络详解

2022PMP项目管理认证考试报考指南(1)

LeetCode每日两题01:二分查找 (均1200道)

网络安全基础-基本dos命令(一)

走向合规化的虚拟人直播

PMP有什么答题技巧?

【物理应用】基于El-centro地震波作用下隔震与非隔震支座下的顶层位移、速度、加速度的对比情况附matlab代码
随机推荐
保护您的 Web 应用程序的最佳开源 Web 应用程序防火墙
设计师设计相关图表时,如何运用设计技巧与合理的用户体验?【大屏可视化(PC端、移动端)】
【.NET 6】开发minimal api以及依赖注入的实现和代码演示
如何在EasyDSS中使用ffmpeg实现点播视频的拼接与合成?
5-4 Seaborn 线性回归绘图
4-4 Matplotlib库 直方图
PostMan导入证书 添加证书
A double non-programmer interviewed Ant, Meituan, Ctrip and other big companies with offers to share the interview process
《LC刷题总结》—— 二叉树
椭圆曲线复习
String compression
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
webrtc 编译
Introduction to LVGL (based on v8.1-8.2)
如何准备一份简历
The Best Open Source Web Application Firewall to Protect Your Web Applications
等到中心化的平台不再,衍生于这个平台的一切都将化作泡影
Network In Network学习记录
容器运维平台的故障处理-1
数据恢复软件EasyRecovery支持恢复所有类型的文件
