How to create table based on JDBC Result Set

后端 未结 5 1916
春和景丽
春和景丽 2020-12-18 15:24

I am building a reporting tool and I need to execute queries on remote databases and store the result set in my own database (because I do not have write permission on remot

5条回答
  •  别那么骄傲
    2020-12-18 16:27

    Maybe this is a little tricky, but I think it might help you.

    JDBC ResultSet objects have a getMetaData() method that returns a ResultSetMetaData object that contains, among other things, the column names and column types. You could do something like this:

    ResultSet rs;
    String strSQL;
    
    ...
    
    strSQL = "create table tbl ("
    for(i=0;i0)
            strSQL += ", "
        strSQL += rs.getMetaData().getColumnName(i) 
               + " " 
               + rs.getMetaData().getColumnType(i);
    }
    strSQL+= ")"
    
    ....
    

    You can construct this way a valid SQL DML that has the columns you need. After that, all that remains is populate the table.

    Hope this helps you.

提交回复
热议问题