MySQL TINYINT(1) mysteriously mapped to Java type Integer

这一生的挚爱 提交于 2019-12-13 18:40:02

问题


I'm stuck in a huge and old project (j2sdk 1.4.2, Tomcat 4.1.29, MySQL 5.0.51a) that I need to install a new development environment for at work.

I've got a MySQL DB that is accessed by my Tomcat, which treats request from my Java application. In that DB, some tables contain boolean values that are needed by my application.

So, in the application, a prepared statement is made, parameters are added to it, then the request is launched and the result set of this request is stored inside of a custom SQLResult object (that is part of a custom framework made by my company, can't do anything 'bout that - though, it is quite similar to a classic java.sql.ResultSet object).

Here's the problem: when the java application request some data that are stored in the DB as TINYINT(1), those data are returned to the java application as java.lang.Integer, not java.lang.Boolean, as I would like to.

Note: the JDBC connector version used by the Tomcat server is mysql-connector-java-3.0.11-stable.

What I tested so far (without result):

  • upgrade/downgrade the MySQL connector
  • added tinyInt1isBit=<true/1> as the end of my connection string
  • upgrade/downgrade the MySQL DB, always with the same data dump I have been given along the source code
  • plenty other things I couldn't even remember, because I tested so much things :-/

I'm pretty sure now that the problem comes from the MySQL JDBC connector used by the Tomcat server. Thus, when I changed the version of the connector, nothing else was working anymore (meaning, couldn't even connect a user).

Any ideas?

EDIT: I forgot to precise that, in another part of the java application, request for data stored as DECIMAL are returned as java.lang.String! This is also a major problem I have to solve, but I think the two are linked to the same cause.


回答1:


From Connector/J documentation

MySQL Type Name: TINYINT

Return value of GetColumnClassName: TINYINT

Returned as Java Class: java.lang.Boolean if the configuration property tinyInt1isBit is set to true (the default) and the storage size is 1, or java.lang.Integer if not.

Please note: or java.lang.Integer if not. Check the property tinyInt1isBit and possibly change it.

If you already did it try to restart the mysql server.




回答2:


So, after a complete week of work, I managed to found the solution. Beware, that was kind of stupid.

I was right when I thought the MySQL connector was the source of my problems. I decided to retry everything I tried until today to solve the situation, and so I slightly upgraded the connector (from v3.0.11 to v3.1.14). Then I re-launched the problematic DB requests and noticed an ERROR log I didn't see before in my Tomcat logs: the DB name specified was not correct (something like myDB\?autoReconnect=true...). Indeed, a \ had been wrongfully inserted before the connection arguments part.

I removed the guilty \ from the connection string, relaunched my Tomcat, and... tadaaa! My problems were solved!

However, I did test with the old MySQL connector (v3.0.11) and it still returns TINYINT(1) as java.lang.Integer and DECIMAL as java.lang.String. So I guess the client upgraded its MySQL connector on its production Tomcat without warning me.

Anyway, thank you all for your suggestions. Guess I'll read server logs more carefully when I debug in the future :-)



来源:https://stackoverflow.com/questions/33825156/mysql-tinyint1-mysteriously-mapped-to-java-type-integer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!