jeudi 21 avril 2016

How to insert in a sqlite database information that its in a button?

I am creating a database that saves questions. Each question has to be saved withs its Subject, Type, Level, Speed, Question, Answer. I know that with a textfield (question and answer) is done like this:

            pst.setString(5,contentQTextField.getText());
            pst.setString(6,answerQTextField.getText());

However, I have to access some information from buttons and I don't know how to do it.How the buttons look

For example, I have to save in the database of what subject the question corresponds to and for this, the user has to click one of the radioButtons shown in the previous pic. So the program should know which button is clicked and save that information in the database. Moreover, I don't know how to do it with normal buttons (also shown in the picture) and with a slider shown in the this picture.

This is how my code looks like:

    addQButton = new Button("ADD QUESTION");

    //Adding more that one action to a button;
    addQButton.addEventHandler(ActionEvent.ACTION, (e)-> {
        try
        {
            String query = "insert into Questions (Subject,Type,Level,Speed,Question,Answer) values (?,?,?,?,?,?)";
            PreparedStatement pst = connectionQuestions.prepareStatement(query);
            //First question mark = 1, second = 2, etc.
            //pst.setString(1,);
            //pst.setString(2,);
            //pst.setString(3,);
            //pst.setString(4,speedSlider);
            pst.setString(5,contentQTextField.getText());
            pst.setString(6,answerQTextField.getText());


            pst.execute();

            System.out.println("Data saved");

            pst.close();

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

        primaryStage.setScene(sceneAddQ);

    });
    addQButton.setPrefWidth(350.0);
    addQButton.setPrefHeight(50.0);
    addQButton.setStyle("-fx-font: 25 timesnewroman; -fx-base: #2EFE64; -fx-text-fill: black;");

    addQButtonPane.add(addQButton,3,21);

As you can see, I put in comments (//) the first 3 pst.setString because I don't know how to do it.

Thank you so much for you help in advance, I really appreciate it.

Aucun commentaire:

Enregistrer un commentaire