wood burning stoves
The moose likes JDBC and Relational Databases and the fly likes Updation problem in JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Updation problem in JDBC" Watch "Updation problem in JDBC" New topic

Updation problem in JDBC

Praveen Kanth

Joined: Dec 08, 2005
Posts: 1
This is the following code which i have i am not able to update into DB it enters into the catch block. I think there is problem in createStatement line. Could any one help me to rectify this error.

I too tried another option which i mentioned in comments.

import java.io.*;
import java.sql.*;
public class HRupdate
public static void main(String args[])


Connection con1= DriverManager.getConnection("jdbc dbc:Ex2","","");
String str = "insert into table(EmpID)" + "values(123)";
Statement stmt1 = con1.createStatement();
int count = stmt1.executeUpdate(str);
PreparedStatement ps=con1.prepareStatement("insert into table(EmpID) values(565)");


if(count == 0)
System.out.println("No update");
catch(Exception e)
System.out.println("Error HRadd");
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Here's a handy tip that you can apply to all your code. In a catch block,
never, never, never, never, never do something like System.out.println("Error HRadd");
At the very least dump the exception so that you can see what went wrong:


There is no emoticon for what I am feeling!
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Take a look at your error message that you receive, and then take a look at your code. Do you really have a table named "table"?

You probably want your statement to read more like:


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19845

If it turns out that the error is something to do with the syntax of that SQL, it's probably because this statementdidn't leave a blank space between the column list (EmpID) and the word "values". Was it really necessary to do that string concatenation instead of just using a single string literal? Makes it much easier to make mistakes like that.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Updation problem in JDBC
It's not a secret anymore!