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源碼解析——獲取SqlSessionFactory方式
這篇文章主要介紹了MyBatis源碼解析——獲取SqlSessionFactory方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
java.net.SocketTimeoutException: Read timed o
本文主要介紹了java.net.SocketTimeoutException: Read timed out異常的解決,可能是因?yàn)榫W(wǎng)絡(luò)延遲、服務(wù)器響應(yīng)慢或連接不穩(wěn)定等原因造成的,下面就一起來(lái)介紹一下,感興趣的可以了解一下2024-05-05

