Get the maximum size of columns in a table using java

This program will give the schemaname and tablename and maximum amount of data we can insert into a single row.

package student;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Rmdata {
  public static void main(String[] args) throws Exception {
    Connection conn = getPostgresConnection();  //creating the connection
    System.out.println(“Got Connection.”);
   
    ResultSet rsSchemas,rsTables,rsColumns = null;
    DatabaseMetaData meta = conn.getMetaData();
   
    rsSchemas=meta.getSchemas();
   
    while(rsSchemas.next()) {
        String schemaname=rsSchemas.getString(“TABLE_SCHEM”);
        rsTables=meta.getTables(null, schemaname, null, null);
        while(rsTables.next())
        {
          String tablename=rsTables.getString(“TABLE_NAME”);
          rsColumns = meta.getColumns(null, null, tablename, null);
          long sum=0;
          while(rsColumns.next())
          {
              int size = rsColumns.getInt(“COLUMN_SIZE”);
              sum=sum+size;
          
          }
          System.out.println(”  ==============================================================================”);
         
          System.out.println(”  SCHEMA_NAME:  “+schemaname+”||  TABLE_NAME: “+tablename+”|| COLUMN_SIZE:  “+sum);
         
          System.out.println(”  ==============================================================================”);
        }
    }

 //   st.close();
    conn.close();
  }
 public static Connection getPostgresConnection() throws Exception {
    String url=”jdbc:postgresql://localhost:5432/test”;
    String username=”postgres”;
    String password=”postgres”;
    Class.forName(“org.postgresql.Driver”);
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
 }
  public static Connection getOracleConnection() throws Exception {
    String driver = “oracle.jdbc.driver.OracleDriver”;
    String url = “jdbc:oracle:thin:@localhost:1521:oguri”;
    String username = “oracle”;
    String password = “oracle”;
    Class.forName(driver); // load Oracle driver
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
  }
}

O/P :

Got Connection.
  ==============================================================================
  SCHEMA_NAME:  event||  TABLE_NAME: stu_detail_tbl_pkey|| COLUMN_SIZE:  50
  ==============================================================================
  ==============================================================================
  SCHEMA_NAME:  event||  TABLE_NAME: eve_register|| COLUMN_SIZE:  160
  ==============================================================================
  ==============================================================================
  SCHEMA_NAME:  event||  TABLE_NAME: stu_detail_tbl|| COLUMN_SIZE:  637
  ==============================================================================
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s