File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC 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
Bookmark "Updation problem in JDBC" Watch "Updation problem in JDBC" New topic
Author

Updation problem in JDBC

Praveen Kanth
Greenhorn

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[])
{

try
{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
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)");

ps.executeUpdate();
*/

if(count == 0)
{
System.out.println("Updated");
}
else
{
System.out.println("No update");
}
stmt1.close();
con1.close();
}
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:

e.printStackTrace();


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

Joined: Aug 20, 2001
Posts: 1817

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:

INSERT INTO EMPLOYEE(EmpId) VALUES(565)


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

Joined: Oct 14, 2005
Posts: 18541
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Updation problem in JDBC