防止 jdbc 在 sql 插入和更新时填充字符串

发布于 2024-07-14 11:26:05 字数 401 浏览 3 评论 0原文

我们使用 jdbc-odbc 桥连接到 MS SQL 数据库。 当执行插入或更新时,字符串被放入数据库中,填充到数据库字段的长度。 有什么方法可以关闭这种行为(字符串应该进入表中而不需要填充)?

作为参考,我们可以使用 SQL 管理工具和查询分析器插入不包含填充的字段值,因此我非常确定这种情况发生在 jdbc 或 odbc 层。

编辑:数据库中的字段列为 nvarchar(X),其中 X = 50, 255,无论什么

编辑2:执行插入的调用使用准备好的语句,就像:

PreparedStatement stmt = new con.prepareStatement("INSERT INTO....");
stmt.setString(1, "somevalue");

We are using the jdbc-odbc bridge to connect to an MS SQL database. When perform inserts or updates, strings are put into the database padded to the length of the database field. Is there any way to turn off this behavior (strings should go into the table without padding)?

For reference, we are able to insert field values that don't contain the padding using the SQL management tools and query analyzer, so I'm pretty sure this is occuring at the jdbc or odbc layer of things.

EDIT: The fields in the database are listed as nvarchar(X), where X = 50, 255, whatever

EDIT 2: The call to do the insert is using a prepared statement, just like:

PreparedStatement stmt = new con.prepareStatement("INSERT INTO....");
stmt.setString(1, "somevalue");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤者何惧 2024-07-21 11:26:05

你如何设置字符串? 你在做什么吗?:

PreparedStatement stmt = new con.prepareStatement("INSERT INTO....");
stmt.setString(1, "somevalue");

如果是这样,试试这个:

stmt.setObject(1, "somevalue", Types.VARCHAR);

同样,这只是猜测,没有看到你是如何插入的。

How are you setting the String? Are you doing?:

PreparedStatement stmt = new con.prepareStatement("INSERT INTO....");
stmt.setString(1, "somevalue");

If so, try this:

stmt.setObject(1, "somevalue", Types.VARCHAR);

Again, this is just guessing without seeing how you are inserting.

笛声青案梦长安 2024-07-21 11:26:05

您在数据库中使用的是 CHAR 字段还是 VARCHAR?

CHAR 填充字段的大小。 VARCHAR 则不然。

我不认为 JDBC 会导致这种情况。

Are you using CHAR fields in the database or VARCHAR?

CHAR pads the size of the field. VARCHAR does not.

I don't think JDBC would be causing this.

萌无敌 2024-07-21 11:26:05

如果您可以使插入与常规 SQL 工具一起使用(例如...我不知道 Toad for MS SQL Sever 或其他工具),那么更改驱动程序就可以了。

使用 Microsoft SQL Server JDBC IV 型驱动程序。

尝试此链接

http ://www.microsoft.com/downloads/details.aspx?familyid=F914793A-6FB4-475F-9537-B8FCB776BEFD&displaylang=en

不幸的是,这些类型的下载带有很多垃圾。 有一个安装工具和另外数百个文件。 只需查找类似以下内容:

intalldir\lib\someSingle.jar

复制到其他位置并卸载/删除其余部分。

几个月前我做了这个,不幸的是我不记得它到底在哪里了。

编辑

好的,我明白了。

单击下载,然后在页面末尾单击“我同意并想要下载 UNIX 版本”。

这是一个常规压缩文件(使用 win rar 或其他),然后在其中查找该单一 jar。

那应该有效。

If you can make your insert to work with regular SQL tools ( like ... I don't know Toad for MS SQL Sever or something ) then changing the driver should do.

Use Microsoft SQL Server JDBC type IV driver.

Give this link a try

http://www.microsoft.com/downloads/details.aspx?familyid=F914793A-6FB4-475F-9537-B8FCB776BEFD&displaylang=en

Unfortunately these kinds of download comes with a lot of garbage. There's an install tool and another hundreds of file. Just look for something like:

intalldir\lib\someSingle.jar

Copy to somewhere else and uninstall/delete the rest.

I did this a couple of months ago, unfortunately I don't remeber exactly where it was.

EDIT

Ok, I got it.

Click on the download and at the end of the page click on "I agree and want to download the UNIX version"

This is a regular compressed file ( use win rar or other ) and there look for that sigle jar.

That should work.

今天小雨转甜 2024-07-21 11:26:05

如果您使用捆绑的 Sun JDBC-ODBC Bridge 驱动程序,您可能需要考虑迁移到适当的 MS SQL JDBC 驱动程序。 Sun 不建议在生产环境中使用桥接驱动程序。

仅建议在实验用途或没有其他替代方案时使用 JDBC-ODBC Bridge 驱动程序。

转向更具针对性的驱动程序可能会同时解决您的问题,或者至少在您修复错误时它将提供一个生产就绪的解决方案。

If you are using the bundled Sun JDBC-ODBC Bridge driver, you may want to consider migrating to a proper MS SQL JDBC driver. Sun does not recommend that the bridge driver be used in a production environment.

The JDBC-ODBC Bridge driver is recommended only for experimental use or when no other alternative is available.

Moving to a more targeted driver may fix your problem all together, or at least it will provide a production ready solution when you do fix the bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文