aspose file tools*
The moose likes JDBC 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
Bookmark "Pass variables in INSERT statement" Watch "Pass variables in INSERT statement" New topic
Author

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
Greenhorn

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

1. you can refer any tutorials
2.
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
then
if(e.getSource == b)

{
temp=tf.getSource();
// now your value is in the String temp
try
{
*********jdbc conxs********
stmt.executeUpdate("INSERT INTO PHONEDIARY(COLOUMN NAME)
VALUES('"temp"');
}
}




dont forget to write your sql connection commands..
i think this will solve your problem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pass variables in INSERT statement