当前位置:网站首页>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命令
边栏推荐
- 基于微信电子书阅读小程序毕业设计毕设作品(5)任务书
- META-INF、WEB-INF分别是什么?
- Global and Chinese smart government industry market research and investment risk outlook report 2022-2028
- Learning summary in March
- A small case with 666 times performance improvement illustrates the importance of using indexes correctly in tidb
- ISCC2022
- Istio 1.14 发布
- Is it really hopeless to choose electronic engineering and be discouraged?
- Wechat e-book reading applet graduation design works (1) development outline
- Dacom G150双模耳机,为爱发声,呵护孩子听力健康成长
猜你喜欢
![[blockbuster release] ant dynamic card, enabling the app home page to realize agile update](/img/a6/62caef27b917bbb4c5529de46bc7f9.jpg)
[blockbuster release] ant dynamic card, enabling the app home page to realize agile update

WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法

What did 3GPP ran do in the first F2F meeting?

基于微信电子书阅读小程序毕业设计毕设作品(2)小程序功能

负数取余问题

Uniapp uses the Ali Icon

Shell 编程正则表达式及元字符

基于微信电子书阅读小程序毕业设计毕设作品(1)开发概要

Shell arrays and functions

基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
随机推荐
运算器的基本结构
负数取余问题
“即服务”,未来已来,始于现在 | IT消费新模式,FOD按需计费
Shell 数组和函数
leetcodeSQL:602. Friend application II: who has the most friends
软件确认测试知识分享,上海第三方软件测试机构有哪些?
Super heavy! Apache Hudi multimode index optimizes queries up to 30 times
Mysql database (28): Variables
Jenkins中pipeline对接CMDB接口获取主机列表的发布实践原创
Equipment management - borrowing and returning module 1
review. JS ppt math formula cannot be displayed
Embedded development: 6 necessary skills for firmware engineers
Is it really hopeless to choose electronic engineering and be discouraged?
The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved
Hardware test - why not use grounding clip for ripple test
Fault analysis | a case of MySQL remote slave database replication delay
Download and configuration of nuitka packaging tutorial
Istio 1.14 release
Shell programming regular expressions and metacharacters
Equipment management - borrowing / returning module interface code