6 messages in com.mysql.lists.javaRE: insert variables
FromSent OnAttachments
Oana Radulescu29 Apr 1999 10:00 
Mark Matthews29 Apr 1999 10:06 
RITESH BISWAS29 Apr 1999 20:58 
mmat...@ecn.purdue.edu30 Apr 1999 06:29 
Ambrose Li30 Apr 1999 06:36 
Tim Endres30 Apr 1999 09:36 
Subject:RE: insert variables
From:Mark Matthews (mmat@ecn.purdue.edu)
Date:04/29/1999 10:06:32 AM
List:com.mysql.lists.java

-----Original Message----- From: or@columbia.edu [mailto:or@columbia.edu] Sent: Thursday, April 29, 1999 12:01 PM To: ja@lists.mysql.com Subject: insert variables

Hi!

I am having difficulty creating an insert statement in Java that would use variable values as the values parameters.

None of the following worked -- I would get either the name of the variable instead of the value or nothing at all:

String value1 = new String("blah");

insert into table values (value1); or insert into table values ('value1'); or insert into table values ("value1"); or insert into table values (\'value1\'); or insert into table values (\"value1\");

The context in which I need to use this is more complex than building an object string from a known value.

There are two ways to do this. You can do:

String SQL = "insert into table values ('" + value1 + "')"; Stmt.executeUpdate(SQL);

or PreparedStatement PStmt = Connex.prepareStatement("insert into table values (?)"; PStmt.setString(1, "Some Value"); PStmt.executeUpdate();

Java and JDBC do not do inline variable expansion like some variants of embedded SQL, or Perl, so you have to use one of the above two methods.

-Mark