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.

using image fields to store & retrieve gif/jpg files

3 posts in JDBC Connect (product renamed to JConnect) Last posting was on 1997-09-18 17:56:04.0Z
Phil Posted on 1997-09-17 06:41:07.0Z
Message-ID: <341F7B83.F06D10DB@global.net.au>
Date: Wed, 17 Sep 1997 14:41:07 +0800
From: Phil <phils@global.net.au>
X-Mailer: Mozilla 4.02 [en] (Win95; I)
MIME-Version: 1.0
Subject: using image fields to store & retrieve gif/jpg files
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 19
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:268
Article PK: 252020

I have a requirement to store and retrieve image data type fields from a
sybase database and to use them to when constructing an HTML page. If
anyone had already done this, or if someone knows the proper steps
required to do it please let me know how it is done.

I have given consideration to using
java.sql.PreparedStatement.setBinaryStream and
java.sql.PreparedStatement.getBinaryStream. I don't know is the binary
stream can be used in the HTML <IMG SRC="getBinaryStream(x,y)"> or is
some other technique is required. another question is; is the
BinaryStream still in the 2K packed hexadecimal format, or does jConnect
convert it to raw binary?

Thanks in advance for any help received.

Phil


David Clegg Posted on 1997-09-18 17:56:04.0Z
Message-ID: <34216B34.C27F448@sybase.com>
Date: Thu, 18 Sep 1997 10:56:04 -0700
From: David Clegg <davec@sybase.com>
X-Mailer: Mozilla 3.01 (X11; I; Linux 1.2.13 i586)
MIME-Version: 1.0
To: Phil <phils@global.net.au>
Subject: Re: using image fields to store & retrieve gif/jpg files
References: <341F7B83.F06D10DB@global.net.au>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 35
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:254
Article PK: 252006

Phil,
I assume that you want to run jConnect in a Servlet (javax.servlet
in JavaServer or NSAPI/ASAPI/CGI-script). Your web server would
invoke a java application/servlet which uses jConnect to retrieve
the binary data from SQL.

What you would want to do is put as <IMG SRC=XXXXXXX...
something so that when your web-server receives a GET XXXXXXX
request, it redirects that request to your java application - the
java application then sends back the packed hex data in jpg/gif
format just as a cgi-script would. (You get to write the code
which converts the byte[] data to this format!).

dave

Phil wrote:
>
> I have a requirement to store and retrieve image data type fields from a
> sybase database and to use them to when constructing an HTML page. If
> anyone had already done this, or if someone knows the proper steps
> required to do it please let me know how it is done.
>
> I have given consideration to using
> java.sql.PreparedStatement.setBinaryStream and
> java.sql.PreparedStatement.getBinaryStream. I don't know is the binary
> stream can be used in the HTML <IMG SRC="getBinaryStream(x,y)"> or is
> some other technique is required. another question is; is the
> BinaryStream still in the 2K packed hexadecimal format, or does jConnect
> convert it to raw binary?
>
> Thanks in advance for any help received.
>
> Phil


Lance Andersen Posted on 1997-09-17 12:58:39.0Z
Message-ID: <341FD3FF.26C@sybase.com>
Date: Wed, 17 Sep 1997 08:58:39 -0400
From: Lance Andersen <lancea@sybase.com>
X-Mailer: Mozilla 3.01Gold (X11; I; SunOS 5.5.1 sun4m)
MIME-Version: 1.0
To: Phil <phils@global.net.au>
Subject: Re: using image fields to store & retrieve gif/jpg files
References: <341F7B83.F06D10DB@global.net.au>
Content-Type: multipart/mixed; boundary="------------3B16496A472D"
Newsgroups: sybase.public.jdbcconnect
Lines: 254
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:265
Article PK: 252016


Phil wrote:
>
> I have a requirement to store and retrieve image data type fields from a
> sybase database and to use them to when constructing an HTML page. If
> anyone had already done this, or if someone knows the proper steps
> required to do it please let me know how it is done.
>
> I have given consideration to using
> java.sql.PreparedStatement.setBinaryStream and
> java.sql.PreparedStatement.getBinaryStream. I don't know is the binary
> stream can be used in the HTML <IMG SRC="getBinaryStream(x,y)"> or is
> some other technique is required. another question is; is the
> BinaryStream still in the 2K packed hexadecimal format, or does jConnect
> convert it to raw binary?
>
> Thanks in advance for any help received.
>
> Phil

I have attached a simple program which demonstrates the use of
set/getBinaryStream.

You will need to create an applet to display the image. the program
simply demonstrates the use of the methods...


-lance

--
===============================================================================
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.java Sybase Product Support group, 06/01/97
* Copyright (c) 1997, Sybase., Emeryville, CA 94608
* All Rights Reserved
*
* TITLE: BinaryStream.java
*
* START-HISTORY:
*
* 01 Jun 97 edit 0 - Lance Andersen.
* Initial coding.
*
* END-HISTORY
*
* START-DESCRIPTION:
*
* 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)
* -U username
* -P password
* -D debuglibraries
* -S server
* -create create the table blobtest(f1 int, f2 image)
*
*
* END-DESCRIPTION
*/



import java.io.*;
import java.sql.*;
import java.util.*;
import com.sybase.utils.Debug;

class BinaryStream {

static String _blobName = "/tmp/ablob";
static String _user = "sa";
static String _password = "";
static String _url = "jdbc:sybase:Tds:alder:6689/tempdb";
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(_url, _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)
{
execDDL(con, _createQuery);
}

// 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
* Insert a row which contains an image column using setBinaryStream
*/


private static void addABlob( Connection con)
throws SQLException, FileNotFoundException
{
// Read in Blob and store it in our table
// Note that the image file must exist

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();
}

/*
* getaBlob
* Retrieve the contents of a image column using getBinaryStream
*/

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();
}
/*
* execDDL
* Execute a DDL or a DML statement that does not return a ResultSet
*/

private static void execDDL( Connection con, String cmd) throws SQLException
{
System.out.println("Executing: " + cmd);
Statement statement = con.createStatement();
int numrows = statement.executeUpdate(cmd);
System.out.println("Number of rows affected= " + numrows);
statement.close();
}

/*
* processCommandline
* Parse the Command Line and set the appropriate options
*/

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++;
_url = 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);
}

}