Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java.sql.Connection在运行时引发AbstractMethodError_Java_Oracle_Jdbc - Fatal编程技术网

使用java.sql.Connection在运行时引发AbstractMethodError

使用java.sql.Connection在运行时引发AbstractMethodError,java,oracle,jdbc,Java,Oracle,Jdbc,我正试图创建一个非常简单的类,将查询发送到Oracle XE 11g实例,本质上是创建一个非常简单的SQL*Plus,以了解JDBC的基本知识 我目前使用的源代码是: public class Example { static String username, password; static String dbDriver = "oracle.jdbc.driver.OracleDriver"; static String dbConnection = "jdbc:or

我正试图创建一个非常简单的类,将查询发送到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);
            }
        }

    }
}
我检查了(java.lang.AbstractMethodError),它说只有在以下情况下才能抛出此错误:

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


我认为我在第27行中遗漏了一些东西,但我不确定如何解决这个问题,任何指导都会非常有用。

我认为您的问题是因为您在Java版本中使用JDBC驱动程序这在DB2上工作(以及旧版本的驱动程序)


在您的帖子中包含源代码。外部链接最终会坏掉,这篇文章对未来的读者来说毫无价值。未来值是SO的全部要点。请仅添加相关代码-请参阅。最有可能的是,您可以删除大部分代码,但仍会重现问题。顺便说一句,调用
getSchema()
方法时,错误不会在第27行抛出,而是在第69行抛出。最有可能的是JDBC版本中存在一些不匹配的问题-请参阅以了解类似的问题。您使用的是哪个Java版本,哪个JDBC驱动程序(版本,下载自)?我已经用postgresql驱动程序检查了您的代码,它工作正常,可能您使用的驱动程序不“完整”;)@AndreasFester:27行是
Class.forName(dbDriver),我不确定在创建驱动程序时是否发生了与Java预期不一致的事情。我知道,由于堆栈跟踪,引发异常的地方。这是有道理的,让我试试更新的驱动程序,我会发回。成功了!我当时使用的是ojdbc6_g.jar,下载并引用更新版本使我能够使用该方法。谢谢你的帮助!
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)
Connection.getMetaData().getConnection().getSchema();