用注释的代码就出错?原理不是一样的么?

猪小烧 2007-08-13 01:03:01
public Boolean isHasInDB ( String name , String password )
{
Connection con = connetionDB.getConnetionDB ();
Statement controlDB = null;
//String SQL = "select Password from Card where Id = "+ name;//为什么这样就会出错??????????
String SQL = "select Id from Card where Password = " + password;
try
{
controlDB = con.createStatement ();
ResultSet rsDB = controlDB.executeQuery ( SQL );
while ( rsDB.next () )
{

//if ( rsDB.getString ( 1 ).equals ( password ) )//为什么这样就会出错??????????
if ( rsDB.getString ( 1 ).equals ( name ) )
{
rsDB.close ();
con.close ();
controlDB.close ();
return true;
}
}
rsDB.close ();
con.close ();
controlDB.close ();

}
catch ( SQLException ex )
{
//return false;
ex.printStackTrace ();
}
return false;
}

真的非常奇怪
按密码来查找就没问题

按名字来查找的话就说没有这列...........

我的名字和密码在SQL里的数据类型是carchar(50)

求高人指点迷津
...全文
139 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
quanwei309 2007-08-13
  • 打赏
  • 举报
回复
//String SQL = "select Password from Card where Id = "+ name;//为什么这样就会出错??????????

varchar 型 要加单引号,按密码查询不出错有可能数据库中密码的类型不是varchar,仔细检查一下
"select Password from Card where Id = '"+ name + "'";
"select Id from Card where Password = '"+ Password + "'";
在他后面增加一个输出语句

System.out.println(sql);

把输出的sql复制用数据库查询
比特灵 2007-08-13
  • 打赏
  • 举报
回复
net_lover(【孟子E章】) : 早,,,我早起是被逼的,你呢?
孟子E章 2007-08-13
  • 打赏
  • 举报
回复
应该都会出错的

"select Password from Card where Id = '"+ name + "'";
"select Id from Card where Password = '"+ Password + "'";
比特灵 2007-08-13
  • 打赏
  • 举报
回复
出什么错?
猪小烧 2007-08-13
  • 打赏
  • 举报
回复
楼上的正解,如果值是数字,是varchar也不需要单引号

不过最好还是都单引的好点

谢谢咯

把输出的sql复制用数据库查询
(这条提示很重要,刚开始接触JDBC,感觉有点慌乱..........)

62,623

社区成员

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

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