Solved

Why does the code have no effect?

Posted on 1998-10-19
15
165 Views
Last Modified: 2010-05-18
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
Comment
Question by:CG
  • 8
  • 7
15 Comments
 

Author Comment

by:CG
ID: 1225955
Edited text of question
0
 

Author Comment

by:CG
ID: 1225956
Edited text of question
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 50 total points
ID: 1225957
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:CG
ID: 1225958
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
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225959
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
 

Author Comment

by:CG
ID: 1225960
"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
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225961
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
 

Author Comment

by:CG
ID: 1225962
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
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225963
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
 

Author Comment

by:CG
ID: 1225964
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
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225965
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
 

Author Comment

by:CG
ID: 1225966
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
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225967
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
 

Author Comment

by:CG
ID: 1225968
You are right. I use an JDBC-ODBC bridge from Sun.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1225969
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
starter POM and spring-boot-starter,  spring-boot-web 2 56
Basic Java Case or If-Else statement... 3 51
servlet example 17 32
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question