Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Inserting values in database

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi can anyone tell wheather there is anything wrong in the following code because when im trying to run it,no values are going in the database although code is not throwing any exception.
I am using ms access databse & there is a table by the name "contacts" in which im trying to save the values through this program.
Please help me into this....




import java.sql.*;
class Db
{
static Connection c;
static Statement st,st1;
static ResultSet rs;
Db()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
c=DriverManager.getConnection("jdbc:odbc:venkat");
st=c.createStatement();
}
catch (Exception e1)
{
e1.printStackTrace();
}
int sqtn=12345;
String senname= "ABCD";
String semail= "hijk";
String sphone= "022-2232";

try
{
System.out.println("sdf");
st.execute("insert into contacts values("+sqtn+",'"+senname+"','"+semail+"','"+sphone+"')");
}
catch (Exception e2)
{
e2.printStackTrace();
}
}
public static void main(String[] args)
{
Db d=new Db();
}
}
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to see whether autoCommit is on or not, by calling Connection objects getAutoCommit() method.If it is off set it.
 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried get System.out.println(c.getAutoCommit()); & its priting true ..any other suggestions?
 
Kamesh Rao
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I dont see any problem in the above, but why dont you try these options

1. A wild try
Statement st=con.createStatement("query");
st.execute();
2. Use a PreparedStatement which is a more cleaner approach.

[ December 27, 2006: Message edited by: Kamesh Rao ]
[ December 27, 2006: Message edited by: Kamesh Rao ]
reply
    Bookmark Topic Watch Topic
  • New Topic