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正在获取空值(不知道是否无法从数据库中获取值),即使我使用了字符串类型的函数但也获取了空值,

任何输入...我出错的地方:( :(

4

1 回答 1

1

将这三个赋值语句保留if在一个块内,否则只有第一个语句将在条件rs.next() 评估为时执行,true并且接下来的 2 个语句将始终执行,即使条件rs.next() 评估为false 可能导致某些异常

if(rs.next()){     
   this.dbSize=rs.getFloat(2);
   this.dataSize=rs.getString(3);
   this.indexSize=rs.getString(4);
}
于 2012-11-23T13:24:37.973 回答