oracle VARCHAR2 列上的 ResultSet.getString() 返回空字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4168494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ResultSet.getString() on VARCHAR2 column returns empty string
提问by Ilia G
Disclaimer: I don't actually know anything about nether Oracle nor Java. The issue is in a project that some other developer completed at some point in time and then left the company. Now I have to setup webserver, database and get it all up and running.
免责声明:我实际上对下界 Oracle 和 Java 一无所知。问题出在某个其他开发人员在某个时间点完成然后离开公司的项目中。现在我必须设置网络服务器、数据库并使其全部启动并运行。
the code is approx this:
代码大约是这样的:
OracleDataSource ods = new OracleDataSource();
ods.setURL("jdbc:oracle:thin:<user>/<password>@localhost:1521:xe");
OracleConnection ocon = (OracleConnection)ods.getConnection();
OracleStatement stmt = (OracleStatement)ocon.createStatement();
OracleResultSet rs = (OracleResultSet)stmt.executeQuery("SELECT POLLID, QUESTION, ISMULTISELECT FROM POLL WHERE POLLID = " + pollID);
if (!rs.next()) {
System.out.println("No rows found.");
return false;
}
this._PollID = rs.getInt("POLLID");
this._Question = rs.getString("QUESTION");
this._IsMultiSelect = rs.getBoolean("ISMULTISELECT");
The POLLID and ISMULTISELECT columns return correct values as expected. The QUESTION seem to always return empty string. The value in the DB is obviously not empty.
POLLID 和 ISMULTISELECT 列按预期返回正确的值。问题似乎总是返回空字符串。DB中的值显然不为空。
The rs.getAsciiStream("QUESTION").available()
also returns zero.
该rs.getAsciiStream("QUESTION").available()
也返回零。
Am I missing something completely obvious here?
我在这里遗漏了一些完全明显的东西吗?
EDIT:
编辑:
sqlplus returns varchar2 value just fine
connecting via odbc (as opposed to thin) also makes things work
sqlplus 返回 varchar2 值就好了
通过 odbc 连接(而不是瘦)也可以使工作正常
采纳答案by Ilia G
Thanks to everyone who replied. At this point it seems issue is between thin driver and XE version of Oracle. Unfortunately we don't have full version kickin' around (we are primarily ASP.NET/MS SQL developers), so we'll have to stick with ODBC driver for now and hope issue will magically resolve itself when we push it to live environment (hosted by third party). Very crappy assumption to make, but at this point I see no other options....
感谢所有回复的人。在这一点上,问题似乎是在瘦驱动程序和 XE 版本的 Oracle 之间。不幸的是,我们没有完整版本(我们主要是 ASP.NET/MS SQL 开发人员),所以我们现在必须坚持使用 ODBC 驱动程序,并希望当我们将其推送到实际环境时,问题会神奇地自行解决(由第三方托管)。做出非常糟糕的假设,但在这一点上我看不到其他选择......
回答by reg
so no Exceptions, you are not using reserved words...maybe try to use other driver, or select into other table and experiment start with empty QUESTION column, then add some value and debug.
所以没有例外,你没有使用保留字......也许尝试使用其他驱动程序,或者选择其他表并从空的问题列开始实验,然后添加一些值并进行调试。
回答by rob.fellows
I had the same exact issue and found that the root of the problem comes from the orai18n.jar. once i removed this from my classpath, the issue went away.
我有同样的问题,发现问题的根源来自 orai18n.jar。一旦我从我的类路径中删除了它,问题就消失了。
回答by electrotype
I have the same problem. I do not have access to the driver that is used because the connection is taken from a Weblogic server using JNDI. I cannot remove any .jar from the server neither.
我也有同样的问题。我无权访问使用的驱动程序,因为连接是从使用 JNDI 的 Weblogic 服务器获取的。我也无法从服务器中删除任何 .jar。
The workaround I found :
我发现的解决方法:
String value = new String(resultset.getBytes());
Make sure you use the right encoding if required :
如果需要,请确保使用正确的编码:
String value = new String(resultset.getBytes(), [CHARSET])
回答by Calvin
I had the same issue. "getInt()" would return correct value from Oracle 9i DB, but using "getString()" would result into empty string, no matter how many times i ran, within eclipse or outside on seperate Tomcat or other servers.
我遇到过同样的问题。“getInt()”将从 Oracle 9i DB 返回正确的值,但使用“getString()”将导致空字符串,无论我在 eclipse 内还是在单独的 Tomcat 或其他服务器上运行多少次。
After going through a lot of various threads, and quite a few trials, I came to the conclusion that the issue is with the version of ojdbc6.jar that I was using. Earlier, I was using ojdbc6.jar with Oracle version 12.1.0.1., which is not so good for connecting to OLD Oracle 9i DB. After realising, I switched on to ojdbc6.jar from Oracle 11.2.0.3 and it worked like a charm.
在经历了很多不同的线程和相当多的试验之后,我得出的结论是问题出在我使用的 ojdbc6.jar 版本上。早些时候,我使用 ojdbc6.jar 和 Oracle 版本 12.1.0.1.,这对于连接到 OLD Oracle 9i DB 不太好。意识到之后,我从 Oracle 11.2.0.3 切换到 ojdbc6.jar,它的工作非常出色。
Hope it helps. cheers!
希望能帮助到你。干杯!
回答by Antti Ryts?l?
I had this same issue with eclise GCJ ( Stock centos6 ) and mysql-connector with the same concatenated queries. The problem was solved with reverting back to openJDK.
我在 eclise GCJ ( Stock centos6 ) 和 mysql-connector 上遇到了同样的问题,具有相同的串联查询。通过恢复到 openJDK 解决了该问题。