Sep 16th, 2011, 07:32 PM
PreparedStatement or JDBCTemplate Java enum translation
I am using Java. I am using functions like JDBCTemplate.query() and JDBCTemplate.update() with Object to hold the parameters referenced in the SQL strings. I have created several enums throughout my code and am using them in these queries/updates. The problem is that, I believe, it is attempting to serialize these objects which results in things like "\xAB\x00..." being inserted into the SQL statements instead of "running" or "stopped" like I want. What I want to do is force JDBCTemplate or PreparedStatement or whatever to, when it receives an enum in the parameter list, to call toString(). Is there a way to do this? Is there some configuration I can add to the startup routine to tell it to call toString() anytime it sees an enum? The other, obvious solution is to simply add toString() to each of the enums in the parameter list, but I would prefer it if I could avoid doing that and, instead, hint to JDBCTemplate to call toString() on its own.
Sep 19th, 2011, 04:38 PM
What I thought I wanted was Connection's setTypeMap, except that that only translates SQL user-defined types into Java user-defined types. MySQL doesn't support UDTs.
The other option would be to have an Interface that I could implement that would be built with a type like SQLData<T extends Types> that I could implement with functions like "T setValue()" which would translate my type to a database type and "static YourClass getValue(T sqlValue)" which would translate what was received from a query back into a my Java type, but I couldn't find anything like that. There is a SQLData type, but it is, again, only for UDTs of which MySQL doesn't have.
Last edited by jojenki; Sep 20th, 2011 at 12:24 PM.
Tags for this Thread