简体   繁体   English

SQL,JDBC“无效列索引”错误,带有简单查询

[英]SQL, JDBC “Invalid column index” error with simple query

I'm new to STRUTS and JDBC, my application tries to connect to a simple DB that has 3 tables, right now all is doing is trying to query 1 table that only stores "first, last names and a Id field" 我是STRUTS和JDBC的新手,我的应用程序尝试连接到具有3个表的简单数据库,现在所有要做的就是尝试查询仅存储“名字,姓氏和ID字段”的1个表

System.out.println("-------- Oracle JDBC Connection Testing ------");

            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            } catch (ClassNotFoundException e) {
                System.out.println("Where is your Oracle JDBC Driver?");
                e.printStackTrace();
                return null;
            }

            System.out.println("Oracle JDBC Driver Registered!");

            try {
                connection = 
                    DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","david","changeit");
            } catch (SQLException e) {
                System.out.println("Connection Failed! Check output console");
                e.printStackTrace();
                return null;
            }

            if (connection != null) {
                System.out.println("You made it, take control your database now!");
            } else {
                System.out.println("Failed to make connection!");
            }

where I would like to get the result of 1 column if a match occurs: 如果发生匹配,我想得到1列的结果:

            String sql = "SELECT S_ID FROM Students WHERE firstname=? AND lastname=?";

            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, firstname);
            ps.setString(2, lastname);

            rs = ps.executeQuery();

            while (rs.next()) {
                studentid = rs.getString(1);
                ret = SUCCESS;
            }
        } catch (Exception e) { ...

As far as I can tell the connection is made, the SQL query 据我所知,已建立连接,SQL查询

Select s_id from Students where firstname='first' and lastname='last';

when run on SQL Dev. 在SQL Dev上运行时。 works and gives me a single result. 工作,并给我一个结果。

I don't really get a stack trace the code just jumps from right before the 'while (rs.next()) {..' directly into the finally block 我并没有真正得到堆栈跟踪,代码只是从“ while(rs.next()){..”之前直接跳转到finally块中

} catch (Exception e) {
        e.printStackTrace();
        ret = ERROR;
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }

I'm not sure how Oracle drivers work. 我不确定Oracle驱动程序如何工作。 But below statement is what i see on Oracle site. 但是下面的陈述是我在Oracle网站上看到的。 Are you getting a non empty resultset ? 您是否得到一个非空的结果集 As you are not getting a nullpointerexception on .next(), i'm wondering if Oracle drivers return an empty ResultSet, which may lead to this problem. 由于您没有在.next()上获得nullpointerexception,因此我想知道Oracle驱动程序是否返回空的ResultSet,这可能导致此问题。

http://docs.oracle.com/cd/B28359_01/java.111/b31224/getsta.htm http://docs.oracle.com/cd/B28359_01/java.111/b31224/getsta.htm

In case of a standard JDBC driver, if the SQL string being executed does not return a ResultSet object, then the executeQuery method throws a SQLException exception. 对于标准JDBC驱动程序,如果正在执行的SQL字符串未返回ResultSet对象,则executeQuery方法将引发SQLException异常。 In case of an Oracle JDBC driver, the executeQuery method does not throw a SQLException exception even if the SQL string being executed does not return a ResultSet object. 对于Oracle JDBC驱动程序,即使正在执行的SQL字符串未返回ResultSet对象,executeQuery方法也不会引发SQLException异常。

Like I said I'm new at using this. 就像我说的那样,我是新手。 The problem was that my schema didn't have the CONNECT role assigned to it. 问题是我的架构没有分配CONNECT角色。

Solution log in as 'SYSTEM' and grant the role to my schema 解决方案以“ SYSTEM”身份登录并将角色授予我的架构

grant connect to MY_SCHEMA;

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

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