There is a note in all the execute type methods in Statement Interface in the java docs that says
Note:This method cannot be called on a PreparedStatement or CallableStatement.
But why it is So? I mean PreparedStatement
is a sub-interface of Statement
, then why we can't do so? In fact I tried and it worked.
ConnectionSetup setCon = new ConnectionSetup();
// My own class for increasing readability.
try{
setCon.loadDriver("com.mysql.jdbc.Driver");//loadDriver calls Class.forName.
con = setCon.setUpConnection("jdbc:mysql://localhost:3306/odi batsman", "root", "");//setUpConnection asks DriverManager for Connection Object.
PreparedStatement ps = con.prepareStatement(query);
ps.execute(query);
}
catch(ClassNotFoundException | SQLException e){
e.printStackTrace();
}
It worked perfectly fine, the record was entered successfully in the database in spite of the fact that I called execute method (which takes a string as input and inherited from the interface Statement
) from PreparedStatement
. So whats going on?
EDIT I am just asking that it is written in java docs that we cannot call execute(string)
from PreparedStatement
but as it is sub-interface of Statement
, why we can't?