Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Interfaces jdbc

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-16-2008, 01:00 AM
Chris Gamache
 
Posts: n/a
Default Filtering out commas for DOUBLE

I was having a problem with JDBC expecting a DOUBLE from a PostgreSQL
function which returnes "money" ... My first inclination was to return
an OTHER, but PgJDBC balks that it expects java.sql.Types=8 (DOUBLE)
but I set it to 1111 (OTHER) ... There was (seemingly) no way around
that.

So, I decided to attack the root of the problem: PostgreSQL is
returning "1,234.00" when it is executing the function that returns
money and JDBC won't budge on it's expectation that it should be
DOUBLE. The comma in the return string is confusing
Double.parseDouble, so I filter out the commas before parseDouble is
called. My hack got me over the hump, but it makes me uneasy to apply
this patch to a codebase that I am not intimately familiar with. Can
you propose a different way around this problem, a more complete
patch, or the blessing that this will not adversely affect the normal
function of the driver?

From org.postgresql.jdbc2.AbstractJdbc2ResultSet:

public static double toDouble(String s) throws SQLException
{
if (s != null)
{
try
{
s = s.trim();
s = s.replace(",",""); //filter out the commas
return Double.parseDouble(s);
}
catch (NumberFormatException e)
{
throw new PSQLException(GT.tr("Bad value for type
{0} : {1}", new Object[]{"double",s}),

PSQLState.NUMERIC_VALUE_OUT_OF_RANGE);
}
}
return 0; // SQL NULL
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-16-2008, 01:00 AM
Kris Jurka
 
Posts: n/a
Default Re: Filtering out commas for DOUBLE



On Tue, 4 Mar 2008, Chris Gamache wrote:

> I was having a problem with JDBC expecting a DOUBLE from a PostgreSQL
> function which returnes "money" ... My first inclination was to return
> an OTHER, but PgJDBC balks that it expects java.sql.Types=8 (DOUBLE)
> but I set it to 1111 (OTHER) ... There was (seemingly) no way around
> that.


Someone else was just complainging about this recently:

http://archives.postgresql.org/pgsql...2/msg00202.php

> So, I decided to attack the root of the problem: PostgreSQL is
> returning "1,234.00" when it is executing the function that returns
> money and JDBC won't budge on it's expectation that it should be
> DOUBLE. The comma in the return string is confusing
> Double.parseDouble, so I filter out the commas before parseDouble is
> called. My hack got me over the hump, but it makes me uneasy to apply
> this patch to a codebase that I am not intimately familiar with. Can
> you propose a different way around this problem, a more complete
> patch, or the blessing that this will not adversely affect the normal
> function of the driver?


That's one approach, but you'd also have to apply it to other getXXX
methods if you really wanted to be able to treat money as a numeric type.
On the previous thread I was advocating taking this special case code out
of the common path instead of adding more. That seemed to be your first
inclination as well (wanting to get back Types.OTHER).

> s = s.replace(",",""); //filter out the commas
>


Your actual implementation is using a JDK 1.5 method while the driver
(probably unnecessarily) supports back to 1.2. So this may work for your
environment, but isn't a general solution. Other than that it looks fine
as long as you aren't concerned that values like "1,,,,,0" will no be
considered valid doubles.

Kris Jurka

--
Sent via pgsql-jdbc mailing list (pgsql-jdbc@postgresql.org)
To make changes to your subscription:
http://mail.postgresql.org/mj/mj_www...tra=pgsql-jdbc

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 06:43 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145