Sybase NNTP forums - End Of Life (EOL)

The NNTP forums from Sybase - forums.sybase.com - are now closed.

All new questions should be directed to the appropriate forum at the SAP Community Network (SCN).

Individual products have links to the respective forums on SCN, or you can go to SCN and search for your product in the search box (upper right corner) to find your specific developer center.

ResultSet getBytes Function

2 posts in JDBC Connect (product renamed to JConnect) Last posting was on 1997-06-18 12:00:58.0Z
Kim Drongesen Posted on 1997-06-17 21:13:49.0Z
Message-ID: <33A6FE0D.429A@ovro.caltech.edu>
Date: Tue, 17 Jun 1997 14:13:49 -0700
From: Kim Drongesen <kimd@ovro.caltech.edu>
Organization: Owens Valley Radio Observatory
X-Mailer: Mozilla 3.0 (X11; I; SunOS 5.5.1 sun4u)
MIME-Version: 1.0
CC: kimd@ovro.caltech.edu
Subject: ResultSet getBytes Function
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 18
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:668
Article PK: 252420

I am running the latest jConnect beta on a Solaris 2.x machine. I am
having no trouble connecting to our database (Sybase) or getting
ResultSets back from my queries. However, I have been trying something
like the following today with no luck:


byte newbyte[];
String newstring;
ResultSet rs;
InputStream is;

rs.next();
newstring = rs.getString(1);
newbyte = rs.getBytes(1);
is = rs.getBinaryStream(1);

The call to getString works fine. The other 2 just hang. I noticed in
the newsgroup that someone said that ResultSet isn't fully implemented -
are these calls in there - or what am I doing wrong?

Thanks for the help.

Kim Drongesen
California Institute of Technology


Lance Andersen Posted on 1997-06-18 12:00:58.0Z
Message-ID: <33A7CDFA.4109@sybase.com>
Date: Wed, 18 Jun 1997 08:00:58 -0400
From: Lance Andersen <lancea@sybase.com>
X-Mailer: Mozilla 3.01Gold (X11; I; SunOS 5.5.1 sun4m)
MIME-Version: 1.0
To: Kim Drongesen <kimd@ovro.caltech.edu>
Subject: Re: ResultSet getBytes Function
References: <33A6FE0D.429A@ovro.caltech.edu>
Content-Type: multipart/mixed; boundary="------------38A23B883292"
Newsgroups: sybase.public.jdbcconnect
Lines: 236
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:666
Article PK: 252418

I haven't had any problems using getBinaryStream or getBytes.

I have attached a sample program which can be used to test
getBinaryStream.


-lance

Kim Drongesen wrote:
>
> I am running the latest jConnect beta on a Solaris 2.x machine. I am
> having no trouble connecting to our database (Sybase) or getting
> ResultSets back from my queries. However, I have been trying something
> like the following today with no luck:
>
> byte newbyte[];
> String newstring;
> ResultSet rs;
> InputStream is;
>
> rs.next();
> newstring = rs.getString(1);
> newbyte = rs.getBytes(1);
> is = rs.getBinaryStream(1);
>
> The call to getString works fine. The other 2 just hang. I noticed in
> the newsgroup that someone said that ResultSet isn't fully implemented -
> are these calls in there - or what am I doing wrong?
>
> Thanks for the help.
>
> Kim Drongesen
> California Institute of Technology

--
===============================================================================
Lance J. Andersen Email: lancea@sybase.com
Sybase Technical Support Phone:(617) 564-6336
77 South Bedford Street Fax: (617) 564-6148
Burlington, MA 01803

The Dark Knight Returns!!! Let's Go Penguins!!!
===============================================================================

//
// BinaryStream class demonstrates how to use the setBinaryStream
// and getBinaryStream methods.
//
// BinaryStream may be invoked with the optional parameters:
// -f filename (default is /tmp/ablob)
// -create create the table blobtest(f1 int, f2 image)
// -U username
// -P password
// -D debuglibraies
// -S server
//
import java.io.*;
import java.sql.*;
import java.util.*;
import com.sybase.utils.Debug;

class BinaryStream {

static Connection _con = null;
static String _blobName = "/tmp/ablob";
static String _user = "sa";
static String _password = "";
static String _server = "jdbc:sybase:Tds:alder:6689/victimdb";
static String createQuery = "create table blobtable(f1 int, f2 Image)";
static boolean _createTable = false;


public static void main (String args[]) {


// Parse the command line

if (!processCommandline(args))
{
System.out.println(
"Syntax:\n" +
"\tBinaryStream [-U <username>] [-P <password>] " +
" [-S <servername>]\n\t\t [-D <debug-class-list>]\n" +
"\t\t [-create] [-f blobname]");
System.exit(1);
}


try {

// Load the Sybase Driver

Class.forName("com.sybase.jdbc.SybDriver");


// Attempt to connect to a driver.

Connection _con = DriverManager.getConnection(_server,
_user, _password);

// If we were unable to connect, an exception
// would have been thrown. So, if we get here,
// we are successfully connected to the URL

// Check for, and display and warnings generated
// by the connect.

checkForWarning (_con.getWarnings ());

// See if we need to create our table

if(_createTable)
{
createTable(_con);
}

// Now insert or blob

addABlob(_con);

// Now Retrieve it back

getABlob(_con);

// Close the connection

_con.close();

}
catch (SQLException ex) {

// A SQLException was generated. Catch it and
// display the error information. Note that there
// could be multiple error objects chained
// together

System.out.println ("\n*** SQLException caught ***\n");

while (ex != null) {
System.out.println ("SQLState: " + ex.getSQLState ());
System.out.println ("Message: " + ex.getMessage ());
System.out.println ("Vendor: " + ex.getErrorCode ());
ex = ex.getNextException ();
System.out.println ("");
}
}
catch (java.lang.Exception ex) {

// Got some other type of exception. Dump it.

ex.printStackTrace ();
}
}

//-------------------------------------------------------------------
// checkForWarning
// Checks for and displays warnings. Returns true if a warning
// existed
//-------------------------------------------------------------------

private static boolean checkForWarning (SQLWarning warn) throws SQLException
{
boolean rc = false;

// If a SQLWarning object was given, display the
// warning messages. Note that there could be
// multiple warnings chained together

if (warn != null) {
System.out.println ("\n *** Warning ***\n");
rc = true;
while (warn != null) {
System.out.println ("SQLState: " + warn.getSQLState ());
System.out.println ("Message: " + warn.getMessage ());
System.out.println ("Vendor: " + warn.getErrorCode ());
System.out.println ("");
warn = warn.getNextWarning ();
}
}
return rc;
}
// addABlob

private static void addABlob( Connection con)
throws SQLException, FileNotFoundException
{
// Read in Blob and store it in our table

File fd = new File(_blobName);
FileInputStream fin = new FileInputStream(fd);
PreparedStatement pstmt =
con.prepareStatement("insert blobtable values(1, ?)" );

System.out.print("Inserting blob " + _blobName);
System.out.println(", Size= " + fd.length() + " bytes");
pstmt.setBinaryStream(1, fin, (int) fd.length());
pstmt.execute();
pstmt.close();
}
private static void getABlob( Connection con)
throws SQLException, IOException
{
Statement stmt = con.createStatement();;
ResultSet rs = stmt.executeQuery("select f2 from blobtable where f1=1");

int numRead= 0;
int numtot= 0;
int tot = 0;
System.out.println("Now Retrieving blob(s) ");
while(rs.next())
{
byte stuff[] = new byte[10000];
numRead= 0;
numtot= 0;
InputStream is = rs.getBinaryStream(1);
for(;;)
{
numRead= is.read(stuff);
if(numRead == -1)
{
break;
}
numtot += numRead;
}
tot++;
System.out.println("Bytes read for blob " + tot + "= " + numtot);
}

rs.close();
stmt.close();
}
// createTable

private static void createTable( Connection con) throws SQLException
{
System.out.println("Executing: " + createQuery);
Statement statement = con.createStatement();
statement.executeUpdate(createQuery);
statement.close();
}

static private boolean processCommandline(String args[])
{
//* DONE
String arg;
int errorCount = 0;
for (int i = 0; i < args.length; i++)
{
arg = args[i];
if (arg.regionMatches(0, "-", 0, 1))
{
try
{
switch(arg.charAt(1))
{
case 'D':
i++;
try
{
Debug.debug(true, args[i]);
}
catch (IOException ioe)
{
System.out.println("Error turning on debugging " +
ioe);
}
break;
case 'c':
i++;
_createTable = true;
break;
case 'f':
i++;
_blobName = args[i];
break;
case 'U':
i++;
_user = args[i];
break;
case 'P':
i++;
_password = args[i];
break;
case 'S':
i++;
_server = args[i];
break;
default:
System.out.println("Invalid command line option: " + arg);
errorCount++;
break;
}
}
catch (ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("missing option argument");
errorCount++;
}
}
else
{
// The syntax has no non "-" arguments
errorCount++;
}
}

return(errorCount == 0);
}

}