abdou amer
abdou amer

Reputation: 849

Postgresql money type and java

I have defined a column in my postgresql database as money type, Let's say total. I'm inserting the data with a JAVA JSpinner uses double model, The inserting is done just perfectly, But when i want to get select the data stored in total column with

result.getBigDecimal("total");

I got an error;

org.postgresql.util.PSQLException: Mauvaise valeur pour le type BigDecimal : 0,00 €
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.toBigDecimal(AbstractJdbc2ResultSet.java:3012)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBigDecimal(AbstractJdbc2ResultSet.java:2400)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBigDecimal(AbstractJdbc2ResultSet.java:355)
    at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getBigDecimal(AbstractJdbc2ResultSet.java:361)
    at DAO.BonDachatDAO.findAll(BonDachatDAO.java:42)
    at VIEW.BonDeAchatUi.initComponents(BonDeAchatUi.java:255)
    at VIEW.BonDeAchatUi.<init>(BonDeAchatUi.java:123)
    at VIEW.BonDeAchatUi$3.run(BonDeAchatUi.java:483)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Upvotes: 2

Views: 2033

Answers (2)

fvu
fvu

Reputation: 32973

As explained on the MONEY documentation page you need to cast the output value in the query if you want to access its numeric value, like this:

select total::numeric, ...

However, there are several problems associated with the money type, better to just use plain numeric for storage of amounts.

Upvotes: 2

GleasonK
GleasonK

Reputation: 1022

I believe the issue is that BigDecimal cannot use a comma like that. If you can get it as a string in your code, consider parsing it with

NumberFormat.getNumberInstance(Locale.FRANCE).parse("0,00")

http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html

Upvotes: 0

Related Questions