当前位置:网站首页>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){}
}
}
}
边栏推荐
- system information
- 渗透实战-SQLServer提权
- MySQL data query optimization -- data structure of index
- 【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
- Mindmanager2022 efficient and easy to use office mind map MindManager
- 1289_FreeRTOS中vTaskSuspend()接口实现分析
- SQL statement strengthening exercise (MySQL 8.0 as an example)
- MySQL maxscale realizes read-write separation
- The difference between MCU serial communication and parallel communication and the understanding of UART
- Solve the problems encountered by the laravel framework using mongodb
猜你喜欢
![[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)](/img/e9/818bdfeae766dca7d2318b52b4424d.jpg)
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)

Session learning diary 1

Webhook triggers Jenkins for sonar detection

What kind of experience is it when the Institute earns 20000 yuan a month!

JSON string conversion in unity

Have you entered the workplace since the first 00???

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

New year's first race, submit bug reward more!

Package details_ Four access control characters_ Two details of protected
![[latex] production of complex tables: excel2latex and detail adjustment](/img/39/0d448ddf006eda262de3ed75666354.jpg)
[latex] production of complex tables: excel2latex and detail adjustment
随机推荐
Apple submitted the new MAC model to the regulatory database before the spring conference
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
JDBC 进阶
Value transfer communication between components (parent to child, child to parent, brother component to value)
Deep thinking on investment
Monitoring - Prometheus introduction
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?
Class summation, shortest row
[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
How to use STR function of C language
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
[PaddleSeg 源码阅读] PaddleSeg计算Dice
MySQL is dirty
Which product is better if you want to go abroad to insure Xinguan?
Want to do something in production? Then try these redis commands
Wechat official account web page authorization
Package details_ Four access control characters_ Two details of protected
Objective-C string class, array class
Setting methods, usage methods and common usage scenarios of environment variables in postman