0

I'm working on my homework and I can't complete one piece of my program ...

I have JTable class which makes table in my code ... i have to write method which takes information from sql database and writes it in list

method MUST look like:

public static List selectAnswers (int questionId) throws SQLException, IOException

following code is written by me:

public static List<AnswerRow> selectAnswers (int questionId) throws SQLException, IOException
{
    Connection veza = connectToDatabase();

    Properties query = new Properties();

    AnswersTableModel atm = new AnswersTableModel();

    String selectAnswers = query.getProperty("selectAnswers");

    PreparedStatement preparedStatement = veza.prepareStatement(selectAnswers);

    ResultSet rs = preparedStatement.executeQuery();

    List<AnswerRow> lista = new ArrayList<AnswerRow>();

    while(rs.next()){

        String answerText = rs.getString("answerText");
        boolean isRight = rs.getBoolean("answerRight");

                    ?????????????????????????????????????????????????


    }

    closeConnectionToDatabase(veza);

    return lista;
}

????? field is missing and i dont know what to write there to write information answeText and isRight into AnswerRow class , into AnswerTableModel, into list ...

Code which makes JTable (and is given to me and cannot be changed by my teacher) is here:

package hr.tvz.java.deveti.model;

import java.util.List;
import java.util.ArrayList;

import javax.swing.table.AbstractTableModel;

public class AnswersTableModel extends AbstractTableModel {

    private Object[][] answers;
    private String[] columnNames;

    public AnswersTableModel (String[] colNames){
        super();
        columnNames = colNames;
    }

    public AnswersTableModel() {
        super();
        this.columnNames = new String[AnswerRow.TABLE_COLUMNS];
        this.columnNames[0] = "Odgovor";
        this.columnNames[1] = "Točan/Netočan";
    }

    public java.lang.Class<?> getColumnClass(int columnIndex) {
        return getValueAt(0, columnIndex).getClass();
    }

    public int getColumnCount() {
        return AnswerRow.TABLE_COLUMNS;
    }

    public int getRowCount() {
        if (answers != null)
            return answers.length;
        else
            return 0;
    }

    public Object getValueAt(int row, int column) {
        return answers[row][column];
    }

    public String getColumnName(int column){
        return columnNames[column];
    }

    public void setValueAt (Object aValue, int rowIndex, int columnIndex){
        answers[rowIndex][columnIndex] = aValue;
    }

    public boolean isCellEditable (int rowIndex, int columnIndex){
        return true;
    }

    public void addNewRow(){
        Object[] o = new Object[] {"", false};
        if ((answers == null) || (answers.length == 0)) {
            answers = new Object[][] {o};
        }else{
            Object[][] answersTemp = new Object[answers.length + 1][AnswerRow.TABLE_COLUMNS];
            for (int i = 0; i < answers.length; i++)
                answersTemp[i] = answers[i];
            answersTemp[answersTemp.length - 1] = o;
            answers = answersTemp;
        }
    }

    public List<AnswerRow> getAnswerRows() {
        List<AnswerRow> list = new ArrayList<>();
        for (Object[] oRow : answers) {
            AnswerRow row = new AnswerRow();
            row.setAnswer((String) oRow[0]);
            row.setRight((boolean) oRow[1]);
            list.add(row);
        }

        return list;
    }

    public void setAnswerRows(List<AnswerRow> answerRows){
        if (answerRows.size() == 0 ) {
            this.answers = new Object[0][0];
            return;
        }
        this.answers = new Object[answerRows.size()][AnswerRow.TABLE_COLUMNS];
        for (int i = 0; i < answers.length; i++){
            answers[i][0] = answerRows.get(i).getAnswer();
            answers[i][1] = answerRows.get(i).isRight();
        }
        this.columnNames = new String[AnswerRow.TABLE_COLUMNS];
        this.columnNames[0] = "Odgovor";
        this.columnNames[1] = "Točno/Netočno";
    }

    public class AnswerRow {
        public static final int TABLE_COLUMNS = 2;
        private boolean isRight;
        private String answer;

        public AnswerRow(){
            answer = "";
            isRight = false;
        }

        public AnswerRow(String answer, boolean isRight){
            this.answer = answer;
            this.isRight = isRight;
        }

        public String getAnswer() {
            return answer;
        }

        public void setAnswer(String answer){
            this.answer = answer;
        }

        public boolean isRight(){
            return isRight;
        }

        public void setRight(boolean isRight){
            this.isRight = isRight;
        }
    }
}

Please help me .. thanks !

2
  • can you confirm that something happens to the query object between : Properties query = new Properties(); and String selectAnswers = query.getProperty("selectAnswers");? Otherwise, the problem is that your query object has no value for 'selectAnswers', and so there is no query to execute. Commented Jan 9, 2013 at 16:51
  • query is working fine (selectAnswers = SELECT * FROM QUIZ.ANSWERS WHERE questionId = ? , and SQL database is fine) ...the only problem is with writing those information (answerText , isRight) into list Commented Jan 9, 2013 at 16:53

2 Answers 2

2
List<AnswerRow> lista = new ArrayList<AnswerRow>();

    while(rs.next()){

        String answerText = rs.getString("answerText");
        boolean isRight = rs.getBoolean("answerRight");

        //Create AnswerRow instance and set values to it and Add it to list.
        AnswersTableModel .AnswerRow ansrow = atm.new AnswerRow();
        ansrow.setAnswer(answerText);
        ansrow.setRight(isRight);

       //Add it to list.
       lista.add(ansrow);
    }

One thing I am not sure is why you have AnswersTableModel and what you do with that.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the model really shouldn't be instantiated inside the selectAnswers method, but wherever the selectAnswer method is call, and then asign the method's result to the model through the setAnswerRows method.
i tried that way but then on AnswerRow ar = new AnswerRow(); line is error : "no enclosing instance of type AnswerTableModel is acessable. Must qualify the allocation with an enclosing instance of type AnswerTableModel(e.g. x.new A() where x is an instance of AnswerTableModel" AnswersTableModel class creates JTable with answers ...
Ok. AnswerRow is inner class, so syntax should be different, updated answer. Read this tutorial docs.oracle.com/javase/tutorial/java/javaOO/nested.html
0

Use public AnswerRow(String answer, boolean isRight) constructor to create object

AnswerRow ar=null;
while(rs.next()){

    String answerText = rs.getString("answerText");
    boolean isRight = rs.getBoolean("answerRight");
    ar=new AnswerRow(answerText, isRight);
    lista.add(ar);

}

1 Comment

i tried that way but then on ar = new AnswerRow(...); line is error : "no enclosing instance of type AnswerTableModel is acessable. Must qualify the allocation with an enclosing instance of type AnswerTableModel(e.g. x.new A() where x is an instance of AnswerTableModel"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.