How to set the timezone region for JDBC Connection and avoid the SqlException timezone region not found?
Asked Answered
F

1

6

I have the following problem trying to create a Connection object to handle the connection from a command line Java application and an Oracle database.

So I have a Main class that contains the main() method, this one:

import java.sql.*;
import oracle.jdbc.OracleDriver;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World !!!");

        String partitaIVA = args[0];
        String nomePDF = args[1];

        Connection conn = null;
        Statement  stmt = null;

        try {
            Class.forName ("oracle.jdbc.OracleDriver");
            // Step 1: Allocate a database "Connection" object
            conn = DriverManager.getConnection("jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB

        } catch(SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}

The problem is that when I try to perform this instruction:

conn = DriverManager.getConnection("jdbc:oracle:thin:@XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB

I obtain this exception:

java.sql.SQLException: ORA-00604: error occurred at recursive SQL level 1
ORA-01882: timezone region  not found

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:392)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:385)
    at oracle.jdbc.driver.T4CTTIfun.processError(T4CTTIfun.java:1018)
    at oracle.jdbc.driver.T4CTTIoauthenticate.processError(T4CTTIoauthenticate.java:497)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:257)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:433)
    at oracle.jdbc.driver.T4CTTIoauthenticate.doOAUTH(T4CTTIoauthenticate.java:950)
    at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:639)
    at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:662)
    at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:32)
    at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:560)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:185)
    at Main.main(Main.java:21)

So, I remember that in some other applications that works with this DB it was necessary to set the timezone or something like this (but now I can't access to these applications).

So, how can I fix this issue? Can I set programmatically the timezone for my Connection?

Tnx

Formerly answered 11/2, 2015 at 10:53 Comment(0)
M
10

Write this before your connection attempt:

TimeZone timeZone = TimeZone.getTimeZone("yourTimeZone"); // e.g. "Europe/Rome"
TimeZone.setDefault(timeZone);

So the whole code would be:

try {
    TimeZone timeZone = TimeZone.getTimeZone("yourTimeZone");
    TimeZone.setDefault(timeZone);
    Class.forName("oracle.jdbc.OracleDriver");
    conn = DriverManager.getConnection("connStr", "myUserName", "myPswd");
    ...

If this does not work, the problem may be an invalid JDBC driver version.

Maimonides answered 11/2, 2015 at 10:57 Comment(4)
Now I try, can you say me what package I need to import to can use TimeZone object?Formerly
java.util.TimeZone docs.oracle.com/javase/7/docs/api/java/util/TimeZone.htmlMaimonides
What's the scope of a call to setDefault()? The javadoc says "Sets the TimeZone that is returned by the getDefault method.": to what extent will this affect other running code? Is it limited to the single application that does the call or to the whole JVM and could potentially alter other applications running inside it (i.e. in a servlet container)?Campania
AFAIK it's a JVM-level setting, but I'd make this a new question, as it's a good oneMaimonides

© 2022 - 2024 — McMap. All rights reserved.