当前位置:网站首页>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){}
}
}
}
边栏推荐
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- Reduce function under functools
- ctf-pikachu-CSRF
- [database I] database overview, common commands, view the table structure of 'demo data', simple query, condition query, sorting data, data processing function (single row processing function), groupi
- Es network layer
- 数据库SQL语句汇总,持续更新......
- JVM family -- monitoring tools
- 2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
- Setting methods, usage methods and common usage scenarios of environment variables in postman
- SQL injection (1) -- determine whether there are SQL injection vulnerabilities
猜你喜欢

2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。

Teach you how to optimize SQL
![[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

PID of sunflower classic

Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?

Audio and video technology development weekly | 232

选择排序与冒泡排序模板

Webhook triggers Jenkins for sonar detection

pytest多进程/多线程执行测试用例

Practical multifunctional toolbox wechat applet source code / support traffic master
随机推荐
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
微信公众号网页授权
Database SQL statement summary, continuous update
Is online futures account opening safe and reliable? Which domestic futures company is better?
Detailed explanation of PPTC self recovery fuse
Objective-C description method and type method
The difference between MCU serial communication and parallel communication and the understanding of UART
Mindmanager2022 efficient and easy to use office mind map MindManager
Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
Development of digital collection trading platform development of digital collection platform
ctf-pikachu-CSRF
If you have just joined a new company, don't be fired because of your mistakes
MySQL query
National standard gb28181 protocol platform easygbs fails to start after replacing MySQL database. How to deal with it?
Imperial cms7.5 imitation "D9 download station" software application download website source code
Aperçu du code source futur - série juc
数据库SQL语句汇总,持续更新......
System integration meets the three business needs of enterprises
Formulaire day05
Jenkins continuous integration environment construction V (Jenkins common construction triggers)