Link to home
Start Free TrialLog in
Avatar of CG
CG

asked on

Why does the code have no effect?

The following code display data with type "Date/Time" incorrectly. Why does the following code have no effect on data with type "Date/Time"?
  else if ((ctype == Types.DATE)|| (ctype == Types.TIME)){
                cval = formatter.format(q.rs.getDate(i));
                System.out.println(cval);
  }

Here are the codes:
 for (i = 1; i <= ccount; i++) {
            ctype = md.getColumnType(i);
            csize = md.getColumnDisplaySize(i);

            if ((ctype == Types.BINARY) || (ctype == Types.LONGVARBINARY)
                         || (ctype == Types.VARBINARY))
                 cval = "BIN";
           //In the actual fact, the following code has no effects
            else if ((ctype == Types.DATE)|| (ctype == Types.TIME)){
                cval = formatter.format(q.rs.getDate(i));
                System.out.println(cval);
            }
            else{
                   
                if (ctype == Types.OTHER)
                   cval = "OBJ";  
                else {
                       cval = rs.getString(i);
                       if (rs.wasNull())
                         cval = "NULL";
                   
                }
            }
            displayrow += allignString(cval, ' ', csize);
        }
        t.append(displayrow + newline );
      }

I made a small table in my database for test:
LastName                 Text
FirstName                 Text
StartTime                  Date/Time

then I input data in the table:
Wonder         John          8:00:00 AM

The textArea displays:

LastName            FirstName           StartTime                            
----------------------------------------------------------------------
Wonder               John                   1899-12-30 08:00:00

Why is "1899-12-30 08:00:00" ( it should be 8:00:00 AM)? Also, the system did not print out  the "StartTime".
Avatar of CG
CG

ASKER

Edited text of question
Avatar of CG

ASKER

Edited text of question
ASKER CERTIFIED SOLUTION
Avatar of msmolyak
msmolyak

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CG

ASKER

Hi,Msmolyak:
Of course, the formatter has been created in my program when I used these codes.   "the system did not print out the StartTime" I meant that the system should print out "8:00:00 AM".  Now I use the following codes, the system still does not print out "8:00:00 AM" or "Error". Actually, the data with "Date/Time" is gotten from the database by the next code:
  cval = rs.getString(i);  That's why I got the start time is: 1899-12-30 08:00:00. If the following code works, the start time should be 8:00:00 AM.

  else if ((ctype == Types.DATE)|| (ctype == Types.TIME)){
         try{
                cval = formatter.format(q.rs.getTime(i));
                System.out.println(cval);
         }catch(Exception e){
              System.out.println("Error");
       }
  }
I am not quite sure which code you use. According to the last piece of code you are getting date value from the result set using getTime() which returns a Date value which is then formatted using your formatter, right?

If your formatter is set to print time only it should not print the date.

So, what happens if you use getTime()? Is it not returning the correct value? What is it returning?

You can use getString() function, then convert the string to Date using SimpleDateFormat.parse() method and then print it using the format method by chopping the date part. But this is the escape hatch, not the right way to do it.
Avatar of CG

ASKER

"what happens if you use getTime()? Is it not returning the correct value? What is it returning? "------------
Because the codes inside the "else if" have no effects on the results, that
is why I posted my question "Why does the code have no effect?".  If I
delete the following codes:

 else if ((ctype == Types.DATE)|| (ctype == Types.TIME)){
         try{
                cval = formatter.format(q.rs.getTime(i));
                System.out.println(cval);
         }catch(Exception e){
              System.out.println("Error");
       }
  }

I get the same results:
LastName            FirstName           StartTime                            
----------------------------------------------------------------------
Wonder               John                   1899-12-30 08:00:00
Does that code ever get executed?
If it does, what does q.rs.getTime(i) return?
Can you use the debugger or add extra print statements to find out?
Avatar of CG

ASKER

1. "Can you use the debugger or add extra print statements to find out?"
   Answer:  I have added extra print statements in the code.
   else if ((ctype == Types.DATE)|| (ctype == Types.TIME)){
         try{
                cval = formatter.format(q.rs.getTime(i));
                System.out.println(cval);        //extra print statement1 for test
         }catch(Exception e){
               System.out.println("Error");    //extra print statement2 for test
       }
    }

2. "Does that code ever get executed?"
    Answer:  Because I did not get any print results from the extra print                                 statement1 and statement2,  the code did not get executed.

I hope this is clear. Please make a small program using these code to test.
If you have no control over column types and the results from the StartTime columns gets returned as a String you can use the suggestion I made above - convert the string to Date and then print it the way you like. I think it is up to the JDBC driver to map database types to Java types so I am not sure wnat you can do to change that.
Avatar of CG

ASKER

I can not convert the string to Date if I don't know the type whether is "Date/Time" or not.  
"I think it is up to the JDBC driver to map database types to Java types"---
I use MS Access 97 to store my database. Will this cause the JDBC driver map's problems?
Instead of checking the column type you can rely on column number or name. You have to check with the vendor of your JDBC driver to find out how the mapping is done.
Avatar of CG

ASKER

I use JDK1.1.6. Could you please tell me indetail "check with the vendor of your JDBC driver to find out how the mapping is done"? Thanks!
Ok, then tell me how you are accessing the database, that is what JDBC driver are you using? Is it an JDBC-ODBC bridge from Sun?
Avatar of CG

ASKER

You are right. I use an JDBC-ODBC bridge from Sun.
Yeah, since it is a free one the is probably not much documentation on that driver. The only thing I can suggest is to test it with other column types to see what it returns as a column type. And again the easier route seems to use the column name or number when deciding how to print its value. I understand it is not as clean as doing it based on column type but you have to live with what you have. If you are certain this driver does not do what you want you may try other JDBC drivers, for example from WebLogic.