英文:
oracle.jdbc.driver.T4CConnection.getSchema() exception
问题
我试图从Oracle连接中获取默认数据库名称。
以下的代码给我一个抽象方法错误。有人能建议我如何使用jdbc获取默认数据库名称吗?
环境信息
版本
oracle:Oracle 11g
java:1.8
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
代码
package snippet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectiongetSchemaErrorTest {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@xx:8095/orcl";
String user = "xx";
String pswd = "xx";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, pswd);
System.out.println(connection.getClass().getName());
String schema = connection.getSchema();
System.out.println(schema);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
错误
oracle.jdbc.driver.T4CConnection
Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.getSchema()Ljava/lang/String;
at snippet.ConnectiongetSchemaErrorTest.main(ConnectiongetSchemaErrorTest.java:24)
英文:
I am trying get the default database name from the connection for oracle
The following code gives me this abstract method error. Can anyone suggest me how I can get default database name using jdbc?
environmental information
version
oracle:Oracle 11g
java:1.8
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
</dependencies>
code
package snippet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectiongetSchemaErrorTest {
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@xx:8095/orcl";
String user = "xx";
String pswd = "xx";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, pswd);
System.out.println(connection.getClass().getName());
String schema = connection.getSchema();
System.out.println(schema);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
error
oracle.jdbc.driver.T4CConnection
Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.getSchema()Ljava/lang/String;
at snippet.ConnectiongetSchemaErrorTest.main(ConnectiongetSchemaErrorTest.java:24)
答案1
得分: 2
getSchema()
方法仅在 Java 7
及更高版本中可用,但 ojdbc6.jar
是使用 Java 6
编译的,而 ojdbc8.jar
是使用 Java 8
编译的。您可以使用 ojdbc8.jar
或者您可以尝试以下代码,使用 ojdbc6.jar
:
// 对于 ojdbc6.jar
String schema = connection.getMetaData().getUserName();
System.out.println("Schema Name is: " + schema);
或者
DatabaseMetaData dbmd = connection.getMetaData();
String schema = dbmd.getUserName();
System.out.println("Schema Name is: " + schema);
英文:
getSchema()
method is only available in Java 7
and above but ojdbc6.jar
is compiled with Java 6
where as ojdbc8.jar
is compiled with Java 8
. Either use ojdbc8.jar
or alternatively you can try the code below with ojdbc6.jar
// For ojdbc6.jar
String schema= connection.getMetaData().getUserName();
System.out.println("Schema Name is : " + schema);
or
DatabaseMetaData dbmd = connection.getMetaData();
String schema=dbmd.getUserName();
System.out.println("Schema Name is : " + schema);
答案2
得分: 0
以下是已翻译的内容:
从11.2.0.4、12.2.0.2、18.3.0.0、19.3.0.0、19.6.0.0和19.7.0.0开始,官方支持的Oracle JDBC版本已在中央Maven存储库上提供。
更多详细信息请参阅Maven中央指南。
建议使用最新版本。查看JDK兼容性FAQ。
英文:
The official supported Oracle JDBC versions from 11.2.0.4, 12.2.0.2, 18.3.0.0, 19.3.0.0,19.6.0.0, and 19.7.0.0 are available on Central Maven Repository.
Refer to Maven Central Guide for more details.
It is recommended to use the latest version. Check out FAQ for JDK compatibility.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论