java使用mysql預(yù)編譯語(yǔ)句查詢優(yōu)勢(shì)及示例詳解
預(yù)編譯語(yǔ)句
預(yù)編譯語(yǔ)句是一種用于執(zhí)行參數(shù)化SQL查詢的技術(shù),它可以提高性能并減少SQL注入的風(fēng)險(xiǎn)。預(yù)編譯語(yǔ)句主要有以下優(yōu)勢(shì):
- 避免SQL注入攻擊。
- 提高性能,因?yàn)轭A(yù)編譯語(yǔ)句只編譯一次,然后可以多次執(zhí)行。
在Java中,使用java.sql.PreparedStatement接口實(shí)現(xiàn)預(yù)編譯語(yǔ)句。以下是幾個(gè)示例,展示了如何使用預(yù)編譯語(yǔ)句進(jìn)行各種數(shù)據(jù)庫(kù)操作。
插入數(shù)據(jù)
以下示例展示了如何使用預(yù)編譯語(yǔ)句插入數(shù)據(jù):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class PreparedStatementInsertExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
String username = "root";
String password = "mypassword";
Connection connection = DriverManager.getConnection(url, username, password);
String sql = "INSERT INTO users (name, age) VALUES (?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "User 7");
preparedStatement.setInt(2, 30);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}查詢數(shù)據(jù)
以下示例展示了如何使用預(yù)編譯語(yǔ)句查詢數(shù)據(jù):
import java.sql.*;
public class PreparedStatementSelectExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
String username = "root";
String password = "mypassword";
Connection connection = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM users WHERE age > ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 30);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println("ID: " + resultSet.getInt("id") + ", Name: " + resultSet.getString("name") + ", Age: " + resultSet.getInt("age"));
}
resultSet.close();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}更新數(shù)據(jù)
以下示例展示了如何使用預(yù)編譯語(yǔ)句更新數(shù)據(jù):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class PreparedStatementUpdateExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
String username = "root";
String password = "mypassword";
Connection connection = DriverManager.getConnection(url, username, password);
String sql = "UPDATE users SET age = ? WHERE name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 31);
preparedStatement.setString(2, "User 7");
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}刪除數(shù)據(jù)
以下示例展示了如何使用預(yù)編譯語(yǔ)句刪除數(shù)據(jù):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class PreparedStatementDeleteExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true";
String username = "root";
String password = "mypassword";
Connection connection = DriverManager.getConnection(url, username, password);
String sql = "DELETE FROMusers WHERE age > ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 60);
preparedStatement.executeUpdate();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}通過(guò)這些示例,你應(yīng)該對(duì)如何使用預(yù)編譯語(yǔ)句有了更清晰的了解。預(yù)編譯語(yǔ)句使得你能夠在查詢中使用參數(shù),提高了性能并減少了SQL注入的風(fēng)險(xiǎn)。在實(shí)際項(xiàng)目中,盡量使用預(yù)編譯語(yǔ)句來(lái)執(zhí)行SQL查詢。
更多關(guān)于java mysql預(yù)編譯查詢的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用MyBatis查詢千萬(wàn)級(jí)數(shù)據(jù)量操作實(shí)現(xiàn)
這篇文章主要為大家介紹了如何使用MyBatis?查詢千萬(wàn)數(shù)據(jù)量的操作過(guò)程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Java實(shí)現(xiàn)按比抽獎(jiǎng)功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)按比抽獎(jiǎng)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
通過(guò)FeignClient調(diào)用微服務(wù)提供的分頁(yè)對(duì)象IPage報(bào)錯(cuò)的解決
這篇文章主要介紹了通過(guò)FeignClient調(diào)用微服務(wù)提供的分頁(yè)對(duì)象IPage報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java線程池的幾種實(shí)現(xiàn)方法和區(qū)別介紹實(shí)例詳解
本篇文章主要介紹了Java線程池的幾種實(shí)現(xiàn)方法和區(qū)別,需要的朋友可以參考2017-04-04
Java實(shí)現(xiàn)本地緩存的四種方法實(shí)現(xiàn)與對(duì)比
本地緩存的優(yōu)點(diǎn)就是速度非常快,沒(méi)有網(wǎng)絡(luò)消耗,本地緩存比如 caffine,guava cache 這些都是比較常用的,下面我們來(lái)看看這四種緩存的具體實(shí)現(xiàn)吧2025-08-08
Shiro與Springboot整合開(kāi)發(fā)的基本步驟過(guò)程詳解
這篇文章主要介紹了Shiro與Springboot整合開(kāi)發(fā)的基本步驟,本文結(jié)合實(shí)例代碼給大家介紹整合過(guò)程,感興趣的朋友跟隨小編一起看看吧2023-06-06

