lundi 25 avril 2016

Putting information that is in a sqlite database into a Label in javafx

I have a table called Questions in a SQlite database and one of the columns is called questions, where a question (using text) is stored. I want to randomly select one of this questions and print it in a label in javafx. My code looks like this:

    question = new Label();
    question.setPrefWidth(400.0);
    question.setPrefHeight(70.0);
    question.setStyle("-fx-font: 30 timesnewroman; -fx-base: #190707;-fx-text-fill: black;");




    startTestButton = new Button("START");
    startTestButton.addEventHandler(ActionEvent.ACTION, (e)-> {

        counterQuestions  = nQTextField.getText();
        System.out.println("Number of questions: " +  counterQuestions);

        if(groupSubjectS.getSelectedToggle() == SmathRB)
        {
            try
            {
                String love = new String();
                String query = "select * from Questions where question=? ";

                PreparedStatement pst = connectionQuestions.prepareStatement(query);

                pst.setString(1,love);


                //Execute the query
                ResultSet rs = pst.executeQuery();
                question.setText(love);

                rs.close();
                pst.close();
            }
            catch(Exception a)
            {
                System.out.println(a);
            }


            primaryStage.setScene(sceneMathTest);
        }
        else
        {
            primaryStage.setScene(scene);
        }

    });
    startTestButton.setPrefWidth(200.0);
    startTestButton.setPrefHeight(50.0);
    startTestButton.setStyle("-fx-font: 30 timesnewroman; -fx-base: #2EFE64; -fx-text-fill: black;");

    createTestButtonPane.add(startTestButton,5,6);

The part about trying to get the question is inside the try.

The label that I want to print with a question form the database is called question. So the program should get a random question from the database, put the text in the label called question and switch to the scene called sceneMathTest and that in this scene it appears the label with the question.

What is been done in my code is probably completely wrong, I have no idea how to do it so I would really appreciate the help. Thank you so much in advance.

Aucun commentaire:

Enregistrer un commentaire