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.

prepared statement

2 posts in JDBC Connect (product renamed to JConnect) Last posting was on 1997-05-12 18:23:27.0Z
Joerg Pichler Posted on 1997-05-12 14:01:35.0Z
Message-ID: <337722BF.76BF@galaxy.fernuni-hagen.de>
Date: Mon, 12 May 1997 16:01:35 +0200
From: Joerg Pichler <pichler@galaxy.fernuni-hagen.de>
X-Mailer: Mozilla 3.01 (X11; I; SunOS 5.5 sun4m)
MIME-Version: 1.0
Subject: prepared statement
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:843
Article PK: 252595

Hi !

I have some problems with the prepared stements.

here are the code fragments:

PreparedStatement pstm = con.prepareStatement
("SELECT name FROM tab1 WHERE id = ?");

pstm.setString (1, theid);

ResultSet resSet = pstm.executeQuery ();

if (resSet.next ()) {
String name = pstm.getString (1);
}


Now the problem:
When I call the executeQuery()-Statement there will raise an exception:

SQLException: Syntaxerror near ?

It seems that the method setString does not replace the questionmark?

Can anyone help me ?

environment:
SQL Server 11.0
OpenClient/Server 10.0.3
JDK 1.1.1
JConnect Trial Version 2.0
Solaris 2.5


Lance Andersen Posted on 1997-05-12 18:23:27.0Z
Message-ID: <3377601F.4E@sybase.com>
Date: Mon, 12 May 1997 14:23:27 -0400
From: Lance Andersen <lancea@sybase.com>
X-Mailer: Mozilla 3.01Gold (X11; I; SunOS 5.5.1 sun4m)
MIME-Version: 1.0
To: Joerg Pichler <pichler@galaxy.fernuni-hagen.de>
CC: lancea
Subject: Re: prepared statement
References: <337722BF.76BF@galaxy.fernuni-hagen.de>
Content-Type: multipart/mixed; boundary="------------49275165653"
Newsgroups: sybase.public.jdbcconnect
Lines: 110
Path: forums-1-dub!forums-master.sybase.com!forums.powersoft.com
Xref: forums-1-dub sybase.public.jdbcconnect:842
Article PK: 252594

This works for me... see the trivial attached program and
also take a peek at $JDBC_HOME/sample/Prepare.java.

-Lance

Joerg Pichler wrote:
>
> Hi !
>
> I have some problems with the prepared stements.
>
> here are the code fragments:
>
> PreparedStatement pstm = con.prepareStatement
> ("SELECT name FROM tab1 WHERE id = ?");
>
> pstm.setString (1, theid);
>
> ResultSet resSet = pstm.executeQuery ();
>
> if (resSet.next ()) {
> String name = pstm.getString (1);
> }
>
> Now the problem:
> When I call the executeQuery()-Statement there will raise an exception:
>
> SQLException: Syntaxerror near ?
>
> It seems that the method setString does not replace the questionmark?
>
> Can anyone help me ?
>
> environment:
> SQL Server 11.0
> OpenClient/Server 10.0.3
> JDK 1.1.1
> JConnect Trial Version 2.0
> Solaris 2.5

--
===============================================================================
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!!!
===============================================================================

import java.net.*;
import java.sql.*;

class selectstring {

public static void main(String argv[] ) {
try {
String url = "jdbc:sybase:Tds:alder:6689/victimdb";
Class.forName("com.sybase.jdbc.SybDriver");
Connection _con = DriverManager.getConnection(url, "sa", "");

PreparedStatement stmt = _con.prepareStatement(
"select * from stringtest where f1 = ?");


stmt.setString(1, "Tonya");

// Send the query
System.out.println("Executing the query...");
ResultSet rs = stmt.executeQuery();
dispResultSet(rs);



stmt.close();
_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.printStackTrace();
ex = ex.getNextException ();
System.out.println ("");
}
}

catch (java.lang.Exception e) {
e.printStackTrace();
}
}
//-------------------------------------------------------------------
// dispResultSet
// Displays all columns and rows in the given result set
//-------------------------------------------------------------------

private static void dispResultSet (ResultSet rs)
throws SQLException
{
int i;

// Get the ResultSetMetaData. This will be used for
// the column headings

ResultSetMetaData rsmd = rs.getMetaData ();

// Get the number of columns in the result set

int numCols = rsmd.getColumnCount ();

// Display column headings

for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");

// Display data, fetching until end of the result set

while (rs.next ()) {

// Loop through each column, getting the
// column data and displaying

for (i=1; i<=numCols; i++) {
if (i > 1) System.out.print(",");
System.out.print(rs.getString(i));
}
System.out.println("");

// Fetch the next result set row

}
}
}