Lấy tên Storage Engine của bảng qua JDBC MySQL
Truy vấn information_schema.tables cột ENGINE từ Java bằng JDBC.
Tạo bảng
mysql> create table DemoTable (
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Name varchar(20),
Age int,
CountryName varchar(20)
);
Query OK, 0 rows affected (0.63 sec)
Mã Java JDBC
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class GetTheStorageEngine {
public static void main(String[] args) {
String hostURL = "jdbc:mysql://localhost:3306/web?useSSL=false";
Connection con = null;
Statement stmt = null;
try {
con = DriverManager.getConnection(hostURL, "root", "123456");
stmt = con.createStatement();
String yourTablename = "DemoTable";
ResultSet resultSet = stmt.executeQuery(
"select engine from information_schema.tables where table_name='" + yourTablename + "';"
);
resultSet.next();
System.out.println("The engine is=" + resultSet.getString(1));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Kết quả
The storage engine is=InnoDB
Các Storage Engine phổ biến:
- InnoDB: Mặc định từ MySQL 5.5, hỗ trợ transaction, foreign key, row-level lock
- MyISAM: Cũ, table-level lock, không transaction, fulltext search tốt (trước 5.6)
- MEMORY: Lưu RAM, mất dữ liệu khi restart
- CSV: Lưu file CSV
⚠️ Cảnh báo SQL Injection: Code ví dụ nối chuỗi SQL trực tiếp. Nên dùng PreparedStatement:
PreparedStatement ps = con.prepareStatement(
"SELECT engine FROM information_schema.tables WHERE table_name = ?"
);
ps.setString(1, yourTablename);
ResultSet rs = ps.executeQuery();