ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Folashade Adeyosoye" <sharda...@gmail.com>
Subject RE: How to get table metadata?
Date Thu, 18 Aug 2005 18:01:08 GMT
I use mine to back up my database, by passing in the table name into a
method, and this methods creates a flat file with a whole bunch of inserts,
that I can use to repopulate the table if I need to.



public StringBuffer systemDatabaseBackup(String tableName) throws
DaoException {

    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    StringBuffer sb = new StringBuffer();
    StringBuffer sbStart = new StringBuffer();
    StringBuffer sql = new StringBuffer(1024);


    sql.append("SELECT * FROM " + tableName.trim());

    try {
      connection = getConnection();
      statement = connection.prepareStatement(sql.toString());
      resultSet = statement.executeQuery();
      ResultSetMetaData rsmd = resultSet.getMetaData();
      int colType[] = new int[rsmd.getColumnCount()];
      String colName = "";
      String colValue = "";


     sbStart.append("insert into " + tableName + " ( ");


      // setting up the insert statement.
      for (int idx = 0, col = 1; idx < colType.length; idx++, col++) {
        colName = rsmd.getColumnName(col);
        if ( idx < colType.length-1 ){
          sbStart.append(colName + ", ");
        }
        else{
          sbStart.append(colName + " ");
        }
      }
      sb.append(sbStart + ") values (");


      //set the values
      while (resultSet.next()) {
        for (int idx = 0, col = 1; idx < colType.length; idx++, col++) {
          colName = rsmd.getColumnName(col);
          colValue = resultSet.getString(colName);

          if ( colValue == null || colValue.equalsIgnoreCase("null")  ){
            colValue = "";
          }
          else{
            colValue = colValue.replaceAll("'","''");
          }


          if (idx < colType.length-1)
            sb.append("'" + colValue + "',");
          else
            sb.append("'" + colValue + "');");

        }
        if ( !resultSet.isLast()){
          sb.append("\n" + sbStart + ") values (");
        }
      }    }
    catch (SQLException sqlEx) {
      logger.fatal("SQLException Error. " + sqlEx.getMessage());
      throw new DaoException(sqlEx.getMessage());
    }
    catch (Exception ex) {
      logger.error("Exception Error. " + ex.getMessage());
      throw new DaoException(ex.getMessage());
    }
    return sb;
  }

-----Original Message-----
From: Prashanth Sukumaran [mailto:prashanthsukumaran@yahoo.com] 
Sent: Thursday, August 18, 2005 12:45 PM
To: user-java@ibatis.apache.org
Subject: RE: How to get table metadata?

Hi Sreenivasulu,

Can you tell us what you want to do with the column names?

Also you may try to get it this way.  

SqlMapDaoTemplate.getSqlMapExecutor() will give you the sqlMapClient handle.

Then you do  sqlMapClient.getDelegate() will give you the handle to
com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.  

I think com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate is the answer
to you question.  

It has 
  private HashMap mappedStatements;
  private HashMap cacheModels;
  private HashMap resultMaps;
  private HashMap parameterMaps;

May be one of these will have the data for what you are looking for.  You
should try the getter
methods for the data.  All the getters work based on the "ID" (defined in
the sqlmap) of the sql
you are executing.

The developers of IBatis may have a better way.  Just see if these can help
you.

Rgds
Prashanth Sukumaran.


--- "Konda, Sreenivasulu (Consultant)" <Sreenivasulu.Konda@Consultmail.net>
wrote:

> Hi,
>  
> I have not got any solution yet. Can you please share the idea to get
> the column names dynamically?
>  
> Thanks and regards,
> Sreenivas
> 
> ________________________________
> 
> From: Folashade Adeyosoye [mailto:shardayyy@gmail.com] 
> Sent: Thursday, August 18, 2005 10:08 AM
> To: user-java@ibatis.apache.org
> Subject: RE: How to get table metadata?
> 
> 
> 
> Not that I know of, I tried this a while back to dynamically get my
> column names. If you find a solution please let us know.
> 
>  
> 
> ________________________________
> 
> From: Konda, Sreenivasulu (Consultant)
> [mailto:Sreenivasulu.Konda@Consultmail.net] 
> Sent: Thursday, August 18, 2005 10:06 AM
> To: user-java@ibatis.apache.org
> Subject: How to get table metadata?
> 
>  
> 
> Hi,
> 
>  
> 
> I would like to know whether or not we can retrieve table metadata such
> as table name, column name/type, etc... when used iBatis.
> 
>  
> 
> In JDBC, there is a class ResultSetMetaData, which gives us the metadata
> of underlying table(s), column name, type, etc.
> 
>  
> 
> Is there any API to get these details and how?
> 
>  
> 
> Thanks and regards,
> 
> Sreenivas
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


Mime
View raw message