gpt4 book ai didi

java - 无法解决 "java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"错误

转载 作者:行者123 更新时间:2023-12-02 01:10:29 25 4
gpt4 key购买 nike

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

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”是这样的--

  1. (uname,nvarchar(50)
  2. (密码,nvarchar(20))

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

注意:我已经知道我的结果集代码有问题,但我无法弄清楚。

有人可以指导我吗???谢谢。

最佳答案

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

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

但是这一行正在寻找字段#2(而您只选择一个)。

pas=r.getString(2);

将其更改为:

pas=r.getString(1);

作为引用,这里是 Java doc on the ResultSet interface's getString method .

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 - 无法解决 "java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57690505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com