Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting values in database

 
venkatesh pendharkar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • 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("jdbcdbc: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();
}
}
 
Kamesh Rao
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic