samedi 25 avril 2015

Displaying Database Images from ArrayList - Java

Apologies if this is a duplicate question, maybe I have been looking for the wrong question. I have several tables in an SQLite database. One holds questions as text, the other holds questions as BLOB images. I have an ArrayList that will display the questions, and when clicking the "next" button.

public static List<Questions> BindList(){
        try{
            //Get a connection
            ...
            ResultSet nextresultset = statement.executeQuery("SELECT * FROM questions WHERE categoryID = 3");
            //Declare ArrayList
            List<Questions> list = new ArrayList<Questions>();

            while(nextresultset.next()){
                Questions ques = new Questions(nextresultset.getString("questionDesc"), 
                        nextresultset.getString("possibleAns1"), 
                        nextresultset.getString("possibleAns2"), 
                        ...
                        );
                list.add(ques);
            }
            return list;
        }catch(SQLException ex1){
            System.out.println(ex1.toString());
        }
        return null;
    }


    public void ShowQuestions(int index){
        textAreaQuestionDesc2.setText(BindList().get(index).getQuestion());
        textAreaHint1.setText(BindList().get(index).getHint1());
    }

Button:

if("Next Question".equals(e.getActionCommand())){
        pos++;
        if(pos < BindList().size()){
            ShowQuestions(pos);
        }
        else{
            pos = BindList().size()-1;
            ShowQuestions(pos);
            //btnNextQuestion.setEnabled(false);

        }
    }

Questions ArrayList:

public class Questions {
private String question;
private String possibleAns1;

public Questions(String _question, String _possibleAns1){
    this.question = _question;
    this.possibleAns1 = _possibleAns1;
    ...
}

public String getQuestion(){
    return this.question;
}

public String getPossibleAns1(){
    return this.possibleAns1;
}

}

Note: I'm able to pull the first question from the resultset and display the images but this is executing a query when a button is clicked

                if(resultset.next()){
                byte[] imageQues = resultset.getBytes("questionDesc");
                imageQuestion = new ImageIcon(imageQues);
                lblQuestionDesc.setIcon(imageQuestion);
                ... 
            }

How would one go about creating a similar ArrayList to display images from the images table, just like the questions from the questions? It might be a simple question but I've not been able to see any similar examples, although it may not be the right way to go about it. Any help and/or pointers in the right direction would be most appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire