?
Solved

Why does the code have no effect?

Posted on 1998-10-19
15
Medium Priority
?
176 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

777 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