无法解析"java.sql.SQLException:[Microsoft] [ODBC SQL Server驱动程序]无效的描述符索引";错误 [英] unable to resolve "java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index" error

查看:351
本文介绍了无法解析"java.sql.SQLException:[Microsoft] [ODBC SQL Server驱动程序]无效的描述符索引";错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是尝试从表中获取密码的jsp代码

here is the jsp code which tries to fetch password from a table

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn2");

String U=(String)session.getAttribute("uname");
String query="select pwd from img_pwd where uname='"+U+"'";

Statement s=con.createStatement();
ResultSet r= s.executeQuery(query);
String pas="";
 if(r.next())
        {
             pas=r.getString(2);
        }

con.close();

数据库中的表"img_pwd"像这样-

and the table "img_pwd" in the database goes like this--

  1. (uname,nvarchar(50)
  2. (pwd,nvarchar(20))

因此,如问题标题中所述,我得到错误- java.sql.SQLException:[Microsoft] [ODBC SQL Server驱动程序]无效的描述符索引

Hence as described in the question title, i get the error--java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index

注意::我已经知道结果集代码存在问题,但是我无法弄清楚.

NOTE: i have come to know that there is a problem with my resultset code but i am unable to figure it out.

有人可以引导我吗??? 谢谢.

Can anybody Guide me??? Thanks.

推荐答案

您的SQL正在选择一个字段:

Your SQL is selecting one field:

String query="select pwd from img_pwd where uname='"+U+"'";

但是此行正在查找字段#2(并且您仅选择一个字段).

But this line is looking for field #2 (and you're only SELECTing one).

pas=r.getString(2);

将其更改为此:

pas=r.getString(1);

作为参考,这是

以Java编程语言中String的形式检索此ResultSet对象的当前行中指定列的值. 参数: columnIndex-第一列为1,第二列为2,...

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. Parameters: columnIndex - the first column is 1, the second is 2, ...

这篇关于无法解析"java.sql.SQLException:[Microsoft] [ODBC SQL Server驱动程序]无效的描述符索引";错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆