473,387 Members | 1,455 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Java Strings and quote marks

Hi I am trying to do an insert query but for some reason although there
appears to be no exception thrown my data does not get into the database.

I will try to explain what I am doing

In SQL you can do the following sort of statement

INSERT INTO mytable (fname, lname, age) VALUES ('Joe', 'Bloggs', 20);

I realise this is not a java newsgroup but please forgive my use of java
specific syntax below.

I am trying to replicate this in Java by doing this

String query = "INSERT INTO mytable (fname, lname, age) VALUES ";
query = query + " ('" + method call that returns a string + "', '"
query = query + method call that returns a string + "', "
query = query + method call that returns an int + ");"

executeUpdate(query);

as far as I am concerned this should equate to the same thing as I wrote in
the first instance, but when I inspect the contents of the literal query it
appears as

INSERT INTO mytable (fname, lname, age) VALUES (\'Joe\', \'Bloggs\', 20);

^^^^^^^^^^^^^^^

Inserts backslashes

Is this why my Update is not working and if so what do I need to do.

Thanks in advance,
Eamon.
Nov 12 '05 #1
1 3994
I have no idea where the backslashes are comming from... very strange.
Are you building your string, then printing it out and it has
backslashes in it from nowhere?

Also, you should probably use a PreparedStatement for this, it will
handle everything for you:

String sqlQuery = "INSERT INTO mytable (fname, lname, age) VALUES (?, ?,
?)";
Connection c = //get connection
PreparedStatement ps = c.prepareStatement(sqlQuery);
ps.setString(1, /* get first name */);
ps.setString(2, /* get last name */);
ps.setString(3, /* get age */);
ps.executeUpdate();
ps.close();
c.close();

Best,
-Riyad

P.S.> Benefits of prepared statements are:
1) If you are doing this in a loop, they are a must.
2) If the driver/db support it, the statement is compiled and cached in
the DB so next time you execute the query, your new params are sent
across to DB and it just reexecutes the statement with new params and
doesn't need to recompile. Executing a statement requires it to be
compiled each time (I think).

Eamon Reyn wrote:
Hi I am trying to do an insert query but for some reason although there
appears to be no exception thrown my data does not get into the database.

I will try to explain what I am doing

In SQL you can do the following sort of statement

INSERT INTO mytable (fname, lname, age) VALUES ('Joe', 'Bloggs', 20);

I realise this is not a java newsgroup but please forgive my use of java
specific syntax below.

I am trying to replicate this in Java by doing this

String query = "INSERT INTO mytable (fname, lname, age) VALUES ";
query = query + " ('" + method call that returns a string + "', '"
query = query + method call that returns a string + "', "
query = query + method call that returns an int + ");"

executeUpdate(query);

as far as I am concerned this should equate to the same thing as I wrote in
the first instance, but when I inspect the contents of the literal query it
appears as

INSERT INTO mytable (fname, lname, age) VALUES (\'Joe\', \'Bloggs\', 20);

^^^^^^^^^^^^^^^

Inserts backslashes

Is this why my Update is not working and if so what do I need to do.

Thanks in advance,
Eamon.


Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ike | last post by:
I'm wondering, in the I18N'ized version of our application, if someone is running it, say, on a French system, where certain characters have accent marks, and such characters are saved into a MySQL...
2
by: sparks | last post by:
ok I was trying to do something real simple I thought Me.Clinic.DefaultValue = Me.Clinic.Value I got error either ! or . misused or not found...something like that I thought ok how can you not...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
7
by: Paul Connolly | last post by:
char *s = "Hello"; s = 'J'; puts(s); might print "Jello" in a pre-ANSI compiler - is the behaviour of this program undefined in any pre-ANSI compiler - or would it always have printed "Jello"...
12
by: steven acer | last post by:
hello, i have a java app that constructs an xml from a specific file format and vice versa. i've been asked to convert it to c++, but im not an expert in c++, actually im mere beginner you can...
17
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
34
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
6
by: Time Waster | last post by:
Java property files are dead simple: key1=val1 some.key2=val2 For simplicity on the Java side, I'd like to use these files from C as well (the C program and Java program must cooperate). ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.