0

Hello,
I'm new in java programming, i have insert amounts in database in this Format exp : 1.100.200,00 but the subtraction of this amount give false result .
here is part of my code

  DecimalFormat df = new DecimalFormat();
                                                 DecimalFormatSymbols symbols = new DecimalFormatSymbols();
                                                 symbols.setDecimalSeparator(',');
                                                 symbols.setGroupingSeparator(' ');
                                                 df.setDecimalFormatSymbols(symbols);

                                         statement = connection.prepareStatement("INSERT INTO Client ( Nom , Prenom,  Num , Nom_projet, Bloc  ,Date_de_naissance ,  Adresse , Type, N°_loge , prix_loge, Verse_total, Etage,Verse_restant,Verse_1,Verse_2,Verse_3,Verse_4,Verse_5,Verse_6,Verse_7,Date_1,Date_2,Date_3,Date_4,Date_5,Date_6,Date_7,Surface) VALUES(? ,? ,? ,? ,? ,? ,? ,? ,? ,? , ?,?,(?-?),?,0,0,0,0,0,0,?,0,0,0,0,0,0,?) ");

                                         String v = comboBox_1.getSelectedItem().toString();

                                         statement.setString(1, nom.getText());
                                         statement.setString(2, prenom.getText());
                                         statement.setString(3, num.getText());
                                         statement.setString(4,comboBox.getSelectedItem().toString());
                                         statement.setString(5,idbloc.getText());
                                         statement.setString(6, datenaissance.getText());
                                         statement.setString(7, adresse.getText());
                                         statement.setString(8,comboBox_1.getSelectedItem().toString());
                                         statement.setString(9, n.getText());
                                         statement.setString(10, textField.getText());//je veux l'inserer en BigDecimal
                                         statement.setString(11, versement.getText());//je veux l'inserer en BigDecimal
                                         statement.setString(12, comboBox_2.getSelectedItem().toString());
                                         statement.setString(13, textField.getText());//je veux l'inserer en BigDecimal
                                         statement.setString(14, versement.getText());//je veux l'inserer en BigDecimal
                                         statement.setString(15, versement.getText());
                                         statement.setString(16, date.getText());
                                         statement.setString(17, surf.getText());

                                          statement.executeUpdate();
2
Contributors
7
Replies
39
Views
7 Months
Discussion Span
Last Post by Bensaber
0

If you want to convert a String (eg from a JTextField) to a BigDecimal you can use one of BigDecimal's constructors...

String s = " 1234.56";
BigDecimal bd = new BigDecimal(s);

0

But the string change, its depends of what i want to insert in textField

0

Yes, that was just an example of how to use the constructor. You get the String from the JTextField, then pass it to the constructor, eg

BigDecimal vbd = new BigDecimal( versement.getText() );

0

i have follow your advice and i get this error

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException
at java.math.BigDecimal.<init>(Unknown Source)
at java.math.BigDecimal.<init>(Unknown Source)
at java.math.BigDecimal.<init>(Unknown Source)
1

That's what you get if the text field does not contain a correctly formatted decimal number ie it must contain exactly one decimal point and at least one digit and nothing else. This is a problem if you are using French formatting as in your original post.
You can do some simple processing on the user input to make it compatible using String's replace method. I'll let you work out the details of the code, but here are the steps:

get the text from the field into a string
replace all the blanks with nothing, eg s= s.replace(" ", "");
replace all the "." with nothing (thousands separators are not supported)
replace the comma decimal point with a full stop (English standard)

NB If you are using a French locale then you may not need to change the decimal point - try it and see.

When this works then put it in a little method so you can just call that for each text field

Finally, you should catch that exception in your code because it will always be triggered if the user types incorrect input (eg O instead of 0). You should catch it, issue a sensible error message to the user and wait for new input.

0

Thnaks for replying, i have to do that but i coudn't, i didn't understand

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.