• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

with Java: how to update a row in sybase with string having a single quote

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to update a row in sybase database using a stored procedure with a string value( ex: john O'reilly)having a single quote in it using java. I am using the statement object to execute the stored procedure and getting the syntax error near the single quote thrown by the sybase driver. And there is no problem to update a database row if I use without a single quote. I appreciate your help if you throw me some ideas.

Thanks
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use a PreparedStatement with ?s as substitution parameters to execute plain SQL, stored procedures/functions (CallableStatement for functions). It will automatically escape special characters based on the driver for you.

It's been a while since I've done hand-rolled JDBC, but it should be close to the following. The JavaDocs for PreparedStatement should demonstrate how to call a stored procedure/function.
 
sri pat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. Actually I am executing a stored procedure(huge) with several parameters getting dynamically as part of the SQL. Do I need to change the stored procedure all the way(variables to ? marks) or is there anyway I can change the input parameters which I am passing dynamically.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sri pat:
Actually I am executing a stored procedure(huge) with several parameters getting dynamically as part of the SQL. Do I need to change the stored procedure all the way(variables to ? marks) or is there anyway I can change the input parameters which I am passing dynamically.

Just to be clear, you don't need to change the stored procedure it self at all. It's only the String that you are giving to JDBC. Instead of building the SQL as it would look if you typed it into isql directly, you build a String with ?s where the parameters would be.

You can write (admittedly tricky and error-prone) Java code to build the query dynamically based on input parameters as you are now. After preparing the statement with the Connection, set the parameter values on it and execute it.

I just looked up the JavaDocs again and I was mistaken: you need to use CallableStatement for both stored procedures and functions. The queries look like this:My work machine appears to be down now, but if I can get to it tonight I'll post some code I wrote about nine months ago to call some stored procedures and functions. I do remember having to futz with the syntax a bit to get it to work.
[ December 22, 2004: Message edited by: David Harkness ]
 
sri pat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification but I am getting "java.sql.SQLException: JZ0SB: Parameter index out of range: 1." and used index starting from 1 even the parameter values(?) are really started from index(24) in stored procedure. I tried giving parameter index 24 in setString method, but still getting the same error saying: parameter index out of rance: 24"..any ideas
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, it's been a while, but I thought that the indices start at 1 (instead of 0 like you'd expect). But perhaps I'm mistaken. Did you try 0? If that doesn't work, try looking at the docs for the driver itself, if there are any. The example in the JavaDocs shows them starting at 1.

You could try calling resetParameters() before setting the parameter values, but that shouldn't affect a newly prepared statement/call.

I've only done this with Oracle, so I won't be much more help.
 
author & internet detective
Posts: 42055
926
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David,
Parameters definitely start with 1 in JDBC.

Sri,
What happens if you switch the stored proc to have parameters starting with 1 too?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can escape a single quote in Sybase by using two single quotes together. So try replacing your ' with ''

Francis
 
sri pat
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot for your help guys! Actually indexing starting from 1 is correct and working fine now but what happend is by some reason the CallableStatement object is not recognising the '?' symbols in SQL Query String while executing(used executeUpdate method)the query string. So I copy the dynamic values into local variables externally and passed it as a new parameters and it worked finally.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic