当前位置:网站首页>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){}
}
}
}
边栏推荐
- Tsinghua University product: penalty gradient norm improves generalization of deep learning model
- Teach you how to optimize SQL
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
- 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?
- Audio and video technology development weekly | 232
- [paddleseg source code reading] paddleseg custom data class
- What is the difference between enterprise wechat applet and wechat applet
- The difference between MCU serial communication and parallel communication and the understanding of UART
- Monitoring - Prometheus introduction
- Es network layer
猜你喜欢
![[paddleseg source code reading] paddleseg custom data class](/img/88/37c535b371486db545abc392a685af.png)
[paddleseg source code reading] paddleseg custom data class

Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches

SQL injection (1) -- determine whether there are SQL injection vulnerabilities

渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
![[PaddleSeg 源码阅读] PaddleSeg 自定义数据类](/img/88/37c535b371486db545abc392a685af.png)
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类

Es network layer

This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t

PID of sunflower classic

MySQL is dirty
![[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush](/img/98/3e5f1094141e34d7e77f908e12acda.jpg)
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
随机推荐
2022 examination summary of quality controller - Equipment direction - general basis (quality controller) and examination questions and analysis of quality controller - Equipment direction - general b
[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
Session learning diary 1
[UE4] parse JSON string
MySQL data query optimization -- data structure of index
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
Aperçu du code source futur - série juc
深入浅出对话系统——使用Transformer进行文本分类
[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
Objective-C string class, array class
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Object oriented -- encapsulation, inheritance, polymorphism
Have you entered the workplace since the first 00???
[PaddleSeg 源码阅读] PaddleSeg计算Dice
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
System integration meets the three business needs of enterprises
[paddleseg source code reading] paddleseg calculates Miou
Management and thesis of job management system based on SSM
Simple dialogue system -- text classification using transformer
Is online futures account opening safe and reliable? Which domestic futures company is better?