首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在运行时抛出AbstractMethodError与java.sql.Connection

问在运行时抛出AbstractMethodError与java.sql.Connection
EN

Stack Overflow用户
提问于 2016-06-28 18:29:56
回答 2查看 11.1K关注 0票数 5

我试图通过创建一个非常简单的类来将查询发送到OracleXE11g的一个实例,实质上是使一个非常简单的*Plus来获取JDBC的基础知识。

我目前使用的源代码是:

代码语言:javascript
复制
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(;;)循环中,我得到以下输出:

代码语言:javascript
复制
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文档 (java.lang.AbstractMethodError),它说只有在以下情况下才能引发此错误:

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

我认为第27行遗漏了一些东西,但我不知道如何处理这个问题,任何指导都会非常有用。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-28 18:50:11

我认为,您的问题是因为您使用了JDBC驱动程序作为JavaJDBC6的一个版本,并且使用了Java7或更高版本,因为方法<= 7中添加了Connection#getSchema()方法。

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

票数 13
EN

Stack Overflow用户

发布于 2020-10-13 11:08:26

这适用于DB2 (以及驱动程序的旧版本)。

代码语言:javascript
复制
Connection.getMetaData().getConnection().getSchema();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38083719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档