当前位置:网站首页>JDBC 进阶
JDBC 进阶
2022-07-04 03:40:00 【InfoQ】
JDBC 进阶
预编译 PreparedStatement
- 可对 SQL 语句进行预编译,可以灵活地修改 SQL 语句,提高开发效率。
- 把用户输入单引号转义,防止恶意注入,保护数据库安全。
Connection connection = DriverManager.getConnection();
String sql = "INSERT INTO test(id,name) VALUES (?,?)";
PreparedStatement stmt = connection.preparedStatement(sql); // 创建对象并预编译
stmt.setInt(1, 755); // 在第一个占位符(?)位置插入数字
stmt.setString(2, "MrJoker"); // 在第二个占位符(?)位置插入字符串
stmt.executeUpdate(); // 更新并执行Copy to clipboardErrorCopied
批处理 executeBath
Connection connection = DriverManager.getConnection();
String sql = "INSERT INTO test(id,name) VALUES (?,?)";
PreparedStatement stmt = connection.prepareStatement(sql);
for (int i = 1; i <= 1000; i++) {
stmt.setInt(1, i);
stmt.setString(2, (i + "号士兵"));
stmt.addBatch(); // 语句添加到批处理序列中
}
preparedStatement.executeBatch(); // 语句发送给数据库批量处理
preparedStatement.clearBatch(); // 清空批处理序列Copy to clipboardErrorCopied
大文本和二进制数据
- clob 用于存储大文本
- blob用于存储二进制数据
JDBC 示例
// 适用于 JDK 1.8 以后版本
import java.sql.*;
public class MySQLTest{
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB?useSSL=false&serverTimezone=UTC";
static final String USER = "root";
static final String PASS = "123456";
public static void useMethod(){
Connection conn = null;
PreparedStatement stmt = null;
try{
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
stmt = conn.preparedStatement("SELECT id, name, url FROM websites");
ResultSet rs = stmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString("area_id"));
}
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){ // 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){ // 处理 Class.forName 错误
e.printStackTrace();
}finally{
try{
if(stmt != null) stmt.close();
}catch(SQLException se2){}
try{
if(conn != null) conn.close();
}catch(SQLException se){}
}
}
}
边栏推荐
- SQL語句加强練習(MySQL8.0為例)
- [Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
- CSP drawing
- Leecode 122. Zuijia timing of buying and selling stocks ②
- Webhook triggers Jenkins for sonar detection
- system information
- MySQL query
- What are the virtual machine software? What are their respective functions?
- Explain AI accelerator in detail: why is this the golden age of AI accelerator?
- [development team follows] API specification
猜你喜欢

Summary of Chinese remainder theorem

JVM family -- monitoring tools

SQL語句加强練習(MySQL8.0為例)

Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex

EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?

Consul of distributed service registration discovery and unified configuration management
![CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构](/img/ba/c1d40de154344ccc9f2fd1dd4cb12f.png)
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构

Detailed explanation of PPTC self recovery fuse

What is cloud primordial?

This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
随机推荐
Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
Dare to climb here, you're not far from prison, reptile reverse actual combat case
“软硬皆施”,助力建成新型云计算数据中心
Apple submitted the new MAC model to the regulatory database before the spring conference
Handler source code analysis
投资深度思考
Setting methods, usage methods and common usage scenarios of environment variables in postman
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Sword finger offer:55 - I. depth of binary tree
The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
Is online futures account opening safe and reliable? Which domestic futures company is better?
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
Es network layer
Package details_ Four access control characters_ Two details of protected
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
PMP 考試常見工具與技術點總結
warning: LF will be replaced by CRLF in XXXXXX
1day vulnerability pushback skills practice (3)
SQL statement strengthening exercise (MySQL 8.0 as an example)
@Scheduled scheduled tasks