Exception in thread "main" java.lang.ClassCastException: org.apache.derby.client.am.Statement cannot be cast to java.beans.Statement
at CollegeLibrary.NotRegistered.DBStatement(NotRegistered.java:114)
at CollegeLibrary.NotRegistered.<init>(NotRegistered.java:85)
at CollegeLibrary.NotRegistered.main(NotRegistered.java:91)
Java Result: 1
在这些部分内有错误
ResultSet SelectQuery(Statement st) {
ResultSet rs = null;
try {
rs = st.executeQuery("select * from APP.STUDENT");
} catch (SQLException ex) {
Logger.getLogger(NotRegistered.class.getName()).log(Level.SEVERE, null, ex);
}
return rs;
}
void InsertQuery(Statement st) {
try {
if(_txtFname.getText().length()>0 & _txtLname.getText().length()>0& _txtAddress.getText().length()>0&_txtPhone.getText().length()>0&_txtDob.getText().length()>0&_txtSTcode.getText().length()>0){
st.executeQuery("insert into APP.STUDENT values ('"+_txtFname.getText()+"','"+_txtLname.getText()+"','"+_txtAddress.getText()+"','"+_txtPhone.getText()+"','"+_txtDob.getText()+"','"+_txtSTcode.getText()+"')");
}else{
System.out.println("please fill up the form");
}
} catch (SQLException ex) {
Logger.getLogger(NotRegistered.class.getName()).log(Level.SEVERE, null, ex);
}
}
void UpdateQuery(Statement st) {
try {
if(_txtFname.getText().length()>0 & _txtLname.getText().length()>0){
st.executeUpdate("update APP.STUDENT SET USERNAME='"+_txtFname.getText()+
"', PASSWORD='"+_txtLname.getText()
+"' where USERNAME='"+_oldData+"'");
}else{
System.out.println("please fill up the form");
}
} catch (SQLException ex) {
Logger.getLogger(NotRegistered.class.getName()).log(Level.SEVERE, null, ex);
}
}
void DeleteQuery(Statement st) {
try {
if(_txtFname.getText().length()>0 & _txtLname.getText().length()>0& _txtAddress.getText().length()>0&_txtPhone.getText().length()>0&_txtDob.getText().length()>0&_txtSTcode.getText().length()>0){
System.out.println(" ");
st.executeUpdate("delete from APP.STUDENT where USERNAME='"+_oldData+"'");
}else{
System.out.println("please fill up the form");
}
} catch (SQLException ex) {
Logger.getLogger(NotRegistered.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:8)
java.lang.ClassCastException:org.apache.derby.client.am.Statement无法强制转换为java.beans.Statement
我认为您的代码中包含错误的import
语句。
java.sql.Statement(你想在这里)和java.beans.Statement(这是完全不同的东西)。
在大多数情况下,您仍然希望使用java.sql.PreparedStatement,这也是名称冲突的步骤。不要将文字值直接放在SQL中,请使用绑定变量。
答案 1 :(得分:4)
您应该导入
java.sql.statment not java.bean.statement