为什么检索不到记录?

adaoke_captain 2006-03-27 10:14:23
我写了一个DAO类,其中有一个boolean existObject(Object obj)方法用来判断指定的用户是否存在代码如下:
public boolean existObject(Object obj) //ojb为传入的用户名
{
System.out.println("existObject(Object obj)");
String userName = (String)obj;
System.out.println(userName);
conn = DBConnect.getConnection();
System.out.println("getConnecgtioned");
try
{ //prop为一个Properties对象用于加载sqlkey.properties文件里的sql语句
prop.load(DBConnect.class.getResourceAsStream("sqlkey.properties"));
System.out.println("sqlkey.properties prop.loaded");
//判断是否存在指定SQL语句
if(prop.containsKey("userDao.selectByUserName"))
{
System.out.println("if prop.containsKey(userDao.selectByUserName true");
//取出指定的SQL语句
String sql = prop.getProperty("userDao.selectByUserName");
System.out.println(sql);
pst = conn.prepareStatement(sql);
System.out.println("psted");
pst.setString(1,userName);
System.out.println("pst.setString(1,userName)ed");
res = pst.executeQuery(); //执行查询
//if(pst.execute())
//{
// res = pst.getResultSet();
// System.out.println("res = pst.getResultSet()");
//}
System.out.println("res.getRow() = "+res.getRow());
// 判断是否只有一条记录
if(res.getRow()==1) is = true;
else is = false;
System.out.println("pst.exectue()ed");
}
else
{ //不存在指定的SQL语句
System.out.println("if prop.containsKey(userDao.selectByUserName false");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if(res!=null) res.close();
if(pst!=null) pst.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
return is;
}
现在的问题是
查询语句已经取出并显示在调试信息里。
select * from userT where userName = ?
指定的用户名也已经传入,也显示在调试信息里了。
可是取出的记录集条数却是0
System.out.println("res.getRow() = "+res.getRow());
res.getRow() = 0
请大家帮我看看问题出在那里???????
...全文
225 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
adaoke_captain 2006-03-29
  • 打赏
  • 举报
回复
是不是和游标类型设置有关?
wangzhiping 2006-03-28
  • 打赏
  • 举报
回复
我觉得上面的代码有几处都写不得不严格,

还有引号,括号没有注意哦!
yuzl32 2006-03-28
  • 打赏
  • 举报
回复
另外你也可以这样判断

if (rs.next())
{
if (rs.next()) is = false; // 不止一条记录
else is = true;
}
else
is = 无记录
yuzl32 2006-03-28
  • 打赏
  • 举报
回复
SQLException - if a database access error occurs or the result set type is TYPE_FORWARD_ONLY
adaoke_captain 2006-03-28
  • 打赏
  • 举报
回复
我执行到rs.last();时为什么出现
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unsupported me
thod: ResultSet.last
这是怎么会事啊?
adaoke_captain 2006-03-28
  • 打赏
  • 举报
回复
哦 要移动到最后一条,在判断当前是第几条记录.
rs 怎么没有直接取得记录集条数的方法呢?
yuzl32 2006-03-28
  • 打赏
  • 举报
回复
方法很多,比如:
rs.last();
if (rs.getRow() == 1) is = true; // 仅有一条记录
else is = false;


adaoke_captain 2006-03-28
  • 打赏
  • 举报
回复
我是想判断是否检索到了指定用户名的那个用户的条记录.
如果用指定的用户名检索到的记录只有一条那么不就说明检索到了指定的用户的记录了吗
我该怎么判断呢?
adaoke_captain 2006-03-28
  • 打赏
  • 举报
回复
大哥救救我吧!我用下面的SQL去取记录集数目
select COUNT(*) from dbo.userT where userName = ?
可执行到int i = res.getInt(1);
就出意外:
java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Invalid operat
ion for the current cursor position.
用res.getString(1);也不行
yuzl32 2006-03-27
  • 打赏
  • 举报
回复
第一次检索后,游标处于第一行之外,getRow自然是0,必须用next()方法移动到下一行才可以取,所以才有
while(rs.next())
{
//处理 ....
}
yuzl32 2006-03-27
  • 打赏
  • 举报
回复
getRow代表当前是第几行.
yuzl32 2006-03-27
  • 打赏
  • 举报
回复
public int getRow() throws SQLException
Retrieves the current row number. The first row is number 1, the second number 2, and so on.

62,614

社区成员

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

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