为什么会出现java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]无效的描述符索引?

cool_sky 2006-03-21 01:54:57
String sql="SELECT station_id,station_name from t_station";
Connection con = null;
PreparedStatement stat = null;
ArrayList infos = new ArrayList();
ResultSet rs = null;

try{
con = Utility.getConnection();
stat = con.prepareStatement(sql);
rs = stat.executeQuery();
while (rs.next()) {
StationInfoBO info = new StationInfoBO();
// System.out.println("station_id:" + rs.getString("STATION_ID"));
//System.out.println("station_name:" + rs.getString("station_name"));
//这里可以打印出station_id和station_name的值,但执行到下一步就取不到值了
String station_id=rs.getString("station_id");////执行到这一步的时候就出现java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]无效的描述符索引??
这是为什么??WATCH里看到的值:"expression" rs.getString("station_id") = <Exception occurred in target VM>
String station_name=rs.getString("station_name");
info.setStation_id(station_id);
info.setStation_name(station_name);

infos.add(info);
}
}catch (Exception e) {
e.printStackTrace();
infos = null;
}
查了很多类似的问题,都说是取数据的时候要按照数据库字段的顺序来取,但我已经是按顺序来取了,为什么还会出现这种问题呢??请各位高手指点一下,谢谢!

而改用jdbc的方法却怎么也联不起来,不知道为什么??
//jdbc连接数据库:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
String url =
"jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=students";
con = DriverManager.getConnection(url,"sa","");

//通过jdbc-odbc连接数据库
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:students";
con =DriverManager.getConnection(url, "sa", "");

...全文
390 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangx1949 2006-03-21
  • 打赏
  • 举报
回复
对。。用索引,rs.getString(1) 试试
isloop 2006-03-21
  • 打赏
  • 举报
回复
很正常啊?按说不应该有问题啊?你用
String station_id=rs.getString(1);
String station_name=rs.getString(2);
试试。
interpb 2006-03-21
  • 打赏
  • 举报
回复
保证字段顺序和访问顺序一致
cool_sky 2006-03-21
  • 打赏
  • 举报
回复
// System.out.println("station_id:" + rs.getString("STATION_ID"));
//System.out.println("station_name:" + rs.getString("station_name"));
这在程序不运行的,只是出错我前面加了一下看看能不能读数据,
实际程序:
while (rs.next()) {
StationInfoBO info = new StationInfoBO();
  String station_id=rs.getString("station_id")//这儿已经出错了
  String station_name=rs.getString("station_name");
  info.setStation_id(station_id);
info.setStation_name(station_name);
}
WiseDragon 2006-03-21
  • 打赏
  • 举报
回复
我觉的这里你已经读了数据了
   // System.out.println("station_id:" + rs.getString("STATION_ID"));
//System.out.println("station_name:" + rs.getString("station_name"));
到这 
String station_id=rs.getString("station_id")
 String station_name=rs.getString("station_name");

你又回头读,
这样写试试-------------------
while (rs.next()) {
StationInfoBO info = new StationInfoBO();
  String station_id=rs.getString("station_id")
  String station_name=rs.getString("station_name");
  System.out.println(station_id);
System.out.println(station_name);
info.setStation_id(station_id);
info.setStation_name(station_name);
}

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧