| Author |
Inserting values in database
|
venkatesh pendharkar
Ranch Hand
Joined: Apr 29, 2006
Posts: 104
|
|
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 dbc: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
Joined: Dec 24, 2006
Posts: 35
|
|
|
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
Joined: Apr 29, 2006
Posts: 104
|
|
|
I tried get System.out.println(c.getAutoCommit()); & its priting true ..any other suggestions?
|
 |
Kamesh Rao
Ranch Hand
Joined: Dec 24, 2006
Posts: 35
|
|
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 ]
|
 |
 |
|
|
subject: Inserting values in database
|
|
|