当前位置:网站首页>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){}
}
}
}
边栏推荐
- Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
- Third party login initial version
- Defensive programming skills
- [paddleseg source code reading] normalize operation of paddleseg transform
- [source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
- SQL语句加强练习(MySQL8.0为例)
- Have you entered the workplace since the first 00???
- Slurm view node configuration information
- Detailed explanation of PPTC self recovery fuse
- PMP 考試常見工具與技術點總結
猜你喜欢
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
Summary of Chinese remainder theorem
Webhook triggers Jenkins for sonar detection
Session learning diary 1
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Setting methods, usage methods and common usage scenarios of environment variables in postman
No clue about the data analysis report? After reading this introduction of smartbi, you will understand!
MySQL data query optimization -- data structure of index
用于TCP协议交互的TCPClientDemo
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
随机推荐
“软硬皆施”,助力建成新型云计算数据中心
Have you entered the workplace since the first 00???
logistic regression
Résumé des outils communs et des points techniques de l'examen PMP
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
Reduce function under functools
[PaddleSeg 源码阅读] PaddleSeg计算Dice
MySQL one master multiple slaves + linear replication
Objective C attribute keyword
[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
@Scheduled scheduled tasks
XSS prevention
Detailed explanation of PPTC self recovery fuse
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
[Valentine's Day confession code] - Valentine's Day is approaching, and more than 10 romantic love effects are given to the one you love
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
Leetcode51.n queen
Session learning diary 1
[paddleseg source code reading] paddleseg custom data class
Jenkins configures IP address access