当前位置:网站首页>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){}
}
}
}
边栏推荐
- Redis notes (I) Linux installation process of redis
- pytest多进程/多线程执行测试用例
- Database SQL statement summary, continuous update
- Tcpclientdemo for TCP protocol interaction
- MySQL one master multiple slaves + linear replication
- [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
- The difference between MCU serial communication and parallel communication and the understanding of UART
- @Scheduled scheduled tasks
- system information
- Webhook triggers Jenkins for sonar detection
猜你喜欢

Session learning diary 1

Sales management system of lightweight enterprises based on PHP

Wechat official account web page authorization

SQL injection (1) -- determine whether there are SQL injection vulnerabilities

微信公众号网页授权

Leetcode51.n queen

Objective-C description method and type method

PHP database connection succeeded, but data cannot be inserted

Select sorting and bubble sorting template

Mitsubishi M70 macro variable reading Mitsubishi M80 public variable acquisition Mitsubishi CNC variable reading acquisition Mitsubishi CNC remote tool compensation Mitsubishi machine tool online tool
随机推荐
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
2022 registration examination for safety production management personnel of fireworks and firecracker production units and examination skills for safety production management personnel of fireworks an
SQL statement strengthening exercise (MySQL 8.0 as an example)
[PaddleSeg 源码阅读] PaddleSeg计算Dice
Pytest multi process / multi thread execution test case
选择排序与冒泡排序模板
智慧地铁| 云计算为城市地铁交通注入智慧
Leecode 122. Zuijia timing of buying and selling stocks ②
pytest多进程/多线程执行测试用例
图解网络:什么是热备份路由器协议HSRP?
MySQL query
Have you entered the workplace since the first 00???
函数计算异步任务能力介绍 - 任务触发去重
MySQL data query optimization -- data structure of index
Why is it recommended that technologists write blogs?
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
What is cloud primordial?
JSON string conversion in unity
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
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush