Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vikas de
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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+ "')");
 
vikas de
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,
Thanx a lot....
vikas
vikasde@usa.net
 
vikas de
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,
Thanx a lot....
vikas
vikasde@usa.net
 
hema malini
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic