oracle.jdbc.driver.T4CConnection.getSchema() exception

huangapple go评论70阅读模式
英文:

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

  &lt;properties&gt;
    &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
    &lt;java.version&gt;1.8&lt;/java.version&gt;
    &lt;maven.compiler.source&gt;${java.version}&lt;/maven.compiler.source&gt;
    &lt;maven.compiler.target&gt;${java.version}&lt;/maven.compiler.target&gt;
  &lt;/properties&gt;
  &lt;dependencies&gt;
    &lt;dependency&gt;
      &lt;groupId&gt;com.oracle&lt;/groupId&gt;
      &lt;artifactId&gt;ojdbc6&lt;/artifactId&gt;
      &lt;version&gt;11.2.0.3&lt;/version&gt;
    &lt;/dependency&gt;
  &lt;/dependencies&gt;

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 = &quot;oracle.jdbc.driver.OracleDriver&quot;;
    String url = &quot;jdbc:oracle:thin:@xx:8095/orcl&quot;;
    String user = &quot;xx&quot;;
    String pswd = &quot;xx&quot;;

    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 &quot;main&quot; 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(&quot;Schema Name is : &quot; + schema);

   or 
 
    DatabaseMetaData dbmd = connection.getMetaData();
   String schema=dbmd.getUserName();
   System.out.println(&quot;Schema Name is : &quot; + 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.

huangapple
  • 本文由 发表于 2020年8月3日 22:55:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63231802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定