oracle 如何为 JDBC 连接设置时区区域并避免找不到 SqlException 时区区域?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28452513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:43:49  来源:igfitidea点击:

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

javaoraclejdbcconnectionsqlexception

提问by AndreaNobili

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

我在尝试创建一个Connection对象来处理来自命令行 Java 应用程序和 Oracle 数据库的连接时遇到以下问题。

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

所以我有一个包含main()方法的Main类,这个:

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?

那么,我该如何解决这个问题?我可以为我的 Connection 以编程方式设置时区吗?

Tnx

田纳西州

回答by meskobalazs

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.

如果这不起作用,问题可能是无效的 JDBC 驱动程序版本。