Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1.  
  2.  
  3. //Button to delete courses
  4.  
  5.  
  6.  
  7. int row = tableCourse.getSelectedRow();
  8. DefaultTableModel model= (DefaultTableModel)tableCourse.getModel();
  9.  
  10. String selected = model.getValueAt(row, 0).toString();
  11.  
  12. //Option dialog to confirm the delete action
  13. if (row >= 0) { int dialogResult = JOptionPane.showConfirmDialog (null, "Would you like to delete this course?","Warning",JOptionPane.OK_CANCEL_OPTION);
  14. if(dialogResult == JOptionPane.YES_OPTION){
  15.  
  16. model.removeRow(row);
  17.  
  18. try {
  19.  
  20. // Deleting courses here
  21.  
  22. Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://eu-cdbr-azure-west-b.cloudapp.net:3306/hermas_august", "b88ca230425d11", "6c52fb01");
  23. PreparedStatement ps = (PreparedStatement) conn.prepareStatement("delete from courses where courseName='"+selected+"' ");
  24. ps.executeUpdate();
  25. }
  26. catch (Exception w) {
  27. JOptionPane.showMessageDialog(null, "Connection Error!");
  28. }
  29. }
  30. }
  31.  
  32. }
  33.  
  34. } );
  35.  
  36.  
  37.  
  38.  
  39. public void actionPerformed(ActionEvent arg0) {
  40.  
  41. DefaultTableModel model = (DefaultTableModel)tableCourse.getModel();
  42.  
  43. // get selected row index
  44. int selectedRowIndex = tableCourse.getSelectedRow();
  45.  
  46. // get selected row data
  47. String courseName = model.getValueAt(selectedRowIndex, 0).toString();
  48. String courseId = model.getValueAt(selectedRowIndex, 1).toString();
  49. String semester = model.getValueAt(selectedRowIndex, 2).toString();
  50. String status = model.getValueAt(selectedRowIndex, 3).toString();
  51. String courseYear = model.getValueAt(selectedRowIndex, 4).toString();
  52.  
  53. // get the entered data
  54. String NewName = JOptionPane.showInputDialog(null,"Enter The New name",courseName);
  55. String NewId = JOptionPane.showInputDialog(null,"Enter The New Id",courseId);
  56. String NewSemester = JOptionPane.showInputDialog(null,"Enter The New Semester",semester);
  57. String NewStatus = JOptionPane.showInputDialog(null,"Enter The New Status",status);
  58. String NewYear = JOptionPane.showInputDialog(null,"Enter The New Year",courseYear);
  59.  
  60. // set the new data into tableCourse row
  61. model.setValueAt(NewName, selectedRowIndex, 0);
  62. model.setValueAt(NewId, selectedRowIndex, 1);
  63. model.setValueAt(NewSemester, selectedRowIndex, 2);
  64. model.setValueAt(NewStatus, selectedRowIndex,3);
  65. model.setValueAt(NewYear, selectedRowIndex,4);
  66.  
  67.  
  68.  
  69. //courseQueries.addCourse(NewName, NewId, NewSemester, NewStatus, Integer.parseInt(NewYear));
  70. try {
  71.  
  72. // Editing courses here
  73. // , status='"+NewStatus+"', courseYear='"+NewYear+"'
  74. Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://eu-cdbr-azure-west-b.cloudapp.net:3306/hermas_august", "b88ca230425d11", "6c52fb01");
  75. PreparedStatement ps = (PreparedStatement) conn.prepareStatement("Update courses set courseName='"+NewName+"', courseId='"+NewId+"', semester='"+NewSemester+"' where courseName='"+courseName+"'" );
  76. ps.executeUpdate();
  77.  
  78. }
  79. catch (Exception w) {
  80. JOptionPane.showMessageDialog(null, "Connection Error!");
  81. }
  82.  
  83. }
  84.  
  85. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement