Solved

Why does the code have no effect?

Posted on 1998-10-19
15
158 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
Comment Utility
Edited text of question
0
 

Author Comment

by:CG
Comment Utility
Edited text of question
0
 
LVL 5

Accepted Solution

by:
msmolyak earned 50 total points
Comment Utility
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
 

Author Comment

by:CG
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:CG
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You are right. I use an JDBC-ODBC bridge from Sun.
0
 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now