JDBC,MySQL:将位转换为BIT(M!= 1)列 [英] JDBC, MySQL: getting bits into a BIT(M!=1) column

查看:1170
本文介绍了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)列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆