本文介绍了Java为Oracle VARCHAR2返回空的字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码似乎可以正常工作,但是它没有显示personCode字符串的任何值. PERSON_CODE是Oracle 9i数据库中的VARCHAR2.

I have the following code which appears to work correctly but it does not display any values for the personCode string. PERSON_CODE is a VARCHAR2 in an Oracle 9i database.

我在我的项目中使用Java SE 1.7和ojdbc7.jar.我是Java新手,有人可以给我一些帮助吗?

I am using Java SE 1.7 and ojdbc7.jar for my project. I am new to Java can anybody give me some help with this?

private static void GetEmployee(String input) {
String output = "";
Connection con=null;
PreparedStatement stmt = null;
String sql ="SELECT ALL BADGE_NUMBER, PERSON_CODE FROM BADGETABLE WHERE BADGE_NUMBER = ?";

try {
    //load driver
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:username/password@host:1521:database");

    //declaring statement
    stmt = con.prepareStatement(sql);
    stmt.setString(1, input);

    // execute query
    ResultSet rows = stmt.executeQuery();

    int i = 0;
    while(rows.next()) {
        i++;
        String badgeCode = rows.getString(1);
        String personCode = rows.getString(2);
        String personType = rows.getString(3);
        System.out.println("Badge number: " + badgeCode);
        System.out.println("Employee ID: " + personCode);
    }
    System.out.println("Number of results: " + i);


    rows.close();    // All done with that resultset
    stmt.close();  // All done with that statement
    con.close();  // All done with that DB connection


}
catch (SQLException e) {
    System.err.println(e);
}
catch (ClassNotFoundException e) {
    System.err.println(e);
}

return;
}

推荐答案

我使用以下方法遇到了同样的问题:

I ran into this same problem using:

  1. Oracle 9i企业版64位(JServer发行版9.2.0.1.0-生产中)
  2. JDBC 12.1.0.1.0-ojdbc7.jar
  3. Java OpenJDK 64位,1.7.0_09-icedtea

具有这样的表: 创建表人( first_name varchar2(60) );

with a table like this: create table person ( first_name varchar2(60) );

然后使用sqlline这样的查询: 从人员中选择first_name,cast(substr(first_name,0,1)作为char);

And query like this using sqlline: select first_name, cast(substr(first_name,0,1) as char) from person;

结果集为[","S"].

Would have a result set of ["","S"].

我的类路径上没有其他任何Oracle jar,这对其他人来说是个问题,但是当我从ojdbc7.jar切换到ojdbc6_g.jar时,此问题得以解决.这是驱动程序版本11.2.0.3.0,在12c下载部分下.

I did not have any other Oracle jars on my class path as was found to be problem for others, but when I switched from ojdbc7.jar to ojdbc6_g.jar this problem resolved. This is driver version 11.2.0.3.0 which is under the 12c download section.

这篇关于Java为Oracle VARCHAR2返回空的字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 03:48