Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4400343

ResultSet.getString() returns "" if datatype is LONGVARCHAR and length is 1.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • core-libs
    • rc1
    • generic
    • generic



      Name: boT120536 Date: 12/21/2000


      java version "1.3.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
      Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

      If the datatype is either a LONGVARCHAR or LONGCHAR and the length of the field
      is 1, ResultSet.getString() function returns a blank instead of the data.

      I have only tested this with the JDBC-ODBC bridge against MS Access and MS SQL
      Server.

      Steps to reproduce this:

      In the database, create the following table in MS Access:

      create table testTab(
       id COUNTER,
       descrip LONGCHAR
      )

      Then insert one row in the table.

      insert into testTab(descrip)
      values('A')

      insert into testTab(descrip)
      values('AA')

      When you run a java program and try the get the data back, the first row comes
      back as blank but the second row returns "AA". This only happens if the datatype
      is LONG, not if the type is VARCHAR or CHAR.


      I am including a test program below. There are two files, the first is a class
      representing the database table and second is an application that uses the class
      with a "main" function.



      //--------------- FILE ONE ----------------------------

      import java.sql.*;
      import java.util.Vector;

      //==========================================================================
      public class TtestTab{

      //Following variables are listed alphabetically
      protected Connection dbConn; //Database connection object.

      //Following fields map to the database fields and are listed
      //in the order in which they appear in the table
      protected int id;
      protected String descrip;


      //==========================================================================
      //This constructor can be used for editable objects.
      public TtestTab(Connection dbConn){
      this.dbConn = dbConn;
      }



      //==========================================================================
      //Runs a select statement and fills the object variables.
      //Returns true if a record is found and false if not.
      public boolean fetchByPk(int id)throws SQLException, Exception {

      PreparedStatement pstmt;
      ResultSet rs;
      String sqlstr;

      if(dbConn == null)
      throw new Exception("dbConn object is null");

      sqlstr = "select * from testTab " +
      "where id = ? ";

      pstmt = dbConn.prepareStatement(sqlstr);

      pstmt.setInt(1, id);

      rs = pstmt.executeQuery();

      if(rs.next()){
      //For PK set both the orginal as well as the current
      field value
      this.id = rs.getInt("id");

                  //The following line results in a blank value if:
                  // * The datatype = LONGCHAR or LONGVARCHAR
                  // * and the length == 1

                  String s = rs.getString("descrip");
      this.descrip = s;

                  if(rs.wasNull()){
                      System.out.println("Last field value was NULL");
                  }

      rs.close();
      pstmt.close();

      return true;
      } //if block ends here.

      rs.close();
      pstmt.close();

      return false;

      }

          //==========================================================================
          public String get_descrip(){
              return descrip;
          }

      //==========================================================================
      }





      //-------------------- FILE TWO ----------------------

      import java.sql.*;
      import java.util.*;
      import java.io.*;


      public class MainApp {




          private Connection dbConn;

          public MainApp() {
          }


          public static void main(String[] args) {
              try{
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
              }catch(java.lang.ClassNotFoundException e) {
      System.err.print("ClassNotFoundException: ");
      System.err.println(e.getMessage());
      return;
      }


              MainApp tester = new MainApp();


              tester.init();

          }

          //==========================================================================
          private void init(){
              try{
                  dbConn = DriverManager.getConnection("jdbc:odbc:accessDSN",
      "imranh", "password");
                  menu();

              }catch(SQLException ex) {
      showMsg("SQLException: " + ex.getMessage());
      showMsg(ex.getMessage());
      }catch(Exception e){
                  e.printStackTrace();
              }

              prompt("Press Enter", "");


          }
          //==========================================================================
          private void menu(){

              while(true){
                  showMsg("================================");
                  showMsg("Make selection: ");
                  showMsg("1:\tTest Bug");



                  String answer = prompt("Enter you choice:" , "x");

                      if(answer.equals("x"))
                          break;
                      else if(answer.equals("1"))
                          unitTest(dbConn);
                      else
                          showMsg("Invalid choice");

              }

          }

          //==========================================================================
          private void unitTest(Connection dbConn){
              TtestTab test = new TtestTab(dbConn);

              try{
                  test.fetchByPk(1);

                  showMsg("The value of descrip is: " + test.get_descrip());

              }catch(Exception e){
                  e.printStackTrace();
              }

          }

          //==========================================================================
      public String prompt(String s, String def){
              String buf = null;

              if(def.length() > 0)
                  System.out.print(s + " [" + def + "]" );
              else
                  System.out.print(s + " ");

              buf = readLine().trim();

              if(buf.length() == 0) buf = def;


              return buf;
          }


      //==========================================================================
          public String readLine(){
          String buf = null;
           try{
      BufferedReader in = new BufferedReader(new
      InputStreamReader(System.in));
      buf = in.readLine();

      if(buf == "..")
      buf = "";

      }catch(IOException e){
                  showMsg(e.getMessage());
              }

              return buf;

          }


          //=========================================================================
          public void showMsg(String s){
              System.err.println(s);
          }


      }
      (Review ID: 114107)
      ======================================================================

            jbrucesunw Jonathan Bruce (Inactive)
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: