I m retriving data from database and want to store that data in the form of objects in my arraylist that furthermore can be use in display custom tables.
I have tried the solutions from prev related queries but couldn't solve my problem.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
public class Appointment {
public int no;
public String patient;
public String doctor;
public String disease;
public String scheduleDay;
public void setNo(int no){
this.no = no;}
public void setPatient(String patient){
this.patient = patient;}
public void setDoctor(String doctor){
this.doctor = doctor;}
public void setDisease(String disease){
this.disease = disease;}
public void setScheduleDay(String scheduleDay){
this.scheduleDay = scheduleDay;}
public int getNo(){
return no;}
public String getPatient(){
return patient;}
public String getDoctor(){
return doctor;}
public String getDisease(){
return disease;}
public String getScheduleDay(){
return scheduleDay;}
public Appointment(){
no = 0;
patient = "not set";
doctor = "not set";
disease = "not set";
scheduleDay = "not set";
}
public Appointment(int no, String patient, String doctor, String
disease, String scheduleDay){
setNo(no);
setPatient(patient);
setDoctor(doctor);
setDisease(disease);
setScheduleDay(scheduleDay);
}
public Appointment (Appointment ap){
no = ap.no;
patient = ap.patient;
doctor = ap.doctor;
disease = ap.disease;
scheduleDay = ap.scheduleDay;
}
public ArrayList<Appointment> appointmentArr() throws
ClassNotFoundException, SQLException {
String url = "jdbc:ucanaccess://D:/MC140202550/MC140202550.accdb";
Connection con = DriverManager.getConnection(url);
Statement st = con.createStatement();
String sql = "select * from Patient, Doctor where Patient.Disease =
Doctor.Specialization";
ResultSet rs = st.executeQuery(sql);
ArrayList<Appointment> appointments = new ArrayList<>();
while(rs.next()==true){
Appointment ap = new Appointment();
ap.setPatient(rs.getString(1));
ap.setDoctor(rs.getString(6));
ap.setDisease(rs.getString(4));
ap.setScheduleDay(rs.getString(8));
int i = 1;
ap.setNo(i);
i++;
appointments.add(ap);
System.out.println(ap.getNo() + " " + ap.getPatient() + " " +
ap.getDoctor() + " " + ap.getDisease() + " " + ap.getScheduleDay());
}
st.executeQuery(sql);
con.close();
st.close();
st.close();
return appointments;
}
}
I want to call this Appointment class in another main class and print the objects stored in array.
public class Main{
public static void main(String args[]){
Main main=new Main();
Appointment ap = new Appointment();
}
}