当前位置:网站首页>JDBC advanced
JDBC advanced
2022-07-04 03:47:00 【InfoQ】
JDBC Advanced
precompile PreparedStatement
- But for SQL Statement precompiling , It can be modified flexibly SQL sentence , Improve development efficiency .
- Escape the single quotation mark entered by the user , Prevent malicious Injection , Protect database security .
Connection connection = DriverManager.getConnection();
String sql = "INSERT INTO test(id,name) VALUES (?,?)";
PreparedStatement stmt = connection.preparedStatement(sql); // Create objects and precompile
stmt.setInt(1, 755); // In the first placeholder (?) Insert number in position
stmt.setString(2, "MrJoker"); // In the second placeholder (?) Position insert string
stmt.executeUpdate(); // Update and execute Copy to clipboardErrorCopied
The batch 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 + " Soldier No "));
stmt.addBatch(); // Statement is added to the batch sequence
}
preparedStatement.executeBatch(); // Statement is sent to the database for batch processing
preparedStatement.clearBatch(); // Empty batch sequence Copy to clipboardErrorCopied
Large text and binary data
- clob For storing large text
- blob Used to store binary data
JDBC Example
// Apply to JDK 1.8 Later versions
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){ // Handle JDBC error
se.printStackTrace();
}catch(Exception e){ // Handle Class.forName error
e.printStackTrace();
}finally{
try{
if(stmt != null) stmt.close();
}catch(SQLException se2){}
try{
if(conn != null) conn.close();
}catch(SQLException se){}
}
}
}
边栏推荐
- [latex] production of complex tables: excel2latex and detail adjustment
- 函数计算异步任务能力介绍 - 任务触发去重
- functools下的reduce函数
- pytest多进程/多线程执行测试用例
- 2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
- Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
- Objective C attribute keyword
- How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
- Katalon框架测试web(二十六)自动发邮件
- JDBC 进阶
猜你喜欢
Mindmanager2022 efficient and easy to use office mind map MindManager
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
Go 语言入门很简单:Go 实现凯撒密码
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?
Jenkins configures IP address access
Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
MySQL data query optimization -- data structure of index
JSON string conversion in unity
MySQL one master multiple slaves + linear replication
随机推荐
[.NET + mqtt]. Mise en œuvre de la communication mqtt dans l'environnement net 6 et démonstration de code pour l'abonnement et la publication de messages bilatéraux du serveur et du client
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
Formulaire day05
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
Solve the problems encountered by the laravel framework using mongodb
数据库SQL语句汇总,持续更新......
MySQL backup notes
疫情来袭--远程办公之思考|社区征文
Future源碼一觀-JUC系列
支持首次触发的 Go Ticker
SQL statement strengthening exercise (MySQL 8.0 as an example)
毕业总结
Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量
Why is it recommended that technologists write blogs?
[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
pytest多进程/多线程执行测试用例
Slurm view node configuration information
[paddleseg source code reading] normalize operation of paddleseg transform