Announcement Announcement Module
Collapse
No announcement yet.
PreparedStatement or JDBCTemplate Java enum translation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

    Thank you,

    John

  • #2
    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, 12:24 PM.

    Comment

    Working...
    X