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.

PreparedStatement -- setxxx methods?

3 posts in JDBC Connect (product renamed to JConnect) Last posting was on 1997-09-23 20:04:02.0Z
Hong Chen Posted on 1997-09-23 17:14:36.0Z
Message-ID: <3427F8FC.D9EBD218@lincom-asg.com>
Date: Tue, 23 Sep 1997 12:14:36 -0500
From: Hong Chen <chenh@lincom-asg.com>
X-Mailer: Mozilla 4.01 [en] (WinNT; I)
MIME-Version: 1.0
Subject: PreparedStatement -- setxxx methods?
X-Priority: 3 (Normal)
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 11
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:245
Article PK: 251997

Does PreparedStatement support setxxx methods?
For example:
...
File file = new File("test.gif");
InputStream fin = FileInputStream(file);
...
PreparedStatement pstmt = con.prepareStatement("UPDATE au_pix SET pic=?
WHERE au_id='409-56-7008' ");
pstmt.setBinaryStream(2, fin, filelength); // this cause SQL Exception
pstmt.executeUpdate();
...

I am using jConnect (syb102.zip).

Thanks for any suggestion!


Lance Andersen Posted on 1997-09-23 18:22:19.0Z
Message-ID: <342808DB.4B3@sybase.com>
Date: Tue, 23 Sep 1997 14:22:19 -0400
From: Lance Andersen <lancea@sybase.com>
X-Mailer: Mozilla 3.01Gold (X11; I; SunOS 5.5.1 sun4m)
MIME-Version: 1.0
To: Hong Chen <chenh@lincom-asg.com>
Subject: Re: PreparedStatement -- setxxx methods?
References: <3427F8FC.D9EBD218@lincom-asg.com>
Content-Type: multipart/mixed; boundary="------------73DE88D62BB"
Newsgroups: sybase.public.jdbcconnect
Lines: 244
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:244
Article PK: 251996

Yes it does.


I have included a sample program demonstrating this..

Hong Chen wrote:
>
> Does PreparedStatement support setxxx methods?
> For example:
> ...
> File file = new File("test.gif");
> InputStream fin = FileInputStream(file);
> ...
> PreparedStatement pstmt = con.prepareStatement("UPDATE au_pix SET pic=?
> WHERE au_id='409-56-7008' ");
> pstmt.setBinaryStream(2, fin, filelength); // this cause SQL Exception
> pstmt.executeUpdate();
> ...
>
> I am using jConnect (syb102.zip).
>
> Thanks for any suggestion!

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

}


Hong Chen Posted on 1997-09-23 20:04:02.0Z
Message-ID: <342820B2.1B6E57CF@lincom-asg.com>
Date: Tue, 23 Sep 1997 15:04:02 -0500
From: Hong Chen <chenh@lincom-asg.com>
X-Mailer: Mozilla 4.01 [en] (WinNT; I)
MIME-Version: 1.0
To: Lance Andersen <lancea@sybase.com>
Subject: Re: PreparedStatement -- setxxx methods?
X-Priority: 3 (Normal)
References: <3427F8FC.D9EBD218@lincom-asg.com> <342808DB.4B3@sybase.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Newsgroups: sybase.public.jdbcconnect
Lines: 7
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:243
Article PK: 251995

>

Thank you very much. Actually, I saw your example from your answering
another people's question. But this time, I noticed that I wrote the
wrong parameter in using setBinaryStream(). So, after I corrected, it
works. Thanks again!