简体   繁体   English

使用 java.sql.Connection 在运行时抛出 AbstractMethodError

[英]AbstractMethodError thrown at runtime with java.sql.Connection

I'm trying to work through creating a very simple class to send queries to an instance of Oracle XE 11g, essentially making a very simple SQL*Plus to get the basics of JDBC down.我正在尝试创建一个非常简单的类来将查询发送到 Oracle XE 11g 的实例,本质上是创建一个非常简单的 SQL*Plus 来了解 JDBC 的基础知识。

The source code I'm currently working with is:我目前正在使用的源代码是:

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);
            }
        }

    }
}

In the second try/catch block, in the for(;;) loop, I get the following output:在第二个 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)

I checked out the Oracle docs (java.lang.AbstractMethodError), and it says that this error can only be thrown if:我查看了Oracle 文档(java.lang.AbstractMethodError),它说只有在以下情况下才能抛出此错误:

"the definition of some class has incompatibly changed since the currently executing method was last compiled." “自上次编译当前执行的方法以来,某些类的定义发生了不兼容的更改。”

I'm thinking that there's something I'm missing with line 27, but I'm not sure how to approach this problem and any guidance would be extremely helpful.我想我在第 27 行中遗漏了一些东西,但我不确定如何解决这个问题,任何指导都会非常有帮助。

I believe that your problem is due to the fact that you use a JDBC driver for a version of Java <= 6 and you use Java 7 or higher because the method Connection#getSchema() has been added in Java 7.我相信您的问题是由于您使用 Java <= 6 版本的 JDBC 驱动程序并且使用 Java 7 或更高版本,因为方法Connection#getSchema()已在 Java 7 中添加。

Use the latest version of your JDBC driver to avoid such issue or at least a JDBC driver compatible with your version of Java.使用最新版本的 JDBC 驱动程序以避免此类问题,或者至少使用与您的 Java 版本兼容的 JDBC 驱动程序。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM