How to set the timezone region for JDBC Connection and avoid the SqlException timezone region not found?(如何为 JDBC Connection 设置时区,避免找不到 SqlException 时区?)
问题描述
我在尝试创建 Connection 对象以处理来自命令行 Java 应用程序和 Oracle 数据库的连接时遇到以下问题.
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.
所以我有一个包含 main() 方法的 Main 类,这个:
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
我得到这个异常:
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).
那么,我该如何解决这个问题?我可以以编程方式为我的 Connection 设置时区吗?
So, how can I fix this issue? Can I set programmatically the timezone for my Connection?
Tnx
推荐答案
在尝试连接之前写下:
TimeZone timeZone = TimeZone.getTimeZone("yourTimeZone"); // e.g. "Europe/Rome"
TimeZone.setDefault(timeZone);
所以整个代码是:
try {
TimeZone timeZone = TimeZone.getTimeZone("yourTimeZone");
TimeZone.setDefault(timeZone);
Class.forName("oracle.jdbc.OracleDriver");
conn = DriverManager.getConnection("connStr", "myUserName", "myPswd");
...
如果这不起作用,则问题可能是无效的 JDBC 驱动程序版本.
If this does not work, the problem may be an invalid JDBC driver version.
这篇关于如何为 JDBC Connection 设置时区,避免找不到 SqlException 时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 JDBC Connection 设置时区,避免找不到 SqlException 时区?


基础教程推荐
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01