Am coding a college council voting system for my school in netbeans and mysql saver. i have
populated jtables containing cadidates (jtable1 for president, jtable2 for vice president etc) and i have put a jbutton so that after sellecting candidents of choice one from each jtable, you click the jbutton to cast a vote for each selected candidate and submitte an update into the mysql tables showing current results for every candidate. please help me out, am required to present this project
next week. thank you very much in advance.

Dani AI

Generated

This is a concise, practical pattern you can drop into the button handler that expands on ’s suggestion (detect choice + run UPDATE) and keeps the thread’s concerns in mind (see on homework). The idea: 1) require one selection per office, 2) collect each selected candidate’s ID (keep an integer ID column in the TableModel, hidden in the view), 3) open a DB transaction, increment each candidate’s votes with a single prepared statement (or batch), commit, then refresh/lock the UI.

Example action flow (extract IDs, then update atomically):

// collect selected candidate IDs (repeat per table)
List<Integer> ids = new ArrayList<>();
int row = jTable_PG.getSelectedRow();
if (row != -1) {
  int modelRow = jTable_PG.convertRowIndexToModel(row);
  ids.add(Integer.parseInt(jTable_PG.getModel().getValueAt(modelRow, 0).toString()));
}

// DB update in one transaction
String sql = "UPDATE candidates SET votes = votes + 1 WHERE candidate_id = ?";
try (Connection conn = getConnection();
     PreparedStatement ps = conn.prepareStatement(sql)) {
  conn.setAutoCommit(false);
  for (int id : ids) {
    ps.setInt(1, id);
    ps.addBatch();
  }
  ps.executeBatch();
  conn.commit();
} catch (SQLException ex) {
  // rollback, show error, log
}

Practical tips and cautions:

  • Use ListSelectionModel.SINGLE_SELECTION for each JTable so users can only pick one.
  • If tables are sortable/filterable, call convertRowIndexToModel before getValueAt.
  • Put the numeric candidate ID in a model column that can be hidden (do not rely on displayed name).
  • Use UPDATE ... = votes + 1 to avoid lost increments under concurrency; wrap in a transaction and handle rollback.
  • Prevent double-voting by recording voter_id in a separate table or disabling the vote button after success; refresh the tables after commit.

If you post the exact jButton2ActionPerformed attempt you tried, this can be tailored to your code (connection helper, column indices, table names).

Recommended Answers

All 6 Replies

Exactly what help do you need?

@ JamesCherrill-I need java source code to write in the actionPerformed method of the jbutton so that if its clicked; a vote is cast for every candidate selected from the different jtables.

If you need Java code for your homework then you have to write it yourself; nobody here will do your homework for you. If you make an effort, and get stuck, post what you have done so far, and people here will help you.

so, you want us to provide you with the code you'll present as your project at college?
does the word plagiarism ring any bells?

//THIS IS PART OF WHAT I HAVE DONE TO POPULATE JTABLES WITH MYSQL DATA IN NETBEANS,(I USED ARRAYS).NOW AM STUCK ON HOW TO ENABLE A VOTE CASTE AND SUBMISSION TO THE DATABASE USING  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                         }
I HAVE SEARCHED THE INTERNET AND COULDN'T JUST FIND ANYTHING SIMILAR TO WHAT AM LOOKING FOR.I HOPE SOMEONE WILL HELP EVEN JUST WITH AN IDEA ON HOW TO GO ABOUT THIS.
//SCREEN SHOT
 ![VotePane.PNG](/attachments/large/4/b6d36c9801da9f29a155e3f6883a372d.PNG "align-center") 

//SOURCE CODE

public PopulateCandidate() {
        initComponents();
        populateJTable();
        populateJTableVP();
        populateJTableSG();
        populateJTableTG();
        populateJTableAS();
        populateJTableRS();
        populateJTableSS();
        populateJTablES();
        populateJTablCM();
    }

public void populateJTable(){
        MyQuery mq = new MyQuery();
        ArrayList<Product2> list = mq.BindTable();
        String[] columnName = {"Candidate_ID","Name","Image",};
        Object[][] rows = new Object[list.size()][6];
        for(int i = 0; i < list.size(); i++){

            rows[i][0] = list.get(i).getName();
            rows[i][1] = list.get(i).getID();


            if(list.get(i).getMyImage() != null){   
             ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getMyImage()).getImage()
             .getScaledInstance(150, 120, Image.SCALE_SMOOTH) );      
             rows[i][2] = image;
            }
            else{
                rows[i][2] = null;
           }   
        }

        TheModel model = new TheModel(rows, columnName) {};
       jTable_PG.setModel(model);
       jTable_PG.setRowHeight(120);
       jTable_PG.getColumnModel().getColumn(2).setPreferredWidth(150);
    }

public void populateJTablCM(){
        MyQuery mq = new MyQuery();
        ArrayList<Product2> list = mq.BindTable8();
        String[] columnName = {"Candidate_ID","Name","Image",};
        Object[][] rows = new Object[list.size()][6];
        for(int i = 0; i < list.size(); i++){

            rows[i][0] = list.get(i).getName();
           rows[i][1] = list.get(i).getID();

            if(list.get(i).getMyImage() != null){

             ImageIcon image = new ImageIcon(new ImageIcon(list.get(i).getMyImage()).getImage()
             .getScaledInstance(150, 120, Image.SCALE_SMOOTH) );   

            rows[i][2] = image;
            }
            else{
                rows[i][2] = null;
            }

        }

        TheModel model = new TheModel(rows, columnName) {};
     jTable_CM.setModel(model);
      jTable_CM.setRowHeight(120);
     jTable_CM.getColumnModel().getColumn(2).setPreferredWidth(150);
    }


//VOTER CASTS A VOTE FOR THEIR FAVOURITE CANDIDATES FROM EACH JTABLE

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
}

In your action event handler:
see which candidate they voted for
run an SQL UPDATE command to update the datbase accordingly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.