• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble with INSERT

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing a utility using JDBC to make scripts to import data from one db to another. But some of the text contains chars that mess up the SQL statement, specifically '\'', '\n', and '\r'; Right now, I am stripping them out of the row text. Can I wrap them somehow so I don't lose the data? I am using MS SQL2000.

An example...

INSERT INTO Customer
(Name, Description, [Action])
VALUES ('MyName', 'MyDesc', 'MyAction's')

Notice Action has an 's
[ December 15, 2005: Message edited by: M Burke ]
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a PreparedStatement. In fact, it's a good idea to prefer PreparedStatements in general...

And it's easy to do:
Since you're probably inserting multiple rows, check out the addBatch() and executeBatch() methods -- batching it is not much harder.
[ December 15, 2005: Message edited by: Jeff Albrechtsen ]
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most JDBC implementations allow you to specify two quotes '' (not a double quote) to represent a single quote. The other characters though, such as line breaks has to be cut out though although some drivers may ignore it.
 
M Burke
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Scott. The '' works. I'm still not sure what to do with \n \r.

Jeff, I am makeing a utility that makes .sql scripts. Your solution is better, but I want to move data betweem db's that are not connected in any way.
 
Scott Selikoff
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use String class indexOf() and replace()/replaceAll() to help you remove characters such as:

String newString = oldString.replaceAll("\n", "");
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Burke:

Jeff, I am makeing a utility that makes .sql scripts. Your solution is better, but I want to move data betweem db's that are not connected in any way.



Could you explain why PreparedStatements are less relevant when moving data *between* databases? (By the way, you may have problems moving dates or timestamps, and the same PreparedStatement approach solves that problem as well... And did I mention that PreparedStatement can be faster, too, because they can be precompiled...)
[ December 16, 2005: Message edited by: Jeff Albrechtsen ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic