my dog learned polymorphism
The moose likes JDBC and Relational Databases and the fly likes Pass variables in INSERT statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Pass variables in INSERT statement" Watch "Pass variables in INSERT statement" New topic

Pass variables in INSERT statement

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
Please refer to the code below.i type some text into the textfiled which I retrieve and insert into database(msaccess).

In the createTable() method I want to insert the string myname which I retrieved from the textfield(in actionPerformed() method.

Is there a way to insert variables holding some string value by insert statement?

stmt.executeUpdate("INSERT INTO PHONEDIARY VALUES(myname,236821)");
this statement doesnt work though.Is there a way to pass variables in VALUES() ??

thanks in advance for your help
regs rajesh

[added code tags]
[ October 03, 2005: Message edited by: Jeanne Boyarsky ]
Craig Jackson
Ranch Hand

Joined: Mar 19, 2002
Posts: 405
One solution is to change the signature of your createTable method by passing the name as a parameter i.e something like this...

Another soluction is to create a prepareStatement of your sql.
mayank yadav

Joined: Sep 25, 2005
Posts: 11
well rajesh you have many options
1. you can use the package
2. by inserting the value in a variable

1. you can refer any tutorials
in this method
if tf is a text box where you are entring your value then
declare a string say String temp:
while actionperforming ....if e is event of clicking a button
if(e.getSource == b)

// now your value is in the String temp
*********jdbc conxs********

dont forget to write your sql connection commands..
i think this will solve your problem
I agree. Here's the link:
subject: Pass variables in INSERT statement
It's not a secret anymore!