This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help in executeUpdate() please

 
Siri Naray
Ranch Hand
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Martin Vajsar
Sheriff
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3747
62
Chrome Netbeans IDE Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok. Will do that next time. Thank you
 
Wendy Gibbons
Bartender
Posts: 1107
Eclipse IDE Oracle VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic