共計 1036 個字符,預計需要花費 3 分鐘才能閱讀完成。
要在 Java 中調用 MySQL 函數,可以使用 JDBC 連接來執行 SQL 語句。以下是一個示例代碼,演示如何調用 MySQL 函數:
java
import java.sql.*;
public class MySQLFunctionExample {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 連接到 數據庫
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_name", "username", "password");
// 創建 Statement 對象
stmt = conn.createStatement();
// 調用 MySQL 函數并獲取結果
rs = stmt.executeQuery("SELECT my_function()");
// 處理結果集
while (rs.next()) {
String result = rs.getString(1);
System.out.println("Result: " + result);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 關閉連接和資源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
在上面的代碼中,my_function()
是要調用的 MySQL 函數。請確保將 db_name
替換為你的數據庫名稱,username
和 password
替換為你的數據庫憑據。
運行代碼后,將輸出 MySQL 函數的結果。
丸趣 TV 網 – 提供最優質的資源集合!