This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Inserting values in database Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Inserting values in database" Watch "Inserting values in database" New topic
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("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

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 ]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Inserting values in database
 
Similar Threads
JUnit Testing
unimplemented or unreasonable request at ps.execute()
Parsing a String
RE: reading Text file with the field positions
retrieving value from a text box