简体   繁体   中英

Getting mysql database size using java

I have an online MySQL database and i upload data into it using a java program.But every-time before inserting the data into the database i want to check if database is empty or not.And if it is empty how much bits are available so that i could compare it with the file(whose size i know before hand)which i have to upload. NOTE:- Total database size is 5MB

I taught of using ResultSet class with "Select * from table" and get the size of object returned by result set.But it seems ResultSet does not provide any such method to achieve it.Please help me out .

String sql = "SELECT table_schema AS "Database", 
ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS "Size (MB)" 
FROM information_schema.TABLES 
GROUP BY table_schema;";
ResultSet resultSet;
PreparedStatement statement = this.conn.prepareStatement(sql);
resultSet= statement.executeQuery();
while (resultSet.next()) {
    System.out.println(resultSet.getString("Database") " | " + resultSet.getString("Size (MB)"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM