JDBC,MySQL:将位转换为BIT(M!= 1)列

编程入门 行业动态 更新时间:2024-10-17 22:27:57
本文介绍了JDBC,MySQL:将位转换为BIT(M!= 1)列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是使用JDBC + MySQL的新手。

I'm new to using JDBC + MySQL.

我有几个1/0值,我想将它们放在带有PreparedStatement的数据库中。目标列是BIT(M!= 1)。我不清楚使用哪种setXXX方法。我可以很容易地找到数据出来的参考资料,但它是如何进入我的。

I have several 1/0 values which I want to stick into a database with a PreparedStatement. The destination column is a BIT(M!=1). I'm unclear on which of the setXXX methods to use. I can find the references for what data comes out as easily enough, but how it goes in is eluding me.

这些价​​值实际上是有序的布尔集合。应用程序使用的对象。另外,我偶尔会从1/0字符的平面文本文件中导入数据。

The values effectively live as an ordered collection of booleans in the objects used by the application. Also, I'll occasionally be importing data from flat text files with 1/0 characters.

推荐答案

设置 BIT(M) MySQL中的列

To set a BIT(M) column in MySQL

对于 M == 1

setBoolean(int parameterIndex, boolean x)

来自javadoc

将指定参数设置为给定Java布尔值的 。当将驱动程序发送到数据库时,驱动程序会将其转换为SQL BIT值。

Sets the designated parameter to the given Java boolean value. The driver converts this to an SQL BIT value when it sends it to the database.

对于 M> 1

对的支持BIT(M)其中 M!= 1 在JDBC中存在问题 BIT(M)只需要完整的SQL-92,只有少数DB支持。

The support for BIT(M) where M!=1 is problematic with JDBC as BIT(M) is only required with "full" SQL-92 and only few DBs support that.

点击这里映射SQL和Java类型:8.3.3 BIT

以下适用于MySQL(至少使用MySQL 5.0.45,Java 1.6和MySQL Connector / J 5.0.8)

The following works for me with MySQL (at least with MySQL 5.0.45, Java 1.6 and MySQL Connector/J 5.0.8)

... PreparedStatement insert = con.prepareStatement( "INSERT INTO bittable (bitcolumn) values (b?)" ); insert.setString(1,"111000"); ...

这使用MySQL的特殊b'110101010'语法来设置BIT列的值。

This uses the special b'110101010' syntax of MySQL to set the value for BIT columns.

更多推荐

JDBC,MySQL:将位转换为BIT(M!= 1)列

本文发布于:2023-07-09 21:30:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1088265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   JDBC   MySQL   BIT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!