This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes JDBC and the fly likes need help in executeUpdate() please Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "need help in executeUpdate() please" Watch "need help in executeUpdate() please" New topic
Author

need help in executeUpdate() please

Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Can someone help me understand why this program is not updating the EMP table? The record that I am retrieving(EMPNO=7934) using executeQuery() is displayed and its throwing Java.sql.SQLException: Invalid Column Index.

But I dont realise anywhere I indexed to a wrong column !! I am missing something. Can someone help me figuring out please...

Here is the code


//CODE

import java.sql.*;

class prepJdbc
{
public static void main(String [] args)
{


Connection con=null;

try
{
Class.forName("oracle.jdbc.OracleDriver");
con = DriverManager.getConnection("jdbcracle:thin:@localhost:1521rcl",

"scott","tiger");

String str = "select * from emp where empno=?";
PreparedStatement ps = con.prepareStatement(str);

ps.setString(1, args[0]);
ResultSet rs = ps.executeQuery();
if(rs.next())
{
System.out.println("Empno = "+rs.getString("empno"));
System.out.println("Ename = "+rs.getString("ename"));
System.out.println("Sal = "+rs.getFloat("sal"));

String update = "update emp set ename =? where empno=?";
PreparedStatement st = con.prepareStatement(update);
ps.setString(1,args[1]);
ps.setInt(2,Integer.parseInt(args[0])); //Probably the error is from here

int i = st.executeUpdate();
System.out.println(+i +" Rows updated");
con.commit();
rs.close();
ps.close();

}
else
System.out.println("No employee found with this number");
}
catch(Exception e)
{
try{
e.printStackTrace();
con.rollback();
}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
}
}
finally
{
try
{
if(con!=null)
con.close();
}
catch(SQLException se)
{ }

}

}
}
//END CODE

Thnak you


If you worry you cannot work... If you work you need not worry
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3450
    
  47

Carefully look at this snippet from your code:

You're creating a new PreparedStatement st, but setting parameters on another PreparedStatement ps. It has only one bind variable, but you're setting two.

Note: please use the code tags next time.
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
Yes.. Thank you. Dint notice that at all...

Code tags.. does it mean marking beginning and end of the CODE??
Thank you
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3450
    
  47

Sirisha Ghatty wrote:Code tags.. does it mean marking beginning and end of the CODE??

Yes, the way I did. Press the Code button while writing your post and put the code in between. It is much more readable. It should look like this:

[code]
-- Put the code here --
[/code]
Siri Naray
Ranch Hand

Joined: May 19, 2006
Posts: 105
ok. Will do that next time. Thank you
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

Martin Vajsar wrote:Carefully look at this snippet from your code:

You're creating a new PreparedStatement st, but setting parameters on another PreparedStatement ps. It has only one bind variable, but you're setting two.

Note: please use the code tags next time.


well spotted.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help in executeUpdate() please
 
Similar Threads
Returning a 0 value error
Applet and JDBC
SQL Syntax Error for MySQL but not sure where...
com.microsoft.sqlserver.jdbc.SQLServerException is troubling me can any help please
BMP, Jboss, and mysql configuring