home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / DBServ / SAMPLES / SAMPLES.ZIP / jdbcReadBLOB / jdbcReadBLOB.java next >
Encoding:
Java Source  |  1997-02-21  |  6.8 KB  |  216 lines

  1. //
  2. // jdbcReadBLOB
  3. //
  4. // Author: Puru Balakrishnan
  5. // Date: 2/1/97
  6.  
  7. //
  8. // jdbcReadBLOB is a java application program which will read data
  9. // from a table and write the data for LongVarBinary & LongVarChar
  10. // columns into a file. Data for all other datatypes would displayed
  11. // on the console.
  12. //
  13. // Usage: jdbcReadBLOB -t tablename
  14. //
  15. // Mandatory arguments: -t tablename    specify a tablename to retrieve data from
  16. //
  17.  
  18. import java.lang.*;
  19. import java.io.*;
  20. import symjava.sql.*;
  21.  
  22. //////////////////////////////////////////////////////////////////////////////
  23. // jdbcReadBLOB
  24. //////////////////////////////////////////////////////////////////////////////
  25.  
  26. class jdbcReadBLOB extends jdbcViewer
  27. {
  28.     static protected String m_strDirectory = "c:\\test\\dbaw\\Samples\\jdbcReadBLOB\\";
  29.     static protected String m_strBaseFileName = "rowdata";
  30.     static protected String m_strFileExt = null;
  31.     static int iRowCount = 0;
  32.  
  33.     ///////////////////////////////////////////////////////////////////////////
  34.     public static void main( String args[] )
  35.     {
  36.         jdbcReadBLOB rLargeData = new jdbcReadBLOB();
  37.         rLargeData.shellMain( args );
  38.     }
  39.  
  40.     ///////////////////////////////////////////////////////////////////////////
  41.     public void displayUsage()
  42.     {
  43.         super.displayUsage();
  44.  
  45.         System.out.println( "  -t  <tablename> - table to display information/contents " );
  46.     }
  47.  
  48.     ///////////////////////////////////////////////////////////////////////////
  49.     protected void init()
  50.     {
  51.         m_strAppName = "jdbcReadBLOB";
  52.     }
  53.  
  54.     ///////////////////////////////////////////////////////////////////////////
  55.     public String getColumnDataValue( int colNum, int colType, ResultSet rs ) throws SQLException
  56.     {
  57.         String strValue = "";
  58.  
  59.         switch ( colType ) {
  60.             case Types.BIT:
  61.                 strValue = String.valueOf( rs.getBoolean( colNum ) );
  62.                 break;
  63.  
  64.             case Types.TINYINT:
  65.                 strValue = String.valueOf( rs.getByte( colNum ) );
  66.                 break;
  67.  
  68.             case Types.SMALLINT:
  69.                 strValue = String.valueOf( rs.getShort( colNum ) );
  70.                 break;
  71.  
  72.             case Types.INTEGER:
  73.                 strValue = String.valueOf( rs.getInt( colNum ) );
  74.                 break;
  75.  
  76.             case Types.BIGINT:
  77.                 strValue = String.valueOf( rs.getLong( colNum ) );
  78.                 break;
  79.  
  80.             case Types.FLOAT:
  81.                 strValue = String.valueOf( rs.getFloat( colNum ) );
  82.                 break;
  83.  
  84.             case Types.REAL:
  85.                 strValue = String.valueOf( rs.getFloat( colNum ) );
  86.                 break;
  87.  
  88.             case Types.DOUBLE:
  89.                 strValue = String.valueOf( rs.getDouble( colNum ) );
  90.                 break;
  91.  
  92.             case Types.NUMERIC:
  93.             case Types.DECIMAL:
  94.             {
  95.                 ResultSetMetaData meta = rs.getMetaData();
  96.                 symjava.math.BigDecimal n = rs.getBigDecimal( colNum, meta.getScale( colNum ) );
  97.                 if ( n != null ) {
  98.                     strValue = String.valueOf( n );
  99.                 }
  100.                 break;
  101.             }
  102.  
  103.             case Types.CHAR:
  104.             case Types.VARCHAR:
  105.                 strValue = rs.getString( colNum );
  106.                 break;
  107.  
  108.             case Types.LONGVARCHAR:
  109.             {
  110.                 ++iRowCount;
  111.                 if ( m_strFileExt == null ) {
  112.                     m_strFileExt = ".txt";
  113.                 }
  114.                 String strFullyQualFileName = m_strDirectory + m_strBaseFileName + iRowCount + m_strFileExt;
  115.  
  116.                 try {
  117.                     File f = new File(strFullyQualFileName);
  118.                     FileOutputStream fOutput = new FileOutputStream( f );
  119.                     DataOutputStream dOut = new DataOutputStream( fOutput );
  120.  
  121.                     java.io.InputStream iStream = rs.getBinaryStream( colNum );
  122.                     DataInputStream dIn = new DataInputStream( iStream );
  123.  
  124.                     int i;
  125.  
  126.                     while ( ( i = dIn.read() ) != -1 ) {
  127.                         dOut.write(i);
  128.                     }
  129.  
  130.                     strValue = "<DATA FOR LONGVARCHAR IN:" + strFullyQualFileName + ">";
  131.                 }
  132.                 catch ( IOException e ) {
  133.                     strValue = "<DATA FOR LONGVARCHAR IN ROW" + iRowCount + " COULD NOT BE WRITTEN>";
  134.                 }
  135.             }
  136.                 break;
  137.  
  138.             case Types.DATE:
  139.                 symjava.sql.Date dt = rs.getDate( colNum );
  140.                 if ( dt != null ) {
  141.                     strValue = dt.toString();
  142.                 }
  143.                 break;
  144.  
  145.             case Types.TIME:
  146.                 Time tm = rs.getTime( colNum );
  147.                 if ( tm != null ) {
  148.                     strValue = tm.toString();
  149.                 }
  150.                 break;
  151.  
  152.             case Types.TIMESTAMP:
  153.                 Timestamp ts = rs.getTimestamp( colNum );
  154.                 if ( ts != null ) {
  155.                     strValue = ts.toString();
  156.                 }
  157.                 break;
  158.  
  159.             case Types.BINARY:
  160.             case Types.VARBINARY:
  161.                 strValue = "<BINARY DATA>";
  162.                 break;
  163.  
  164.             case Types.LONGVARBINARY:
  165.             {
  166.                 ++iRowCount;
  167.                 if ( m_strFileExt == null ) {
  168.                     m_strFileExt = ".bin";
  169.                 }
  170.                 String strFullyQualFileName = m_strDirectory + m_strBaseFileName + iRowCount + m_strFileExt;
  171.  
  172.                 try {
  173.                     File f = new File(strFullyQualFileName);
  174.                     FileOutputStream fOutput = new FileOutputStream( f );
  175.                     DataOutputStream dOut = new DataOutputStream( fOutput );
  176.  
  177.                     java.io.InputStream iStream = rs.getBinaryStream( colNum );
  178.                     DataInputStream dIn = new DataInputStream( iStream );
  179.  
  180.                     int i;
  181.  
  182.                     while ( ( i = dIn.read() ) != -1 ) {
  183.                         dOut.write(i);
  184.                     }
  185.  
  186.  
  187.                     strValue = "<DATA FOR LONGVARBINARY IN:" + strFullyQualFileName + ">";
  188.                 }
  189.                 catch ( IOException e ) {
  190.                     strValue = "<DATA FOR LONGVARBINARY IN ROW" + iRowCount + " COULD NOT BE WRITTEN>";
  191.                 }
  192.             }
  193.                 break;
  194.  
  195.             case Types.OTHER:
  196.                 strValue = "Other: " + rs.getString( colNum );
  197.                 break;
  198.  
  199.             case Types.NULL:
  200.                 strValue = "<<NULL>>";
  201.                 break;
  202.  
  203.             default:
  204.                 strValue = "Default: " + rs.getString( colNum );
  205.                 break;
  206.         }
  207.  
  208.         if ( rs.wasNull() == true ) {
  209.             strValue = "<<NULL>>";
  210.         }
  211.  
  212.         return strValue;
  213.     }
  214.  
  215.  
  216. }