【问题标题】:AbstractMethodError thrown at runtime with java.sql.Connection使用 java.sql.Connection 在运行时引发 AbstractMethodError
【发布时间】:2016-10-31 05:22:55
【问题描述】:

我正在尝试通过创建一个非常简单的类来向 Oracle XE 11g 的实例发送查询,本质上是制作一个非常简单的 SQL*Plus 来了解 JDBC 的基础知识。

我目前正在使用的源代码是:

public class Example {
    static String username, password;
    static String dbDriver = "oracle.jdbc.driver.OracleDriver";
    static String dbConnection = "jdbc:oracle:thin:@//localhost:1521/xe";

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String line;
        Connection c = null;
        Statement stmt = null;
        int loginAttempts = 0;

        // Log in loop
        do {
            try {
                Class.forName(dbDriver);
                System.out.print("Enter username: ");
                line = input.nextLine();
                if (line.contains("/")) {
                    String[] login = line.split("/");
                    if (login.length != 2) {
                        System.out
                                .println("Unrecognized information, exiting...");
                        System.exit(0);
                    }

                    username = login[0].trim();
                    password = login[1].trim();
                } else {
                    username = line;
                    System.out.print("Enter password: ");
                    password = input.nextLine();
                }

                c = DriverManager.getConnection(dbConnection, username,
                        password);
                stmt = c.createStatement();
                loginAttempts = -1;
            } catch (ClassNotFoundException e) {
                System.err.println("Unable to connect to database, exiting...");
                System.exit(-1);
            } catch (SQLException e) {
                System.out.println("Username and/or password is incorrect");
                if (++loginAttempts == 1) {
                    System.out.println("Too many failed login attempts, exiting...");
                    System.exit(0);
                }
            }
        } while (loginAttempts != -1);

        // Input loop
        for (;;) {
            try {
                // Write out the prompt text and wait for input
                if(c == null) {
                    throw new IllegalStateException("Connection should not be null");
                }
                System.out.print(c.getSchema() + ":> ");
                String tmp = input.nextLine();

                // Check if the user entered "exit"
                if (tmp.toLowerCase().equals("exit")) {
                    System.out.println("Exiting...");
                    input.close();
                    System.exit(0);
                }

                String query;
                // TODO: For some reason, no semi-colon is allowed
                if (tmp.charAt(tmp.length() - 1) == ';')
                    query = tmp.split(";")[0];
                else
                    query = tmp;
                // System.out.println(query);
                ResultSet rset = stmt.executeQuery(query);
                ResultSetMetaData rmd = rset.getMetaData();
                int colCount = rmd.getColumnCount();

                // Column indices start with 1, print column names
                for (int i = 1; i <= colCount; i++) {
                    System.out.printf("%-20.20s   ", rmd.getColumnName(i));
                }
                System.out.println();

                while (rset.next()) {
                    for (int i = 0; i < colCount; i++) {
                        System.out.printf("%-20.20s | ", rset.getString(i + 1));
                    }
                    System.out.println();
                }
                System.out.println();
            } catch (SQLSyntaxErrorException e) {
                System.out.println("Encountered a syntax error:\n"
                        + e.getMessage());
            } catch (SQLException e) {
                System.err.println("An unexpected error occurred");
                e.printStackTrace();
                input.close();
                System.exit(-1);
            }
        }

    }
}

在第二个 try/catch 块中,在 for(;;) 循环中,我得到以下输出:

Enter username: hr/hr
Exception in thread "main" java.lang.AbstractMethodError: oracle.jdbc.driver.T4CConnection.getSchema()Ljava/lang/String;
at jdbctest.Example.main(Example.java:69)

我检查了Oracle docs (java.lang.AbstractMethodError),它说这个错误只能在以下情况下抛出:

“自上次编译当前执行的方法以来,某些类的定义发生了不兼容的更改。”

我认为第 27 行缺少一些东西,但我不确定如何解决这个问题,任何指导都会非常有帮助。

【问题讨论】:

  • 在您的帖子中包含源代码。外部链接最终会变坏,使帖子对未来的读者毫无价值。未来价值是 SO 的重点。
  • 并且,请仅添加相关代码 - 请参阅minimal reproducible example。很可能您可以消除大部分代码并仍然重现该问题。
  • btw,在调用getSchema() 方法时,错误不是在第 27 行,而是在第 69 行。您很可能在 JDBC 版本中有一些不匹配 - 请参阅 stackoverflow.com/questions/1194990/… 了解类似问题。您使用的是哪个 Java 版本,哪个 JDBC 驱动程序(版本,下载自)?
  • 我已经用 postgresql 驱动程序检查了你的代码,它工作正常,可能你使用的驱动程序不是“完整的”;)
  • @AndreasFester : 27 的行是Class.forName(dbDriver);,我不确定在创建驱动程序时是否发生了与什么不一致的事情Java 期望。我知道,由于堆栈跟踪,异常被抛出。

标签: java oracle jdbc


【解决方案1】:

我认为您的问题是由于您使用 Java Connection#getSchema()。

使用最新版本的 JDBC 驱动程序来避免此类问题,或者至少使用与您的 Java 版本兼容的 JDBC 驱动程序。

【讨论】:

  • 有道理,让我试试更新的驱动程序,我会回帖。
  • 做到了!我使用的是 ojdbc6_g.jar,下载和引用较新的 ojdbc7_g.jar 允许我使用该方法。感谢您的帮助!
【解决方案2】:

这适用于 DB2(和旧版本的驱动程序)

Connection.getMetaData().getConnection().getSchema();

【讨论】:

    猜你喜欢
    • 2019-03-02
    • 2023-03-07
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2016-01-09
    • 2016-11-30
    • 2020-07-27
    • 2013-03-03
    相关资源
    最近更新 更多