Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Updation problem in JDBC

 
Praveen Kanth
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Paul Clapham
Sheriff
Pie
Posts: 20768
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic