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.setGroupingSeparator(' ');

                                         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(6, datenaissance.getText());
                                         statement.setString(7, adresse.getText());
                                         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());


Recommended Answers

All 7 Replies

any answer

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);

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

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() );

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)

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.