What is the java.sql.Types equivalent for the MySQL TEXT?
When usi开发者_Python百科ng PreparedStatement
's setObject
method for a column of type TEXT (in a MySQL DB), what should the last parameter be?
For example, I know that this is ok for a VARCHAR column:
stmt.setObject(i, "bla", Types.VARCHAR);
where stmt
is a PreparedStatement
, and Types
is java.sql.Types
.
But if the DB column's type is TEXT, should I still use VARCHAR? or maybe BLOB, or CLOB?
The answer (YES, you are meant to use VARCHAR) can be found here:
http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html (updated outdated broken link)
and here is some info about the MYSQL TEXT
type
http://dev.mysql.com/doc/refman/5.0/en/blob.html
Why you don't use
stmt.setString ?
for text type ? http://download.oracle.com/javase/1.4.2/docs/api/java/sql/PreparedStatement.html#setString(int, java.lang.String)
There is no TEXT field here Oracle Doc for sql Types, but it may appear in the future (now you can use the info that size is 21845 as an implementation hardcoded trick - if you need to know later that this is TEXT field - the actual field size is something you choose and the rest of the text is memorized elsewhere by MySql)
So the first answer with "use VARCHAR as type" is working (probably the library make a setString call)
The answer "setString" is also working (preceded by a switch( field_type) if you want to take the decisions by yourself - I use this option in order to debug the code easier - the speed is the same as cost = switch + setString in both cases)
Thank you all for the links and confirmations !
精彩评论