当前位置:网站首页>JDBC接口总结
JDBC接口总结
2022-06-12 19:31:00 【嘉然一天饿三顿】
JDBC接口总结
什么是JDBC?
JDBC(Java DataBase Connectivity) 称为Java数据库连接,它是一种用于数据库访问的应用程序API,由一组用Java语言编写的类和接口组成,有了JDBC就可以用同一的语法对多种关系数据库进行访问,而不用担心其数据库操作语言的差异。 有了JDBC,就不必为访问Mysql数据库专门写一个程序,为访问Oracle又专门写一个程序等等。
简单说,就是一套标准化接口。
创建JDBC应用程序的步骤
- 注册驱动
- 获取连接
- 获取数据库操作对象
- 执行SQL语句
- 处理查询结果集(DQL)
- 关闭连接
public class JDBCTest {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
ResourceBundle rb = ResourceBundle.getBundle("jdbc");
String driver = rb.getString("driver");
String url = rb.getString("url");
String user = rb.getString("user");
String password = rb.getString("password");
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
rs = stmt.executeQuery("select empno,ename,sal from emp");
while(rs.next()){
int empno = rs.getInt("empno");
String ename = rs.getString("ename");
double sal = rs.getDouble("sal");
System.out.println(empno + "," + ename + "," + (sal + 200));
}
} catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null){
try{
rs.close();
} catch (Exception e){
e.printStackTrace();
}
}
if(stmt != null){
try{
stmt.close();
} catch (Exception e){
e.printStackTrace();
}
}
if(conn != null){
try{
conn.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
}
}
JDBC控制事务
1. 事务:一个包含多个步骤的业务操作。如果这个业务操作被事务管理,则这多个步骤要么同时成功,要么同时失败。
- 操作:
1. 开启事务
2. 提交事务
3. 回滚事务
- 使用Connection对象来管理事务
* 开启事务:setAutoCommit(boolean autoCommit) :调用该方法设置参数为false,即开启事务
* 在执行sql之前开启事务
* 提交事务:commit()
* 当所有sql都执行完提交事务
* 回滚事务:rollback()
* 在catch中回滚事务
public class JDBCDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
//1.获取连接
conn = JDBCUtils.getConnection();
//开启事务
conn.setAutoCommit(false);
//2.定义sql
//2.1 张三 - 500
String sql1 = "update account set balance = balance - ? where id = ?";
//2.2 李四 + 500
String sql2 = "update account set balance = balance + ? where id = ?";
//3.获取执行sql对象
pstmt1 = conn.prepareStatement(sql1);
pstmt2 = conn.prepareStatement(sql2);
//4. 设置参数
pstmt1.setDouble(1,500);
pstmt1.setInt(2,1);
pstmt2.setDouble(1,500);
pstmt2.setInt(2,2);
//5.执行sql
pstmt1.executeUpdate();
// 手动制造异常
int i = 3/0;
pstmt2.executeUpdate();
//提交事务
conn.commit();
} catch (Exception e) {
//事务回滚
try {
if(conn != null) {
conn.rollback();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}
SQL注入
JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}
### SQL注入
在输入账号和密码的时候,在末尾加上 "or" 再接上任何为真的语句,这样一来,有真就为真,这样也能登录成功。这种叫 sql注入,是欺骗服务器执行恶意的SQL命令
边栏推荐
- Méthode de sauvegarde programmée basée sur la base de données distribuée elle - même
- "As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand
- 基于微信电子书阅读小程序毕业设计毕设作品(5)任务书
- A journey of database full SQL analysis and audit system performance optimization
- 合理地配置线程池
- How do I create my own appender in log4j- How to create my own Appender in log4j?
- 【5GC】三种SSC(Session and Service Continuity)模式介绍
- In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
- io. seata. common. exception. FrameworkException: can not connect to services-server.
- 基于微信电子书阅读小程序毕业设计毕设作品(2)小程序功能
猜你喜欢

Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022

IO流基础知识详解--文件及IO流原理

What is data driven

Méthode de sauvegarde programmée basée sur la base de données distribuée elle - même

今晚7:00 | PhD Debate 自监督学习在推荐系统中的应用

leetcodeSQL:578. Questions with the highest response rate

负数取余问题

YOLOX网络结构详解
![[observation] Huawei's next generation data center](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] Huawei's next generation data center "adds momentum" to Guangxi's low-carbon and high-quality development

Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
随机推荐
王学岗room+paging3
【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解
unity websockt一些知识:
原生Servlet - 文件的Upload&Download
Detailed explanation of IO flow basic knowledge -- file and IO flow principle
【5GC】三种SSC(Session and Service Continuity)模式介绍
vc hacon 联合编程 GenImage3Extern WriteImage
【图像去噪】基于正则化实现图像去噪附matlab代码
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
Download and configuration of nuitka packaging tutorial
Module 8 fonctionnement
设备管理-借还模块1
WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法
Cookie & Session & kaptcha验证码
Report on market demand trends and future strategic planning recommendations of the global and Chinese smart financial solutions industry 2022-2028
Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN
选电子工程被劝退,真的没前景了?
运算器的基本结构
Original introduction to Jenkins' configuration options
什么是数据驱动