Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Button to delete courses
- int row = tableCourse.getSelectedRow();
- DefaultTableModel model= (DefaultTableModel)tableCourse.getModel();
- String selected = model.getValueAt(row, 0).toString();
- //Option dialog to confirm the delete action
- if (row >= 0) { int dialogResult = JOptionPane.showConfirmDialog (null, "Would you like to delete this course?","Warning",JOptionPane.OK_CANCEL_OPTION);
- if(dialogResult == JOptionPane.YES_OPTION){
- model.removeRow(row);
- try {
- // Deleting courses here
- Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://eu-cdbr-azure-west-b.cloudapp.net:3306/hermas_august", "b88ca230425d11", "6c52fb01");
- PreparedStatement ps = (PreparedStatement) conn.prepareStatement("delete from courses where courseName='"+selected+"' ");
- ps.executeUpdate();
- }
- catch (Exception w) {
- JOptionPane.showMessageDialog(null, "Connection Error!");
- }
- }
- }
- }
- } );
- public void actionPerformed(ActionEvent arg0) {
- DefaultTableModel model = (DefaultTableModel)tableCourse.getModel();
- // get selected row index
- int selectedRowIndex = tableCourse.getSelectedRow();
- // get selected row data
- String courseName = model.getValueAt(selectedRowIndex, 0).toString();
- String courseId = model.getValueAt(selectedRowIndex, 1).toString();
- String semester = model.getValueAt(selectedRowIndex, 2).toString();
- String status = model.getValueAt(selectedRowIndex, 3).toString();
- String courseYear = model.getValueAt(selectedRowIndex, 4).toString();
- // get the entered data
- String NewName = JOptionPane.showInputDialog(null,"Enter The New name",courseName);
- String NewId = JOptionPane.showInputDialog(null,"Enter The New Id",courseId);
- String NewSemester = JOptionPane.showInputDialog(null,"Enter The New Semester",semester);
- String NewStatus = JOptionPane.showInputDialog(null,"Enter The New Status",status);
- String NewYear = JOptionPane.showInputDialog(null,"Enter The New Year",courseYear);
- // set the new data into tableCourse row
- model.setValueAt(NewName, selectedRowIndex, 0);
- model.setValueAt(NewId, selectedRowIndex, 1);
- model.setValueAt(NewSemester, selectedRowIndex, 2);
- model.setValueAt(NewStatus, selectedRowIndex,3);
- model.setValueAt(NewYear, selectedRowIndex,4);
- //courseQueries.addCourse(NewName, NewId, NewSemester, NewStatus, Integer.parseInt(NewYear));
- try {
- // Editing courses here
- // , status='"+NewStatus+"', courseYear='"+NewYear+"'
- Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://eu-cdbr-azure-west-b.cloudapp.net:3306/hermas_august", "b88ca230425d11", "6c52fb01");
- PreparedStatement ps = (PreparedStatement) conn.prepareStatement("Update courses set courseName='"+NewName+"', courseId='"+NewId+"', semester='"+NewSemester+"' where courseName='"+courseName+"'" );
- ps.executeUpdate();
- }
- catch (Exception w) {
- JOptionPane.showMessageDialog(null, "Connection Error!");
- }
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement