• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

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".
0
CG
Asked:
CG
  • 8
  • 7
1 Solution
 
CGAuthor Commented:
Edited text of question
0
 
CGAuthor Commented:
Edited text of question
0
 
msmolyakCommented:
Create your formatter as

 SimpleDateFormat formatter
          = new SimpleDateFormat (" hh:mm:ss a");

What do you mean "the system did not print out the StartTime"?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CGAuthor Commented:
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");
       }
  }
0
 
msmolyakCommented:
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.
0
 
CGAuthor Commented:
"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
0
 
msmolyakCommented:
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?
0
 
CGAuthor Commented:
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.
0
 
msmolyakCommented:
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.
0
 
CGAuthor Commented:
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?
0
 
msmolyakCommented:
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.
0
 
CGAuthor Commented:
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!
0
 
msmolyakCommented:
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?
0
 
CGAuthor Commented:
You are right. I use an JDBC-ODBC bridge from Sun.
0
 
msmolyakCommented:
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.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now