开发者

Java JDBC Procedure Call

开发者 https://www.devze.com 2023-01-29 19:38 出处:网络
I have a oracle procedure that takes date as a parameter and gets called by two different Java web apps, within the procedure I extract the day using to_char(date, \'d\') from the date passed. I am un

I have a oracle procedure that takes date as a parameter and gets called by two different Java web apps, within the procedure I extract the day using to_char(date, 'd') from the date passed. I am unable to find out why the day returned by one app is different from other. I am using sa开发者_如何学JAVAme ojdbc driver for both of the apps. Does it have anything to do with machine env variable these apps are running at?

Thanks


this is because the first day of the week is not the same in all countries, for example in Europe the first day of the week is Monday while in the US it is Sunday. Consider:

SQL> select * from nls_session_parameters where parameter = 'NLS_TERRITORY';

PARAMETER                      VALUE
------------------------------ ----------------------------------------
NLS_TERRITORY                  AMERICA

SQL> select to_char(date '2010-12-07', 'D') from dual;

TO_CHAR(DATE'2010-12-07','D')
-----------------------------
3

SQL> alter session set nls_territory=FRANCE;

Session altered

SQL> select to_char(date '2010-12-07', 'D') from dual;

TO_CHAR(DATE'2010-12-07','D')
-----------------------------
2

Set the session parameter NLS_TERRITORY at the beginning of the procedure if it depends on it.


Try to explicitly specify the timezone on both of your webapp containers by passing them the -Duser.timezone="America/New_York" VM argument (adjust per your timezone needs) when startingup them.

To address your comment, in the application level you could explicitly specify the desired timezone when calling your stored proc. For example:

CallableStatement statement = null;
Connection conn = null;

    try {
        conn = getYourConnection();
        Calendar dbCal = new GregorianCalendar(YOUR_DATABASE_TIMEZONE);

        String sql = "begin schema_name.package_name.stored_proc(var1=>?, " +
                "var2=>?); end;";

        statement = conn.prepareCall(sql);
        statement.setInt(1, something);
        statement.setTimestamp(2, yourDate.getTime(), dbCal);

        statement.execute();

        conn.commit();

    } finally {
        if (statement!=null) statement.close();
        if (conn!=null) conn.close();
    }


try to check the default locale in JAVA/Oracle in both the applications.

I think that it may depend on the default locale set in JAVA.

0

精彩评论

暂无评论...
验证码 换一张
取 消