当前位置:网站首页>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){}
}
}
}
边栏推荐
- ctf-pikachu-CSRF
- MySQL one master multiple slaves + linear replication
- Package details_ Four access control characters_ Two details of protected
- SQL statement strengthening exercise (MySQL 8.0 as an example)
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
- JVM family -- monitoring tools
- PHP database connection succeeded, but data cannot be inserted
- Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
- Zigzag scan
- MySQL is dirty
猜你喜欢

Leetcode51.n queen

What is cloud primordial?

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

用于TCP协议交互的TCPClientDemo

The difference between MCU serial communication and parallel communication and the understanding of UART

@Scheduled scheduled tasks

Detailed explanation of PPTC self recovery fuse

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

三菱M70宏变量读取三菱M80公共变量采集三菱CNC变量读取采集三菱CNC远程刀补三菱机床在线刀补三菱数控在线测量

Reduce function under functools
随机推荐
Recursive structure
Leetcode51.n queen
vue多级路由嵌套怎么动态缓存组件
[paddleseg source code reading] paddleseg calculation dice
Want to do something in production? Then try these redis commands
AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
Handler source code analysis
Consul of distributed service registration discovery and unified configuration management
Pytest multi process / multi thread execution test case
Add token validation in swagger
Package details_ Four access control characters_ Two details of protected
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions
Objective-C member variable permissions
What is cloud primordial?
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
Apple submitted the new MAC model to the regulatory database before the spring conference
CSP drawing
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
Database SQL statement summary, continuous update
1day vulnerability pushback skills practice (3)