2012-11-23 106 views
1

我检查我的数据库的大小(当我检查所使用的数据库的大小,我发现它在浮点值):我使用这段代码:无法获取数据库的大小

Public class DBSize{ 

private float dbSize; 

    public float databaseSize(Float dbSize,String dataSize,String indexsize){ 
     String apps="apps"; // It's my DB name 
     String query_data = "select table_schema,SUM(data_length+index_length)/1024/1024 AS total_mb,SUM(data_length)/1024/1024 AS data_mb,SUM(index_length)/1024/1024 AS index_mb,COUNT(*) AS tables,CURDATE() AS today FROM information_schema.tables where table_schema='"+apps+"' GROUP BY table_schema ORDER BY table_schema;"; 
     try { 
      connection = getConnection(); 
      stmt = connection.createStatement(); 
      ResultSet rs=stmt.executeQuery(query_data); 
      if(rs.next())  //as I'm selecting a particular database's information 
      this.dbSize=rs.getFloat(2); 
      this.dataSize=rs.getString(3); 
      this.indexSize=rs.getString(4); 

      System.out.println("DB Size : "+this.dbSize);   
     } catch (SQLException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try { 
       if(stmt != null) 
        stmt.close(); 
       if(connection != null) 
        connection.close(); 
       } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 

     return dbSize; 

    } 

public float getDbSize(){ 
     return dbSize; 
    } 
} 

rs越来越空值(不知道无法提取从数据库或值不是是否),即使我已经使用字符串类型的功能,而且还获得了空值,

任何输入......这哪里我要错了:(:(

+0

你是否先在数据库上激发了你的查询? – Abubakkar

+0

是的,..也显示适当的值 –

+0

你可以发布堆栈跟踪? – Abubakkar

回答

1

保持分配这三个声明if块后,如果condidion rs.next()计算结果为true和明年2个语句将始终执行即使condidion rs.next()计算为false这可能会导致一些例外,否则只有第一条语句将得到执行内部

if(rs.next()){  
    this.dbSize=rs.getFloat(2); 
    this.dataSize=rs.getString(3); 
    this.indexSize=rs.getString(4); 
} 
+0

任何权限相关的问题都可以在那里....我想。 –

+0

是啊可能是,但我的回答可能会帮助你 – Abubakkar

+0

我无法检索任何值...到目前为止:( –