Hello guys, i created a method that would set the value of my string so that when i pass it to another class, it would still contain the needed value:
This is the first class:
public Student getSelectedStudent(){
Student selectedStudent = new Student();
DefaultTableModel dtm = (DefaultTableModel) tblListOfStudents.getModel();
String studentNum = dtm.getValueAt(tblListOfStudents.getSelectedRow(), 0).toString();
String name = dtm.getValueAt(tblListOfStudents.getSelectedRow(), 1).toString();
String course = dtm.getValueAt(tblListOfStudents.getSelectedRow(), 2).toString();
String year= dtm.getValueAt(tblListOfStudents.getSelectedRow(), 3).toString();
selectedStudent.setStudentNumber(studentNum);
selectedStudent.setStudentName(name);
selectedStudent.setCourse(course);
selectedStudent.setYear(year);
return selectedStudent;
}
public void getNumber(){
System.out.print("was here: ");
EditStudent myEditStudent = new EditStudent();
System.out.print("nakalampas ");
Student num = new Student();
num.setStudentNumber(getSelectedStudent().getStudentNumber());
System.out.print("dumaan dito: "+num.getStudentNumber());
myEditStudent.receiver(num);
}
Button btnEdit = new JButton("Edit Student Data");
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(tblListOfStudents.getSelectedRow()==-1){
JOptionPane.showMessageDialog(frame, "No Student Data to Edit", "No Selected Student", JOptionPane.ERROR_MESSAGE);
}else{
System.out.print("1: ");
getNumber();
EditStudent myEditStudent = new EditStudent();
myEditStudent.editStudent();
//List<Student> studentList =writer.updater(selectedStudent);
//updateStudentListTable();
}
this is the second class:
public void actionPerformed(ActionEvent e) {
if(txtStudentName.getText().equals(" ")||txtAddYear.getText().equals(" ")||
Age.equals(" ") ||txtAddContactNumber.getText().equals(" ")||txtAddReligion.getText().equals(" ")||
txtAddBPlace.getText().equals(" ")| txtAddNationality.getText().equals(" ")||txtAddAddress.getText().equals(" ")||
txtAddParent.getText().equals(" ")||txtAddRelation.getText().equals(" ")||txtAddRContactNumber.getText().equals(" ")||
txtAddSYear.getText().equals(" ")||txtAddSection.getText().equals(" ") ||txtAddSchoolYear.getText().equals(" ")
){
noValue();
JOptionPane.showMessageDialog(null, "Please fill-up all the fields!");
}else{
Student myStudent = setAddNewStudent();
DataFileWriter myDataFileWriter = new DataFileWriter();
myDataFileWriter.updater(myStudent);
// GradeDBWriter myDbWriter = new GradeDBWriter();
// myDataFileWriter.updater(myNumber,myStudent);
// myDbWriter.StudentGradeWriter(myStudent);
Menu myMenu = new Menu();
myMenu.Menu();
frame.dispose();
}
}
public void receiver (Student no){
DataFileWriter myDataFileWriter = new DataFileWriter();
String number = no.getStudentNumber();
System.out.println("Nakarating sa receiver: "+ number);
myDataFileWriter.tangap(number);
}
this is the third class, where i needed the Student Number, so that i can create an edit feature in my program:
public List<Student> updater(Student editMe){
String url = "jdbc:mysql://localhost:3307/";
String dbName = "Student";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "1234";
List<Student> studentList = new ArrayList<Student>();
try {
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(url+dbName,userName,password);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM bio");
conn.close();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,"Cannot Connect to Database! Contact Ate Con, Rosselle , and Lonilie NOW! as in RIGHT NOW! ahihih!!");
}
return studentList;
}
public String tangap(String bilang){
System.out.println("nandito na siya YES!" +bilang);
return bilang;
}
}
I need to get the Student Number from the first class and send it to the second class so that i can compare it and update my databse, please help, thanks!