我需要一个Java代码读取mysql数据库的所有表和元数据,可以给我代码吗
时间: 2023-02-28 11:01:05 浏览: 159
好的,这里是一个读取MySQL数据库表的简单Java代码:
```
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class ReadMySQLTables {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/testdb";
String username = "root";
String password = "root";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet resultSet = databaseMetaData.getTables(null, null, "%", null);
while (resultSet.next()) {
System.out.println("Table name: " + resultSet.getString("TABLE_NAME"));
System.out.println("Table type: " + resultSet.getString("TABLE_TYPE"));
System.out.println("---");
}
} catch (SQLException e) {
System.out.println("An error occurred while reading the tables: " + e.getMessage());
}
}
}
```
请注意,需要确保已经安装了MySQL驱动,并在代码中加载该驱动。
阅读全文