home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-02-21 | 6.8 KB | 216 lines |
- //
- // jdbcReadBLOB
- //
- // Author: Puru Balakrishnan
- // Date: 2/1/97
-
- //
- // jdbcReadBLOB is a java application program which will read data
- // from a table and write the data for LongVarBinary & LongVarChar
- // columns into a file. Data for all other datatypes would displayed
- // on the console.
- //
- // Usage: jdbcReadBLOB -t tablename
- //
- // Mandatory arguments: -t tablename specify a tablename to retrieve data from
- //
-
- import java.lang.*;
- import java.io.*;
- import symjava.sql.*;
-
- //////////////////////////////////////////////////////////////////////////////
- // jdbcReadBLOB
- //////////////////////////////////////////////////////////////////////////////
-
- class jdbcReadBLOB extends jdbcViewer
- {
- static protected String m_strDirectory = "c:\\test\\dbaw\\Samples\\jdbcReadBLOB\\";
- static protected String m_strBaseFileName = "rowdata";
- static protected String m_strFileExt = null;
- static int iRowCount = 0;
-
- ///////////////////////////////////////////////////////////////////////////
- public static void main( String args[] )
- {
- jdbcReadBLOB rLargeData = new jdbcReadBLOB();
- rLargeData.shellMain( args );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public void displayUsage()
- {
- super.displayUsage();
-
- System.out.println( " -t <tablename> - table to display information/contents " );
- }
-
- ///////////////////////////////////////////////////////////////////////////
- protected void init()
- {
- m_strAppName = "jdbcReadBLOB";
- }
-
- ///////////////////////////////////////////////////////////////////////////
- public String getColumnDataValue( int colNum, int colType, ResultSet rs ) throws SQLException
- {
- String strValue = "";
-
- switch ( colType ) {
- case Types.BIT:
- strValue = String.valueOf( rs.getBoolean( colNum ) );
- break;
-
- case Types.TINYINT:
- strValue = String.valueOf( rs.getByte( colNum ) );
- break;
-
- case Types.SMALLINT:
- strValue = String.valueOf( rs.getShort( colNum ) );
- break;
-
- case Types.INTEGER:
- strValue = String.valueOf( rs.getInt( colNum ) );
- break;
-
- case Types.BIGINT:
- strValue = String.valueOf( rs.getLong( colNum ) );
- break;
-
- case Types.FLOAT:
- strValue = String.valueOf( rs.getFloat( colNum ) );
- break;
-
- case Types.REAL:
- strValue = String.valueOf( rs.getFloat( colNum ) );
- break;
-
- case Types.DOUBLE:
- strValue = String.valueOf( rs.getDouble( colNum ) );
- break;
-
- case Types.NUMERIC:
- case Types.DECIMAL:
- {
- ResultSetMetaData meta = rs.getMetaData();
- symjava.math.BigDecimal n = rs.getBigDecimal( colNum, meta.getScale( colNum ) );
- if ( n != null ) {
- strValue = String.valueOf( n );
- }
- break;
- }
-
- case Types.CHAR:
- case Types.VARCHAR:
- strValue = rs.getString( colNum );
- break;
-
- case Types.LONGVARCHAR:
- {
- ++iRowCount;
- if ( m_strFileExt == null ) {
- m_strFileExt = ".txt";
- }
- String strFullyQualFileName = m_strDirectory + m_strBaseFileName + iRowCount + m_strFileExt;
-
- try {
- File f = new File(strFullyQualFileName);
- FileOutputStream fOutput = new FileOutputStream( f );
- DataOutputStream dOut = new DataOutputStream( fOutput );
-
- java.io.InputStream iStream = rs.getBinaryStream( colNum );
- DataInputStream dIn = new DataInputStream( iStream );
-
- int i;
-
- while ( ( i = dIn.read() ) != -1 ) {
- dOut.write(i);
- }
-
- strValue = "<DATA FOR LONGVARCHAR IN:" + strFullyQualFileName + ">";
- }
- catch ( IOException e ) {
- strValue = "<DATA FOR LONGVARCHAR IN ROW" + iRowCount + " COULD NOT BE WRITTEN>";
- }
- }
- break;
-
- case Types.DATE:
- symjava.sql.Date dt = rs.getDate( colNum );
- if ( dt != null ) {
- strValue = dt.toString();
- }
- break;
-
- case Types.TIME:
- Time tm = rs.getTime( colNum );
- if ( tm != null ) {
- strValue = tm.toString();
- }
- break;
-
- case Types.TIMESTAMP:
- Timestamp ts = rs.getTimestamp( colNum );
- if ( ts != null ) {
- strValue = ts.toString();
- }
- break;
-
- case Types.BINARY:
- case Types.VARBINARY:
- strValue = "<BINARY DATA>";
- break;
-
- case Types.LONGVARBINARY:
- {
- ++iRowCount;
- if ( m_strFileExt == null ) {
- m_strFileExt = ".bin";
- }
- String strFullyQualFileName = m_strDirectory + m_strBaseFileName + iRowCount + m_strFileExt;
-
- try {
- File f = new File(strFullyQualFileName);
- FileOutputStream fOutput = new FileOutputStream( f );
- DataOutputStream dOut = new DataOutputStream( fOutput );
-
- java.io.InputStream iStream = rs.getBinaryStream( colNum );
- DataInputStream dIn = new DataInputStream( iStream );
-
- int i;
-
- while ( ( i = dIn.read() ) != -1 ) {
- dOut.write(i);
- }
-
-
- strValue = "<DATA FOR LONGVARBINARY IN:" + strFullyQualFileName + ">";
- }
- catch ( IOException e ) {
- strValue = "<DATA FOR LONGVARBINARY IN ROW" + iRowCount + " COULD NOT BE WRITTEN>";
- }
- }
- break;
-
- case Types.OTHER:
- strValue = "Other: " + rs.getString( colNum );
- break;
-
- case Types.NULL:
- strValue = "<<NULL>>";
- break;
-
- default:
- strValue = "Default: " + rs.getString( colNum );
- break;
- }
-
- if ( rs.wasNull() == true ) {
- strValue = "<<NULL>>";
- }
-
- return strValue;
- }
-
-
- }