Java PreparedStatement setString更改字符 [英] Java PreparedStatement setString changes characters

查看:266
本文介绍了Java PreparedStatement setString更改字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所示:可以肯定的是,我正在调试我的应用程序,因此,在将字符串放入PreparedStatement变量中的地方,特殊字符更改为?".我实际上不知道在哪里搜索应该修复的东西,所以我不知道是否需要代码.无论如何,我会在这里放一些东西:

As in title: to be sure, I was debugging my application, and so in line, where I put strings into PreparedStatement variable, special characters are changing to "?". I actually don't know where to search for things that should repair it, so I don't know if code is required.. Anyway, I'll put some here:

PreparedStatement stm = null;
String sql = "";

    try{
      sql = "INSERT INTO methods (name, description) VALUES (?, ?)";
      stm = connection.prepareStatement(sql);
      stm.setString(1, method.getName());
      stm.setString(2, method.getDescription());
      //...
    }catch(Exception e){}

在调试方法对象中的名称"字段时正确,但是将其添加到stm变量后,将其字符更改为?".

while debugging 'name' field was correct in method object, but after adding it into stm variable, it changed it's characters to '?'.

我在SO上找到了一个与类似西他阿丁有关的话题,但是没有任何答案对我有帮助,因为我完全知道在语句中添加字符串,而不是在数据库中不正确.但是我不知道..

I have found one topic about the similar sitoatuin on SO, but there wasn't any answer that could help me since I exactely know that there is something not right in adding string to statement, not in database. But I don't know what..

有没有建议?

PS.我正在使用netbeans 6.7.1版本

PS. I'm using netbeans 6.7.1 version

我正在使用标准的netbeans调试器进行调试,并且在将字符串添加到'stm'变量之前正在检查变量的状态.我什至将getName()方法更改为带有特殊字符的静态字符串.因此,可以肯定,使用Method类一切正常.

I was debugging with standard netbeans debugger, and was checking state of variables before adding strings to 'stm' variable. I was even changing getName() method to static string with special characters. So for sure everything is ok with Method class.

我又做了一个测试.检查的stm变量及其属性之一是设置为"cp1252"的"charEncoding".所以主要的问题是..如何改变呢?

I've made one more test. Checked stm variable and one of it's properties is "charEncoding" which is set to "cp1252". So the main question is.. how to change that?

推荐答案

听起来像字符编码问题.也许驱动程序正在将您的字符串转换为适用于字段/表/模式/数据库的适当编码,而不是让服务器执行?如果您试图存储在字段/表/模式/数据库的编码中没有表示形式的字符,那将解释?"字符.

Sounds like a character encoding issue to me. Perhaps the driver is transcoding your strings into the appropriate encoding for the field/table/schema/database rather than letting the server do it? If you are trying to store a character which has no representation in the encoding of the field/table/schema/database, that would explain the '?' characters.

这篇关于Java PreparedStatement setString更改字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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