Retrieving Data problem

Discussions

General J2EE: Retrieving Data problem

  1. Retrieving Data problem (2 messages)

    I have written a class which query a Date from a table of the Database with getString().
    When I deploy the class in the Database & run the class from a Java Stored Procedure, I get an incorrect value for the day 21/March/2002 (I obtain 20/March/2002 23:00:00)

    The result of the select from SQL/plus is correct:

    SQL> select c1,to_char(c1,'yyyy-mm-dd hh24:mi:ss') c1fmt from prueba;

    c1 c1fmt
    -------- -------------------
    20/03/02 2002-03-20 00:00:00
    21/03/02 2002-03-21 00:00:00
    22/03/02 2002-03-22 00:00:00
    21/10/02 2002-10-21 00:00:00

    When I run the class from JDeveloper or from command line directly, the result is the same:
    2002-03-20 00:00:00.0
    2002-03-21 00:00:00.0
    2002-03-22 00:00:00.0
    2002-10-21 00:00:00.0

    But when I run the Java Stored Procedure associated to the class in the database the result is wrong:
    2002-03-20 00:00:00.0
    2002-03-20 23:00:00.0 <--
    2002-03-22 00:00:00.0
    2002-10-21 00:00:00.0

    ¿There is any idea what occurs with the day: 21/March/2002?
    Any help would be greatly appreciated.

    -------------------------------------------------
    Here is the code of the class:

    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import oracle.jdbc.driver.OracleDriver;

    public class Prueba{

      public main() {
       try{
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    // To deploy in DB:
        Connection conn = new oracle.jdbc.driver.OracleDriver().defaultConnection();
    // To run from Jdeveloper:
    // Connection conn = DriverManager.getConnection("jdbc:oracle:thin:teseonet/teseonet@jerte:1521:ccdo2");
        
        } catch (SQLException ex) {
          ex.printStackTrace();
        }

        String fecha;
        try {
          
          // Obtiene el número de envios cargados.
          String strSQL = "SELECT c1 from prueba";
          Statement stmt = conn.createStatement ();
          ResultSet res = stmt.executeQuery(strSQL);
          
          while (res.next()){
             System.out.println(res.getString(1));
          }
          stmt.close();
          res.close();
          conn.close();
          }
        catch (Exception ex) { // Se capturan los posibles errores SQL
            try {
              conn.close();
            } catch (SQLException ex_conn) {
            }
        }
      }
    }
    -------------------------------------------------
  2. This problem should be related to "daylight savings changes".
    Make sure all machines have the same time zone. It could solve the problem.
    Unfortunately it could be just a bug. For example I had similar problem with an old JDK version. You might need to write a workaround for the problem if changing the version is not an option.

    Kalin
  3. I have solved the problem.
    Indeed it could be a bug, because the Time Zone of the database was correct but when I get Time Zone from my program with: TimeZone.getDefault() I obtained GMT+04:30. It’s impossible!
    I have solved the problem changing the Timezone of the JVM after establishing database connection:

    conexion = new OracleDriver().defaultConnection();
    TimeZone.setDefault(TimeZone.getTimeZone("Europe/Madrid"));

    Thanks.