jdbc获取数据库信息 转
程序员文章站
2022-04-06 17:43:06
...
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class GetDBTablesInfo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=123456";
Connection conn = DriverManager.getConnection(url);
DatabaseMetaData databaseMetaData = conn.getMetaData();
// 获取所有表
ResultSet tableSet = databaseMetaData.getTables(null, "%", "%",
new String[] { "TABLE" });
while (tableSet.next()) {
String tableName = tableSet.getString("TABLE_NAME");
String tableComment = tableSet.getString("REMARKS");
System.out.println("tableName=" + tableName);
System.out.println("tableComment=" + tableComment);
// 获取tableName表列信息
ResultSet columnSet = databaseMetaData.getColumns(null, "%",
tableName, "%");
while (columnSet.next()) {
String columnName = columnSet.getString("COLUMN_NAME");
String columnComment = columnSet.getString("REMARKS");
String sqlType = columnSet.getString("DATA_TYPE");
System.out.println(columnName);
System.out.println(columnComment);
System.out.println(sqlType);
}
}
}
}
http://www.zhuoda.org/lunzi/97624.html
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class GetDBTablesInfo {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=123456";
Connection conn = DriverManager.getConnection(url);
DatabaseMetaData databaseMetaData = conn.getMetaData();
// 获取所有表
ResultSet tableSet = databaseMetaData.getTables(null, "%", "%",
new String[] { "TABLE" });
while (tableSet.next()) {
String tableName = tableSet.getString("TABLE_NAME");
String tableComment = tableSet.getString("REMARKS");
System.out.println("tableName=" + tableName);
System.out.println("tableComment=" + tableComment);
// 获取tableName表列信息
ResultSet columnSet = databaseMetaData.getColumns(null, "%",
tableName, "%");
while (columnSet.next()) {
String columnName = columnSet.getString("COLUMN_NAME");
String columnComment = columnSet.getString("REMARKS");
String sqlType = columnSet.getString("DATA_TYPE");
System.out.println(columnName);
System.out.println(columnComment);
System.out.println(sqlType);
}
}
}
}
http://www.zhuoda.org/lunzi/97624.html