1

I've been kind of stuck with this for about a week now. I thought it would be just as easy in java as it is in PHP. Here goes:

After gathering data from the database and placing them in the JTable, I wanted to add a feature that could edit or delete a row in the JTable and in the database. Here's my code

private void getEmployees(String Query){
    DefaultTableModel dm = new DefaultTableModel(0, 0){
        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
   };       
    String header[] = new String[] {"<html><b>ID</b></html>", "<html><b>Name</b></html>", 
                                    "<html><b>Birthday</b></html>", "<html><b>Department</b></html>", 
                                    "<html><b>Date Hired</b></html>", "<html><b>Position</b></html>" ,
                                    "<html><b>Modify</b></html>"
                                    };
    dm.setColumnIdentifiers(header);
    empList.setModel(dm);
    empList.setRowSelectionAllowed(false);
    try {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/employee?zeroDateTimeBehavior=convertToNull","root","passwords");
        Statement stmt = conn.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery(Query);
        while(rs.next()){
            Vector<Object> data = new Vector<Object>();
            data.add(rs.getString("id"));
            data.add(""+rs.getString("lName")+", "+rs.getString("fName"));
            data.add(rs.getString("bday"));
            data.add(rs.getString("dept"));
            data.add(rs.getString("dateHire"));
            data.add(rs.getString("position"));
            data.add("Delete");
            dm.addRow(data);
        }
    } 
    catch (Exception e) {
        System.err.println("Got an exception! ");
        System.err.println(e.getMessage());
    } 
}

See the data.add("Delete")? I've tried placing and href tag linked to a function but apparently it doesn't work. I also tried putting in a void function there but it doesn't accept void. I also tried using JButton and an EventListener but I keep getting lost in my track. I'm sorry for the stupid questions. I'm still a beginner hoping to learn more.

iroNic
  • 11
  • 2
  • First, take a look at [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html). You could do something like [this](http://stackoverflow.com/questions/24625083/how-to-delete-a-row-from-jtable/24626105#24626105), but personally, I find it very limiting, as you can't delete groups of rows in a single, like you can do with something like [this](http://stackoverflow.com/questions/25070511/add-jbutton-to-each-row-of-a-jtable/25071138#25071138) – MadProgrammer Jul 10 '15 at 02:13
  • To remove the rows from the database, you could gather the id's from each row (when actioned) and simply use a `delete` statement batching the result into a single call – MadProgrammer Jul 10 '15 at 02:14
  • @MadProgrammer, Thank you very much for the reply. You're first suggestion (the limiting one) is perfect for the task I'm trying to implement. I think its possible to add an SQL query line to also delete the indexed row from the database. Unless I'm wrong though.. Also, would it be considered a bad coding convention to fit all functions inside one class? – iroNic Jul 10 '15 at 02:58
  • *"Also, would it be considered a bad coding convention to fit all functions inside one class?"* - that depends. Do you have a use case for re-usability to do you foresee the need to change the functionality (how the delete works for example)? – MadProgrammer Jul 10 '15 at 03:08
  • To be honest, I kinda skipped the use-case and went directly to the implementation. I had all these ideas and just started typing away without thinking about the structure of the program. Thanks for reminding me and thanks for the advice on the JTable. It actually set me back on track. (y) – iroNic Jul 10 '15 at 03:21

0 Answers0