File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes how to pass the value of a variable into a db?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to pass the value of a variable into a db??" Watch "how to pass the value of a variable into a db??" New topic
Author

how to pass the value of a variable into a db??

vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
Dear Friends,
does somebody know to to pass the value from a variable into a database....
please see the code...the database saves only the variable name not the value...it saves only one time...
import java.sql.*;
import sun.jdbc.odbc.*;
import java.io.*;
public class showfile1
{
public static void main(String args[]) throws IOException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc:thisdb");
Statement stmt = con.createStatement();
for(int i=0;i<=10;i++)
{
int result = stmt.executeUpdate("INSERT INTO Table1 VALUES('i')");
stmt.close();
con.close();
{

}
catch(Exception e)
{
e.printStackTrace(System.out);
}

}
}

Thanx a lot...


[This message has been edited by vikas de (edited February 15, 2001).]
martin samm
Ranch Hand

Joined: Nov 09, 2000
Posts: 31
if understand you correctly, you could use a prepared statement, which contains a place holder for your value (using '?' notation) and then within your loop you can rebind a value to the placeholder and execute the statement.

------------------
martin samm
m_sam@rroom.net


martin samm<BR>m_sam@rroom.net
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Hello ,
You are treating the variable i as a constant. Instead you should write the query as :
int result=stmt.executeUpdate("INSERT INTO Table1 VALUES('" + i+ "')");

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
hello friends,
Thanx a lot....
vikas
vikasde@usa.net
vikas de
Ranch Hand

Joined: Feb 07, 2001
Posts: 39
hello friends,
Thanx a lot....
vikas
vikasde@usa.net
hema malini
Greenhorn

Joined: Jan 11, 2001
Posts: 5
Hai vikas de,
You declare the i value as integer then why you are passing the value as String .
You pass the value with in single quote, it take it as string,with out using single qutote it take it as int.
So,Please try this coding,
----------------------------------------------------------------
import java.sql.*;
import sun.jdbc.odbc.*;
import java.io.*;
public class showfile1
{
public static void main(String args[]) throws IOException,SQLException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc dbc:thisdb");
Statement stmt = con.createStatement();
for(int i=0;i<=10;i++)
{
int result = stmt.executeUpdate("INSERT INTO Table1 VALUES("+i+")");
}
stmt.close();
con.close();
}
catch(Exception e)
{
e.printStackTrace(System.out);
}
}
}
-----------------------------------------------------------------
I run this program it give the correct answer,
Thank u
Hema

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to pass the value of a variable into a db??