惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - 传说一梦

在class文件中读取Tomcat站点的根目录 我的新Blog文章 今天,我正式更换了自己的Blog空间 介绍两种国内访问Blogger.com的方式 absolute方式并不是好的Oracle分页查询方式 谈谈Ajax的“异步” 微软也会存在的Ajax误区——强制刷新页面内容 Scriptaculous 中的 "... getStyle is not a function." 错误 SVN Web Client中文修订版下载 词点——点评词汇,共享您的智慧! [链接] Ajax相关 推荐两款Firefox加速软件 取当月数据的算法 关于Linux下C语言编译器gcc不认识bool类型的问题 [专题] Ajax三种数据格式的读取实例 关于Ajax中XMLHttpRequest对象的status属性值问题 Window Live Toolbar 初体验 贵的比便宜的好? 庆祝杭州移动电视诞生一周年
Java中从Orcle里取出数据时,为什么提示“无效的列索引”
传说一梦 · 2006-06-03 · via 博客园 - 传说一梦

  第一次在Java中用JDBC连接Oracle数据库,连接通了,但是一个简单的SQL查询,却报告“无效的列索引”。

  开始,我以为是Oracle里的表索引没有设置。于是,我给要查询的表添加了索引。但是,还是报同样的错。

  记得,前段时间,我用JSP中用JDBC连接过Oracle数据库,SQL查询查出来过记录。只是当时用的数据表不同。于是,我又试着用与JSP中同样的代码,同样的数据表再测了一次。结果,还是不行。

  没办法,只得请教一位公司里的JAVA高手,让她帮忙看一下。原来,是我取字段的时候,字段序号写错了。高手就是高手,一看就看出来了。

  错误代码如下:

            ……
            ResultSet rs = null;               
            try {
                    ……
                    if (rs.first()) {
                            String col = rs.getString(0);   // 错误代码处
                    }                   
                }               
            }
            ……

  完整的正确代码如下:

            // 省略数据库连接部分

            String sqlquery = "select * from users";           
             
            PreparedStatement ps = null;
            ResultSet rs = null;               
            try {
                    ps = conn.prepareStatement(sqlquery,
                                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                                    ResultSet.CONCUR_READ_ONLY);
           
                    rs = ps.executeQuery();
                    if (rs.first()) {
                            String col = rs.getString(1);   // 修改后的正确代码
                    }                   
                }               
            } catch (SQLException se) {
                throw se;
            } catch (Exception ex) {
                throw ex;
            } finally {
                if (rs != null)
                    rs.close();
                if (ps != null)
                    ps.close();
            }

  从上面的代码可以看出,ResultSet类型的getString(int index)方法,是以1开始的。我用的是0,所以出错了。

VB中数字以1开关,Java同C++一样,数字一般以0开始。而像这样突然在Java里面出现以1开始的情况,确实很难想到。因此,虽然只是一字之错,但我还是把它写了下来。