Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Problem reading LONG Column using jdbc thin driver

Re: Problem reading LONG Column using jdbc thin driver

From: John Przybylski <John.Przybylski_at_lmco.com>
Date: Tue, 02 Mar 1999 18:27:57 -0500
Message-ID: <36DC73FD.5130EAA1@lmco.com>


The example should run without any troubles. Just change the hostname and make sure the oracle thin driver is in the classpath. Don't bother trying to use the thin driver for BLOB/CLOBs with Oracle 8. Sorry for the looong example :)

import java.io.*;
import java.lang.*;
import java.net.*;
import java.sql.*;
import java.sql.ResultSetMetaData;
import java.util.*;
import java.util.Date;

public class sql
{

  static long l1,l2,duration;
  static Connection conn=null;

  public static void main (String args[])   {
    Vector vec = new Vector();
    Properties dbprop = new Properties();     try
{

conn=connect("oracle.jdbc.driver.OracleDriver","jdbc:oracle:thin:@localhost:1521:ORCL","scott","tiger");

    } catch (ClassNotFoundException cnfe) {}       catch (SQLException sqle) {}
    System.out.println("Drop the current table");     exec("drop table TEST");
    System.out.println("Table dropped, creating TEST");     exec("create table TEST (ID VARCHAR2(10),NAME LONG)");     System.out.println("Insert values into table");     exec("insert into TEST (ID,NAME) VALUES ('1','New Company')");     System.out.println("Retrieving a long column");     select("select ID,NAME from TEST","test");     vec = select("select ID,NAME from TEST");     System.out.println("The vector returned "+vec.size()+" records");     for (int i=0; i<vec.size();i++)
{

      dumpHashtable((Properties)vec.elementAt(i),System.out);     }
  }

  /**

      if (obj != null)
      {
        if (obj.getClass().getName().compareTo( "java.lang.String" ) == 0) { sVal
= (String)ht.get( sKey ); }
        else { try { sVal = ((String[])ht.get( sKey ))[0]; } catch
(ClassCastException eCCE) { sVal = obj.getClass().getName(); } }
      }
      o.println( "  [" + sKey + ", " + sVal + "]" );
    }
  }
  /*
   * generic connect method
   */

  public static Connection connect(String drvr, String cnct, String userid, String passwd)

       throws SQLException, ClassNotFoundException   {
    Connection c = null;
    l1 = new Date().getTime();
    Class.forName (drvr);
    l2 = new Date().getTime();
    duration = (l2-l1);
    System.out.println("Duration of Load JDBC Driver="+duration+" ms");

    l1 = new Date().getTime();
    c = DriverManager.getConnection (cnct,userid, passwd);     l2 = new Date().getTime();
    duration = (l2-l1);
    System.out.println("Duration of Create Connection="+duration+" ms");     return c;
  }

  /*
   * Generic Execute
   */

  public static void exec(String st)
  {
    System.out.println("start exec");
    int res=0;
    Statement stmt = null;
    try
{

      stmt = conn.createStatement();
    } catch (SQLException sqle) { System.out.println(sqle.toString()); }

    l1 = new Date().getTime();
    try
{

      res = stmt.executeUpdate ( st );
    } catch (SQLException sqle) {}
    l2 = new Date().getTime();
    duration=(l2-l1);
    System.out.println("end exec res="+res+" Duration="+duration+" ms");   }

  /*
   * Generic Select
   * select string from string [WHERE string] [ORDER BY string]
   */

  public static void select(String st, String test)   {
    Statement stmt = null;
    ResultSet rset = null;
    String value="";
    Vector vec = new Vector();
    byte[] buff = new byte[4096];
    int size=0;
    try
{
      stmt = conn.createStatement();
      stmt.setMaxFieldSize(0);

    } catch (SQLException sqle) {}

    System.out.println("query: ["+st+"]");

    l1 = new Date().getTime();
    try
{

      rset = stmt.executeQuery(st);
    } catch (SQLException sqle) { System.out.println(sqle.toString()); }     l2 = new Date().getTime();
    System.out.println("Duration of select Query="+duration+" ms");     int cnt2 = 0;
    l1 = new Date().getTime();
    try
{

      cnt2=(rset.getMetaData()).getColumnCount();
      while (rset.next())
      {
        Properties prop = new Properties();
        for (int i=1;i<cnt2+1;i++)
        {
          String column=((rset.getMetaData()).getColumnName(i)).toLowerCase();
          // get type looking for CLOBS  getAsciiStream(int columnIndex)
          String ctype =(rset.getMetaData()).getColumnTypeName(i);
          System.out.println("column number "+i+" is a "+ctype+" type");
          if (ctype.compareTo("VARCHAR2")==0)
          {
            value=rset.getString(i);
            System.out.println("value="+value);
          }
          else
          {
            System.out.print("not a VARCHAR2\nbuffer=");
            InputStream fin = rset.getAsciiStream(i);
            for (;;)
            {
              try
              {
                size = fin.read(buff);
              } catch (IOException ioe) {System.out.println(ioe.toString());}
              if (size == -1)
                { // at end of stream
                break;
              }
              // Send the newly-filled buffer to some ASCII output stream:
              System.out.write(buff, 0, size);
              // outputstream to string
            }
          }
        }
        System.out.println("");
      }

    } catch (SQLException sqle) {}
    System.out.println("select query completed");   }
  /*
   * Select returns vector of properties
   */

  public static Vector select(String st)   {
    System.out.println("start select");     Statement stmt = null;
    ResultSet rset = null;
    String value="";
    Vector vecTmp = new Vector();
    try
{
      stmt = conn.createStatement();
      stmt.setMaxFieldSize(0);

    } catch (SQLException sqle) { System.out.println(sqle.toString()); }

    l1 = new Date().getTime();
    try
{

      rset = stmt.executeQuery(st);
    } catch (SQLException sqle) { System.out.println(sqle.toString()); }     l2 = new Date().getTime();
    int cnt2 = 0;
    l1 = new Date().getTime();
    try
{

      cnt2=(rset.getMetaData()).getColumnCount();
      while (rset.next())
      {
        Properties prop = new Properties();
        for (int i=1;i<cnt2+1;i++)
        {
          String column=((rset.getMetaData()).getColumnName(i)).toLowerCase();
          String ctype =(rset.getMetaData()).getColumnTypeName(i);
          value=rset.getString(i);
          if (value!=null)
          {
            prop.put(column,value);
          }
        }
        vecTmp.addElement(prop);
      }

    } catch (SQLException sqle) {}
    System.out.println("end select returned "+vecTmp.size()+" records duration "+duration+" ms");

    return vecTmp;
  }
}



afrancis_at_my-dejanews.com wrote:
> hi,
>
> I'm trying to grab a LONG column off an oracle 8.x database.  I've got the
> most recent jdbc from the oracle site, I'm using the thin driver.  Is there a
> problem accessing this?
>
> I'm trying to read it off the database, turn it into a string (it will be one
> long token).
>
>          So after I do a select I then:
>
>    while (rset.next()) {
>        InputStream stream_var = rset.getAsciiStream ("COLUMN_NAME");
>        Reader r = new BufferedReader (new InputStreamReader(stream_var));
>        StreamTokenizer st = new StreamTokenizer(r);
>        string_var = st.sval;
>        System.out.println(string_var);
>
> the printed line just returns: null But there is text in the LONG column, and
> when I was testing and using a varchar field type every thing worked.
>
> So, I wondering what I'm doing wrong, or if the oracle thin driver has any
> problems with LONG  (Note: not to be confused with the long integer type in
> java)
>
> Any help appreciated.  I'm pretty new to this so please, include details!
> Thanks in advance
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own

Received on Tue Mar 02 1999 - 17:27:57 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US