这里简单实现了从数据库中,查看所有的表及其对应的列数
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test"; // 3306/后边是数据库名
String user = "root";
String passWord = ""; //这里添加数据库登陆密码
Connection conn = DriverManager.getConnection(url, user, passWord);
if(conn != null)
{
System.out.println("数据库连接成功");
}
DatabaseMetaData dbmd = conn.getMetaData();
String[] types = { "TABLE" };
ResultSet resultSet = dbmd.getTables(null, null, "%", types);
String sqlStr = "";
PreparedStatement ps = null;
ResultSet rs = null;
while (resultSet.next()) {
String tableName = resultSet.getString("TABLE_NAME");
/* String tableCatalog = resultSet.getString(1);
String tableSchema = resultSet.getString(2);
System.out.println(tableName + " "+tableCatalog + " "+ tableSchema);*/
sqlStr = "select count(*) as columNum from information_schema.columns where table_name= '" + tableName + "'";
ps = conn.prepareStatement(sqlStr);
rs = ps.executeQuery();
rs.next();
System.out.println(tableName + " " + rs.getString("columNum"));
}
ps.close();
rs.close();
resultSet.close();
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}