MySQL TEXT 的 java.sql.Types 等价物是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6772594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:12:29  来源:igfitidea点击:

What is the java.sql.Types equivalent for the MySQL TEXT?

javamysqlsqljdbctypes

提问by Eran Zimmerman

When using PreparedStatement's setObjectmethod for a column of type TEXT (in a MySQL DB), what should the last parameter be?

当对 TEXT 类型的列(在 MySQL 数据库中)使用PreparedStatement'ssetObject方法时,最后一个参数应该是什么?

For example, I know that this is ok for a VARCHAR column:

例如,我知道这对于 VARCHAR 列是可以的:

stmt.setObject(i, "bla", Types.VARCHAR);

stmt.setObject(i, "bla", Types.VARCHAR);

where stmtis a PreparedStatement, and Typesis java.sql.Types.

其中stmtPreparedStatementTypesjava.sql.Types

But if the DB column's type is TEXT, should I still use VARCHAR? or maybe BLOB, or CLOB?

但是如果 DB 列的类型是 TEXT,我还应该使用 VARCHAR 吗?或者 BLOB 或 CLOB?

采纳答案by Kevin Burton

The answer (YES, you are meant to use VARCHAR) can be found here:

可以在此处找到答案(是的,您打算使用 VARCHAR):

http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html(updated outdated broken link)

http://dev.mysql.com/doc/connector-j/en/connector-j-reference-type-conversions.html(更新过时的断开链接)

and here is some info about the MYSQL TEXTtype

这是关于 MYSQLTEXT类型的一些信息

http://dev.mysql.com/doc/refman/5.0/en/blob.html

http://dev.mysql.com/doc/refman/5.0/en/blob.html

回答by Vivek Goel

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)

对于文本类型? http://download.oracle.com/javase/1.4.2/docs/api/java/sql/PreparedStatement.html#setString(int, java.lang.String)

回答by Dorin

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)

Oracle Doc for sql Types 中没有 TEXT 字段,但它可能会在未来出现(现在您可以使用大小为 21845 的信息作为实现硬编码技巧 - 如果您稍后需要知道这是 TEXT 字段 - 实际字段大小由您选择,其余文本由 MySql 在别处记忆)

So the first answer with "use VARCHAR as type" is working (probably the library make a setString call)

因此,“使用 VARCHAR 作为类型”的第一个答案正在起作用(可能是库进行了 setString 调用)

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)

答案“setString”也有效(如果你想自己做出决定,前面有一个开关(field_type) - 我使用这个选项是为了更容易调试代码 - 速度与 cost = switch + setString 相同两种情况)

Thank you all for the links and confirmations !

谢谢大家的链接和确认!