I had a problem converting the result of a MySQL query to a Java class when using SUM.
When executing a simple SUM in MySQL
SELECT SUM(price) FROM cakes WHERE ingredient = 'chocolate';
Since price
is an integer, it seems that SUM
sometimes returns a string, and sometimes an integer, depending on the version of the JDBC driver.
Obviously, the server tells the JDBC driver that the SUM
result is a string, and the JDBC driver sometimes "conveniently" converts this to an integer. (see Mark Matthews explanation ).
The Java code uses BeanInfo and Introspection to automatically fill in the (list) bean (s) with the query result. But this obviously cannot work if the data types differ between the servers on which the application is deployed.
I don't care if I get a string or an integer, but I would like to have the same data type or at least know in advance which data type I will receive.
Is there any way to know which data type MySQL SUM
will return from Java code? Or does anyone know of any better way to handle this?
source share