当前位置:网站首页>JDBC interface summary
JDBC interface summary
2022-06-12 19:40:00 【Jiaran is hungry three times a day】
JDBC Interface summary
What is? JDBC?
JDBC(Java DataBase Connectivity) be called Java Database connection , It is an application for database access API, By a group Java Language written classes and interfaces , With JDBC You can use the same syntax to access multiple relational databases , Don't worry about the difference of its database operation language . With JDBC, There's no need to visit Mysql The database specially writes a program , For the visit Oracle Another program is specially written, and so on .
In short , Is a set of standardized interfaces .
establish JDBC Application steps
- Registration drive
- Get the connection
- Get database operation object
- perform SQL sentence
- Process query result set (DQL)
- Close the connection
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 Control the transaction
1. Business : A business operation with multiple steps . If the business operation is managed by a transaction , Then these multiple steps will either succeed at the same time , Or fail at the same time .
- operation :
1. Open transaction
2. Commit transaction
3. Roll back the transaction
- Use Connection Object to manage affairs
* Open transaction :setAutoCommit(boolean autoCommit) : Call this method to set the parameter to false, That is, start the transaction
* In execution sql Before opening the transaction
* Commit transaction :commit()
* When all sql All commit transactions are completed
* Roll back the transaction :rollback()
* stay catch Rollback transaction
public class JDBCDemo {
public static void main(String[] args) {
Connection conn = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
try {
//1. Get the connection
conn = JDBCUtils.getConnection();
// Open transaction
conn.setAutoCommit(false);
//2. Definition sql
//2.1 Zhang San - 500
String sql1 = "update account set balance = balance - ? where id = ?";
//2.2 Li Si + 500
String sql2 = "update account set balance = balance + ? where id = ?";
//3. Access to perform sql object
pstmt1 = conn.prepareStatement(sql1);
pstmt2 = conn.prepareStatement(sql2);
//4. Set parameters
pstmt1.setDouble(1,500);
pstmt1.setInt(2,1);
pstmt2.setDouble(1,500);
pstmt2.setInt(2,2);
//5. perform sql
pstmt1.executeUpdate();
// Manual manufacturing abnormality
int i = 3/0;
pstmt2.executeUpdate();
// Commit transaction
conn.commit();
} catch (Exception e) {
// Transaction rollback
try {
if(conn != null) {
conn.rollback();
}
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}
SQL Inject
JDBCUtils.close(pstmt1,conn);
JDBCUtils.close(pstmt2,null);
}
}
}
### SQL Inject
When entering the account and password , Add at the end "or" And any statements that are true , thus , If it's true, it's true , In this way, you can log in successfully . This is called sql Inject , It is deceiving the server to execute malicious SQL command
边栏推荐
- Module 8 fonctionnement
- ThansmittableThreadLocal详解
- Ctfshow-web266 (deserialization)
- 设备管理-借还模块1
- Leetcode topic [string]-344- reverse string
- 存储体系概述
- Axure RP 9 for Mac(交互式产品原型设计工具)中文版
- [blockbuster release] ant dynamic card, enabling the app home page to realize agile update
- 基于微信电子书阅读小程序毕业设计毕设作品(1)开发概要
- Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN
猜你喜欢

exec函数、shell的实现

Demand and business model innovation - demand 3- demand engineering process

Shell arrays and functions
![[image denoising] image denoising based on anisotropic filtering with matlab code](/img/3d/ee2e36b15b5db2502e43f6945685c5.png)
[image denoising] image denoising based on anisotropic filtering with matlab code

Equipment management - borrowing and returning module 1

Basic structure of arithmetic unit

Demand and business model innovation - demand 4- overview of demand acquisition

"As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand

什么是数据驱动

API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters
随机推荐
What did 3GPP ran do in the first F2F meeting?
Attack and defense world (WEB) -- supersqli
Jenkins各配置选项介绍原创
Leetcodesql: count the number of students in each major
WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法
RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
2022年最新宁夏建筑安全员模拟题库及答案
7:00 tonight | application of PhD debate self supervised learning in Recommendation System
今晚7:00 | PhD Debate 自监督学习在推荐系统中的应用
Pyinstaller packaging tutorial packaging resource files
基于微信电子书阅读小程序毕业设计毕设作品(5)任务书
从16页PPT里看懂Jack Dorsey的Web5
Module 8 fonctionnement
The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved
[SQL] MySQL query statement execution sequence analysis
Implementation of exec function and shell
Wincc7.5 SP1 method for adjusting picture size to display resolution
New product launch
Demand and business model analysis-2-business model types
Negative remainder problem