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);
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();
You're creating a new PreparedStatementst, but setting parameters on another PreparedStatementps. 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
posted
0
Yes.. Thank you. Dint notice that at all...
Code tags.. does it mean marking beginning and end of the CODE??
Thank you
Martin Vajsar wrote:Carefully look at this snippet from your code:
You're creating a new PreparedStatementst, but setting parameters on another PreparedStatementps. It has only one bind variable, but you're setting two.