-1

I have a project in JAVA using MySQL database. My database has "Answers" table and I need to allow users to vote (+1 or -1) if answer is good or bad. User can vote only once for one answer.

EDIT: Here is my code for display answers for choosen Question:

private void showAnswer(String chosenString) {
        editAnswer = new JButton("Edit");
        deleteAnswer = new JButton("Delete");
        editAnswer.addActionListener(this);
        deleteAnswer.addActionListener(this);
        JPanel commentsPanel = new JPanel();
        answerFrame = new JFrame();
        answerFrame.setLayout(new BorderLayout());
        JLabel questionText = new JLabel(chosenString);
        arrowNorth = new BasicArrowButton(BasicArrowButton.NORTH);
        arrowSouth = new BasicArrowButton(BasicArrowButton.SOUTH);
        arrowNorth.addActionListener(this);
        arrowSouth.addActionListener(this);
        answerFrame.add(questionText, BorderLayout.NORTH);
        tableInsideModel = new ResultSetTableModel(null);
        tableInside = new JTable(tableInsideModel);
        JScrollPane tableScroll = new JScrollPane(tableInside);
        answerFrame.add(tableScroll);
        String query = "select a_id, answer, nickname, a.add_date from answers a inner join users on au_id=u_id where aq_id="
                + "(select q_id from questions where question='"+chosenString+"')";
        sendInsideQuery(query);

        tableInside.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e){
                if(e.getClickCount()==2){
                    JTable target = (JTable) e.getSource();
                    int row = target.getSelectedRow();
                    doubleClickValue = (int) tableInsideModel.getValueAt(row, 0);
                    String doubleClickText = (String) tableInsideModel.getValueAt(row, 1);
                    doubleClickWindow(doubleClickValue, doubleClickText);
                }
            }
            public void mouseReleased(MouseEvent er){               
                int r = tableInside.rowAtPoint(er.getPoint());
                if(r>= 0 && r<tableInside.getRowCount()){
                    tableInside.setRowSelectionInterval(r, r);
                }else{
                    tableInside.clearSelection();
                }
                int rowindex = tableInside.getSelectedRow();
                if(rowindex<0)
                    return;
                if(er.isPopupTrigger() && er.getComponent() instanceof JTable){
                    popupInside = new JPopupMenu();

                    popupInside.add(editAnswer);
                    popupInside.add(deleteAnswer);
                    popupInside.show(er.getComponent(), er.getX(), er.getY());
                }
            }
        });

        JButton buttonReturn = new JButton("Back");
        buttonReturn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                answerFrame.setVisible(false);
            }
        });

        JButton buttonAddAnswer = new JButton("Add Answer");
        buttonAddAnswer.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                newAnswer(chosenString);
            }
        });


        commentsPanel.add(buttonAddAnswer,BorderLayout.CENTER);
        commentsPanel.add(buttonReturn,BorderLayout.CENTER);
        commentsPanel.add(arrowNorth, BorderLayout.WEST);
        commentsPanel.add(arrowSouth, BorderLayout.WEST);
        answerFrame.add(commentsPanel,BorderLayout.SOUTH);

        answerFrame.setVisible(true);   
        answerFrame.setSize(1000, 500);
        answerFrame.setLocationRelativeTo(null);
    }

Here is my problem: How can I force MySQL database to "remember" which user voted for which answer? Is there any option to add arrows (up and down) to all JTable rows? Or should i make only 2 arrows (as above) on the bottom of GUI and with TableModel.getValueAt(); choose a Answer to vote for?

  • I should have been more careful. Please show us an example of what you have tried. A [mcve] is better than simply providing the entire code. Here there is a lot of GUI not useful for the question. – AxelH Jan 12 '17 at 13:56
  • I didn't try anything, because I don't have idea how could I solve this – najdzion15 Jan 12 '17 at 13:59
  • I did it. I know how to add it, but i don't know how to protect this project for multi-voting one question by one user – najdzion15 Jan 12 '17 at 14:07
  • _How can I force MySQL database to "remember" which user voted for which answer?_ You can tell MySql to **insert** the information. I guess you just add or substract one to a value in the question. Well you could simply use a table to store those vote `ID_USER,ID_QUESTION, VOTE (-1 | 1)` then you just need to do a join to know if there is already a vote for a specific question for this user. You are the one telling to the Database what to do... PS: Your question still need improvment because this is not clear... see [ask] – AxelH Jan 12 '17 at 14:11
  • I got it, but how can I check, if user didn't vote for answer yet? – najdzion15 Jan 12 '17 at 14:13
  • Ok, so this was not clear at all, see my answer to see how to know if a user as voted – AxelH Jan 12 '17 at 14:21

1 Answers1

1

Since the question is not clear for me, I used this comment from OP to focus on a problem.

I got it, but how can I check, if user didn't vote for answer yet?


If you want to know who vote for a question, you just need to store those information in a table.

This table would join the reference of the question and the user with the vote.

Some pseudo code because I don't know enough about your tables.

CREATE TABLE vote (
    id_question numeric,
    id_user     numeric,
    vote        bit, -- 0 : -1 and  1 : +1
)

Then, you just need to use a left join on this table to see if there is a value or not:

SELECT id_question, id_user, question_value, vote
    FROM question q
    LEFT JOIN vote v ON  q.id      = v.id_question
                     AND v.id_user = ?

Using a left join would give vote to null if there is no vote yet.

Just need to add a unique constraint on vote(id_question, id_user) to prevent the duplication of vote in the database and the same controle in your DAO.

AxelH
  • 14,325
  • 2
  • 25
  • 55