Hi,
Were doing a hospital admin system where the hospital admin can enter a new patients details and save them to a ms access DB.
For this we created Data Access Objects.
We must have the ability to SELECT, ADD, DELETE and UPDATE the Db.
So far we have managed 3 but cannot figure out how to Update the DB.
Heres some of my ADD code:
public class PatientDAO
{
/** Creates a new instance of AdminADO */
public PatientDAO() {
}
public void select()
{
Connection con = null;
Patient patient = null;
Vector<Patient> patients = new Vector();
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=hospital.mdb";
con = DriverManager.getConnection(database,"","");
Statement stmt = con.createStatement();
ResultSet results = stmt.executeQuery("Select * from Patient");
while (results.next())
{
patient = new Patient();
patient.setPatientID(results.getString("Patient_ID"));
patient.setFName(results.getString("PF_Name"));
patient.setSName(results.getString("PS_Name"));
patient.setAddress(results.getString("Address"));
patient.setContact(results.getString("ContactNo"));
patient.setBP(results.getString("BloodPressure"));
patient.setTemp(results.getString("Temperature"));
patient.setBedNo(results.getString("BedNumber"));
patient.setWardID(results.getString("Ward_ID"));
patient.setDoctorID(results.getString("Doctor_ID"));
patient.setObservations(results.getString("Observations"));
patient.setMedicalFile(results.getString("MedicalFile"));
patients.add(patient);
}
for(Patient p :patients)
{
System.out.print(p.getPatientID()) ;
System.out.print(p.getFName()) ;
System.out.print(p.getSName()) ;
System.out.print(p.getAddress()) ;
System.out.println(p.getContactNo()) ;
System.out.print(p.getBP()) ;
System.out.print(p.getTemp()) ;
System.out.print(p.getBedNo()) ;
System.out.println(p.getWardID()) ;
System.out.println(p.getDoctorID()) ;
System.out.println(p.getObservations()) ;
System.out.println(p.getMedicalFile()) ;
}
}
catch(ClassNotFoundException e)
{
System.out.println(e.toString());
}
catch(SQLException e)
{
System.out.println(e.toString());
}
}
public void insert(Patient p)
{
Connection con1=null;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=hospital.mdb";
con1 = DriverManager.getConnection(database,"","");
PreparedStatement newpatient = con1.prepareStatement("INSERT INTO Patient(Patient_ID, PF_Name, PS_Name ,Address, ContactNo, BloodPressure, Temperature, BedNumber, Ward_ID, Doctor_ID, Observations, MedicalFile,)VALUES(?,?,?,?,?,?,?,?,?,?,?,?)");
newpatient.setString(1,p.getPatientID());
newpatient.setString(2,p.getFName());
newpatient.setString(3,p.getSName());
newpatient.setString(4,p.getAddress());
newpatient.setString(5,p.getContact());
newpatient.setString(6,p.getBP());
newpatient.setString(7,p.getTemp());
newpatient.setString(8,p.getBedNo());
newpatient.setString(9,p.getWardID());
newpatient.setString(10,p.getDoctorID());
newpatient.setString(11,p.getObservations());
newpatient.setString(12,p.getMedicalFile());
newpatient.executeUpdate();
System.out.println("***Patient Added***");
//CLOSE
con1.close();
}
catch(SQLException e)
{
System.out.println(e.toString());
}
catch(ClassNotFoundException e)
{
System.out.println(e.toString());
}
}
}
Then, here is the main method*****************
public class PatientTest {
/** Creates a new instance of Main */
public PatientTest() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
PatientDAO patientDAO = new PatientDAO();
Patient p = new Patient();
p.setAddress("Glanmire");
p.setContact("021-4822236");
p.setFName("Daniel");
p.setSName("Whelan");
p.setPatientID("1");
p.setWardID("Ward 1");
p.setTemp("56 degrees");
p.setBP("12");
p.setBedNo("Bed 2");
p.setDoctorID("1");
p.setObservations("Head Ache and Sore throat");
p.setMedicalFile("this is a medical file...input here");
patientDAO.insert(p);
patientDAO.select();
}
}
Any ideas on how i could update the DB now
PLEASE, PLEASE, please help
Thanks alot