• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello friends,
Thanx a lot....
vikas
vikasde@usa.net
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic