当前位置:网站首页>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命令
边栏推荐
- New product launch
- Shell 数组和函数
- IO流基础知识详解--文件及IO流原理
- Yoloe target detection notes
- Shell arrays and functions
- 基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
- [blockbuster release] ant dynamic card, enabling the app home page to realize agile update
- 硬件测试之—纹波测试为什么不要使用接地夹子
- Mode of most elements (map, sort, random, Boyer Moore voting method)
- BannerViewPager
猜你喜欢

How does Eldon's ring of the law get lune quickly? Introduction to the fastest and safest method for obtaining lune

leetcodeSQL:578. Questions with the highest response rate

【生成对抗网络学习 其三】BiGAN论文阅读笔记及其原理理解

What is data driven

Implementation of VGA protocol based on FPGA

Php+flash large file breakpoint continuation function sharing

基于分布式数据库本身的定时备份方法

负数取余问题

Wincc7.5 SP1 method for adjusting picture size to display resolution

First build green, then build city
随机推荐
RT thread simulator builds lvgl development and debugging environment
unity websockt一些知识:
Download and configuration of nuitka packaging tutorial
VC Hacon Joint Programming genimage3extern writeimage
王学岗room+paging3
Basic structure of arithmetic unit
Cookie & Session & kaptcha驗證碼
review.js ppt数学公式无法显示
Programming tool download address
Méthode de sauvegarde programmée basée sur la base de données distribuée elle - même
存储体系概述
WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法
基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
[matrix theory & graph theory] final exam review mind map
3GPP RAN第一次F2F会议,都干了些啥?
Wechat e-book reading applet graduation design works (1) development outline
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update
In 2021, the global revenue of electro-optical modulator (EOM) is about USD 360.3 million, and it is expected to reach USD 704.4 million in 2028
Research Report on current market situation and investment prospect of China's tobacco RFID industry 2022-2027
模塊八作業