zoukankan      html  css  js  c++  java
  • 实现从MySql数据库中查看所有的表及其对应的列数

    这里简单实现了从数据库中,查看所有的表及其对应的列数

    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();
    		 }
    

      

  • 相关阅读:
    python基础学习-socket套接字的应用
    python基础学习-网络编程(二)
    python基础学习-网络编程(一)
    python基础学习-异常处理
    作业0313
    作业3.11
    作业 3.10
    作业03
    day64——orm单表操作/多表操作
    day63——CBV内部原理、过滤器、标签、inclusion_tag、模版的继承
  • 原文地址:https://www.cnblogs.com/ywl925/p/3020398.html
Copyright © 2011-2022 走看看