package oracle.otnsamples.sqlj.inheritance;
import java.sql.Connection; import java.sql.SQLException; import java.math.BigDecimal;
import oracle.sqlj.runtime.Oracle;
public class InheritanceSample { private PersonFrame gui; private Connection conn;
public InheritanceSample() { try { gui = new PersonFrame(this); } catch(Exception ex) { gui.putStatus(" Error : Couldn't Initialize frame , "+ex.toString()); } }
public static void main(String args[]) { InheritanceSample inheritanceSample = new InheritanceSample();
inheritanceSample.dbConnect();
inheritanceSample.displayPersonDetails(); }
void dbConnect() { try{ gui.putStatus(" Connecting to Database ...."); Oracle.connect(this.getClass(), "../../../../Connection.properties"); gui.putStatus(" Connected to Database ..."); }catch(Exception ex){ System.out.println("Error while Connecting to the database: "+ ex.toString()); System.exit(0); } }
public void dispatchEvent (String eventName) { if (eventName.equals("GET PERSON DETAILS")) selectPersonDetails(); if (eventName.equals("SHOW PERSON DETAILS")) showPersonDetails(); if (eventName.equals("SET PERSON DETAILS")) setPersonDetails();
if (eventName.equals("EXIT")) exitApplication(); }
private void displayPersonDetails() { String ssnNo = null; String fullName = null; String objType = null;
Student st=new Student(); ParttimeStudent ps=new ParttimeStudent(); Teacher t=new Teacher(); Person personObj=null; objectIter tempIter=null; try { while(true){ if(tempIter.endFetch())break;
if(personObj instanceof ParttimeStudent) objType=" P ";
else if(personObj instanceof Student) objType=" S ";
else if(personObj instanceof Teacher) objType=" T ";
else
objType=" O ";
ssnNo = personObj.getSsn();
fullName = personObj.getFirstname() + " " + personObj.getLastname();
gui.addToJTable(ssnNo,fullName,objType); } gui.putStatus(" Select a person ..."); } catch(Exception ex) { gui.putStatus("Error while Displaying : "+ex.toString()); } finally { try { tempIter.close(); } catch(SQLException sqe){ gui.putStatus("Error while Displaying : "+sqe.toString()); } } }
private void selectPersonDetails() {
Person personObj = getSelectedPerson(); if(personObj != null) { try { if(personObj instanceof ParttimeStudent) { ParttimeStudent pstudent = (ParttimeStudent)personObj; gui.viewPerson.showParttime_studentDetails( pstudent.getpersondetails()); }
else if(personObj instanceof Student) { Student student = (Student)personObj; gui.viewPerson.showStudentDetails(student.getpersondetails()); }
else if(personObj instanceof Teacher) { Teacher teacher = (Teacher)personObj; gui.viewPerson.showTeacherDetails(teacher.getpersondetails()); } gui.setVisible(false); } catch(Exception ex) { gui.putStatus("Error while Selecting: "+ex.toString()); } } }
private void showPersonDetails() { Person personObj = getSelectedPerson(); if(personObj != null) gui.editPerson.showPersonDetails(personObj); }
private void setPersonDetails() { Person personObj = preparePerson(); try { String ssn = personObj.getSsn(); } catch(Exception ex) { gui.putStatus("Error while updating person: "+ex.toString()); } }
private Person preparePerson() { EditPersonDetailsFrame EditFrame = gui.editPerson;
Person personObj = gui.personObj;
if(personObj instanceof ParttimeStudent) { ParttimeStudent pstudent = (ParttimeStudent)personObj;
String Grade = EditFrame.txtGrade.getText(); String Yearofcompletion = EditFrame.txtYearofCompletion.getText(); String Coursestaken = EditFrame.txtCoursesTaken.getText(); BigDecimal Hours = new BigDecimal(EditFrame.txtHours.getText()); String Batchcode = EditFrame.txtBatchCode.getText(); String Starttime = EditFrame.txtStartTime.getText();
try {
personObj = pstudent.setpersondetails(Grade,Yearofcompletion, Coursestaken,Hours,Batchcode,Starttime); } catch(Exception e){ gui.putStatus("Error while setting parttime student: "+e.toString()); } } else if(personObj instanceof Student) { Student student = (Student)personObj; String Grade = EditFrame.txtGrade.getText(); String Yearofcompletion = EditFrame.txtYearofCompletion.getText(); String Coursestaken = EditFrame.txtCoursesTaken.getText(); try {
personObj = student.setpersondetails(Grade,Yearofcompletion,Coursestaken); } catch(Exception e){ gui.putStatus("Error while setting student details : "+e.toString()); } } else if(personObj instanceof Teacher) { Teacher teacher = (Teacher)personObj; BigDecimal Experience = new BigDecimal(EditFrame.txtExperience.getText()); BigDecimal Salary = new BigDecimal(EditFrame.txtSalary.getText()); String Courseshandled = EditFrame.txtCoursesHandled.getText(); try { personObj = teacher.setpersondetails(Experience,Salary,Courseshandled); } catch(Exception e){ gui.putStatus("Error while setting teacher details: "+e.toString()); } } return((Person)personObj); } private Person getSelectedPerson() { Person personObj = null; String ssn = getSSNo(); objectIter tempIter=null;
if(!ssn.equals("NS")){ try { } catch(Exception ex) { gui.putStatus("Error while Selecting person : "+ex.toString()); } finally { try { tempIter.close(); } catch(SQLException sqe){ gui.putStatus("Error while Displaying : "+sqe.toString()); } } } return(personObj); }
private String getSSNo() { int selectedRow = gui.personTable.getSelectedRow(); if(selectedRow==-1) { gui.putStatus(" Select a Person from the Table ..."); return("NS"); } String objType=(String)gui.tableModel.getValueAt(selectedRow,2); if(objType.equals(" O ")) { gui.putStatus("The selected person is some other sub type of person ."); return("NS"); }
String ssnNo = (String)gui.tableModel.getValueAt(selectedRow,0);
return(ssnNo); }
private void exitApplication(){ try { Oracle.close(); } catch (SQLException ex){ gui.putStatus(" Error while Closing the connection: "+ex.toString()); } System.exit(0); } }
|