简体   繁体   English

从Java ResultSet读取Unicode文本

[英]Reading Unicode Text from Java ResultSet

如何从Java结果集中读取Unicode文本?

rs.getString() returns a Java String which is Unicode by definition. rs.getString()返回定义为Unicode的Java字符串。

If you get mangled characters, you have to configure your database driver to use the right encoding for the connection to the database. 如果收到乱码,则必须配置数据库驱动程序以使用正确的编码来连接数据库。

Just read the strings. 只是读字符串。 All strings in Java are unicode already. Java中的所有字符串已经是unicode了。 If you're having problems, then: 如果您遇到问题,请:

  • It could be a diagnostic problem - you may be reading the right data out of the ResultSet but displaying it so it looks like you haven't read it properly 这可能是一个诊断问题-您可能正在从ResultSet读取正确的数据,但显示了它,因此看起来好像您没有正确读取它
  • It could be a configuration problem - there may be something you need to do when connecting to the database so that it determines the right encoding to use 可能是配置问题-连接到数据库时可能需要做一些事情,以便它确定要使用的正确编码
  • It could be a database problem - the database may not be configured to store full Unicode data 可能是数据库问题-数据库可能未配置为存储完整的Unicode数据
  • It could be a database schema problem - the particular column you're using may be configured using a column type which doesn't support full Unicode 可能是数据库架构问题-您正在使用的特定列可能使用不支持完整Unicode的列类型进行配置
  • It could be a problem in the data, eg with another program incorrectly submitting data. 数据可能有问题,例如,另一个程序错误地提交了数据。

I've seen all of these before now. 我以前看过所有这些。 You should use detailed logging (eg of the individual characters, in hex) to work out whether you've got the data correctly or not - that will tell you where to look next. 您应该使用详细的日志记录(例如,以十六进制表示的各个字符)来确定您是否正确获取了数据-这将告诉您接下来要查找的位置。

如果使用的是DataSource(fe com.mysql.jdbc.jdbc2.optional.MysqlDataSource ),则可以将通道编码直接设置为UTF8,例如ds.setEncoding("UTF-8")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM