0

I'm working on a desktop application using javaFX, I'm using scene builder version 11 to create my interface that contains TextFields and ChoiceBox, my inputs are supposed to be set in Arabic. The problem is when I retrieve the text from the TextFiled or the ChoiceBox and print it in the console it shows characters like that "بننلنلن" and it also generates a problem to store the inputs in database, this is the SQLException: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'بننلنلن,نبلنبنلب,2021-06-30,أمر جزائي,تم التبليغ

When I tried another project without scene builder I got the Arabic outputs correctly, could scene bulider be the problem's origin?

here is my fxml file:

    <?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.DatePicker?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>

<AnchorPane id="AnchorIns" prefHeight="529.0" prefWidth="523.0" stylesheets="@newinsc.css" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="newInsc.NewInscController">
   <children>
      <VBox fx:id="boxContainer" layoutX="139.0" layoutY="99.0" prefHeight="273.0" prefWidth="245.0">
         <children>
            <HBox alignment="CENTER_RIGHT" prefHeight="42.0" prefWidth="245.0">
               <children>
                  <TextField fx:id="firstNameTxtField" alignment="CENTER_RIGHT" />
                  <Label alignment="CENTER_RIGHT" contentDisplay="RIGHT" prefHeight="17.0" prefWidth="71.0" stylesheets="@newinsc.css" text="الاسم" />
               </children>
            </HBox>
            <HBox alignment="CENTER_RIGHT" prefHeight="42.0" prefWidth="245.0">
               <children>
                  <TextField fx:id="lastNameTxtField" alignment="CENTER_RIGHT" />
                  <Label prefHeight="17.0" prefWidth="71.0" text="اللقب" />
               </children>
            </HBox>
            <HBox alignment="CENTER_RIGHT" prefHeight="42.0" prefWidth="245.0">
               <children>
                  <ChoiceBox fx:id="docTypeChoice" prefWidth="150.0" />
                  <Label prefHeight="17.0" prefWidth="71.0" text="نوع التبليغ" />
               </children>
            </HBox>
            <HBox alignment="CENTER_RIGHT" prefHeight="42.0" prefWidth="245.0">
               <children>
                  <DatePicker fx:id="dateField" prefHeight="25.0" prefWidth="149.0" />
                  <Label prefHeight="17.0" prefWidth="71.0" text="التاريخ" />
               </children>
            </HBox>
            <HBox alignment="CENTER_RIGHT" prefHeight="42.0" prefWidth="245.0">
               <children>
                  <ChoiceBox fx:id="tablighCase" prefWidth="150.0" />
                  <Label prefHeight="17.0" prefWidth="71.0" text="حالة التبليغ" />
               </children>
            </HBox>
            <HBox alignment="CENTER" onMouseClicked="#cancelBtnClicked" prefHeight="46.0" prefWidth="245.0">
               <children>
                  <Button fx:id="cancleBtn" mnemonicParsing="false" onMouseClicked="#cancelBtnClicked" text="الغاء">
                     <HBox.margin>
                        <Insets right="16.0" />
                     </HBox.margin>
                  </Button>
                  <Button fx:id="saveInscBtn" mnemonicParsing="false" onMouseClicked="#registerBtnClicked" text="تسجيل" />
               </children>
            </HBox>
         </children>
      </VBox>
   </children>
</AnchorPane>

and this here is my controller file:

package newInsc;

import DB.dbConnection;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 * FXML Controller class
 *
 * @author asus
 */
public class NewInscController implements Initializable {

    @FXML
    private ChoiceBox<String> docTypeChoice;
    @FXML
    private ChoiceBox<String> tablighCase;
    @FXML
    private DatePicker dateField;
    @FXML
    private Button saveInscBtn;
    @FXML
    private TextField firstNameTxtField;
    @FXML
    private TextField lastNameTxtField;
    @FXML
    private Button cancleBtn;
    
    private Connection c;
    private Statement s;
    @FXML
    private VBox boxContainer;

 
    @Override
    public void initialize(URL url, ResourceBundle rb) {

     docTypeChoice.getItems().add("أمر جزائي");
     docTypeChoice.getItems().add("حكم جزائي" );
     docTypeChoice.getItems().add("تكليف بالحضور" );
     docTypeChoice.getItems().add("قرار جزائي" );
     
     tablighCase.getItems().add("تم التبليغ");
     tablighCase.getItems().add("ترك اشعار");
     tablighCase.getItems().add("عدم التمكن من التبليغ");
     
     
    }    

    @FXML
    private void cancelBtnClicked(MouseEvent event) {
        Stage s = (Stage) cancleBtn.getScene().getWindow();
        
    }

    @FXML
    private void registerBtnClicked(MouseEvent event) {
        try {
            String query;
            String fname = firstNameTxtField.getText();
            String lname = lastNameTxtField.getText();
            LocalDate date = dateField.getValue();
            String docType = docTypeChoice.getValue();
            String state = tablighCase.getValue();
           
            query = "insert into infotable values " + fname + "," +  lname + ","  + date+ ","+ docType+"," +state ;
            dbConnection dbc = new dbConnection();
            s = dbc.createConnection().createStatement();
            s.execute(query);
            s.close();
        } catch (SQLException ex) {
            Logger.getLogger(NewInscController.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }


    
}
Majid
  • 51
  • 7
  • This could be of some help https://stackoverflow.com/q/14091011/6708879. Basically what you are experiencing is the wonderfull world of character encoding. This tutorial could also be of help: https://www.baeldung.com/java-string-encode-utf-8. I don't know where you are coding but look also at the character encoding of the IDE. Eclipse uses cp1252 in window by default – MissingSemiColon Jul 20 '21 at 13:31
  • @MissingSemiColon thanks for your answer, I use netbeans IDE, actually the used encoding in my fxml file is UTF-8 but I don't understand why the output coming from a textfield or choicebox isn't displayed in Arbaic letter. – Majid Jul 20 '21 at 14:01

1 Answers1

0

I could find a solution, before I run the program I first compile it with F9 key then I run it with a key combination SHIFT + F6

Majid
  • 51
  • 7