Creating a DB connection and executing a query
SELECT DISTINCT USERNAME AS TEST, SEC_OBJ_CODE AS TEST1 ,SEC_OBJ_MEMBER_VAL, DML_MODE FROM SEC_SECURE_BY_OBJ_MEMBER_USER_TMP;
Now from the java class, the objective is to create a temporary table with the same column and the below code helps to achieve it but in the output, it prints with column names "TEST" and "TEST1" but the expectation is to get - "USERNAME" and "SEC_OBJ_CODE" as mentioned in the query.
Java class:
PreparedStatement ps = connection.prepareStatement("SELECT DISTINCT USERNAME AS TEST, SEC_OBJ_CODE AS TEST1 ,SEC_OBJ_MEMBER_VAL, DML_MODE FROM SEC_SECURE_BY_OBJ_MEMBER_USER_TMP");
ResultSet rs = ps.executeQuery();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
String tableName = null;
StringBuilder sb = new StringBuilder( 1024 );
if ( columnCount > 0 ) {
sb.append( "Create table " ).append("ML_TEMP").append( rsmd.getTableName( 1 ) ).append( " ( " );
}
// for columns
for ( int i = 1; i <= columnCount; i ++ ) {
if ( i > 1 ) sb.append( ", " );
String columnName = rsmd.getColumnLabel( i );
String columnType = rsmd.getColumnTypeName( i );
sb.append( columnName ).append( " " ).append( columnType );
int precision = rsmd.getPrecision( i );
if ( precision != 0 ) {
sb.append( "( " ).append( precision ).append( " )" );
}
}
sb.append( " )" );
System.out.println( sb.toString() );
Output : Create table ML_TEMP ( TEST VARCHAR2( 512 ), TEST1 VARCHAR2( 255 ), SEC_OBJ_MEMBER_VAL VARCHAR2( 4000 ), DML_MODE CHAR( 1 ) );
Expectation : Create table ML_TEMP ( USERNAME VARCHAR2( 512 ), SEC_OBJ_CODE VARCHAR2( 255 ), SEC_OBJ_MEMBER_VAL VARCHAR2( 4000 ), DML_MODE CHAR( 1 ) );
Any input is appreciated. Thanks