当前位置: 技术问答>java相关
对数据库的操作
来源: 互联网 发布时间:2015-09-03
本文导语: 我写了个JAVABEAN,里面有个函数是实现这样功能的: 输入用户名和密码,然后在数据库中查找,如果找不到就返回0,否则返回1 ,可是,,,我应该由什么值来判断能否找到呢?谢谢,如下是我的代码: public int che...
我写了个JAVABEAN,里面有个函数是实现这样功能的:
输入用户名和密码,然后在数据库中查找,如果找不到就返回0,否则返回1 ,可是,,,我应该由什么值来判断能否找到呢?谢谢,如下是我的代码:
public int checkUser(String username, String password ) throws SQLException, Exception
{
int ret;
if (con != null)
{
try
{
// create a prepared SQL statement
PreparedStatement selectuser;
selectuser = con.prepareStatement("SELECT * FROM user WHERE username=? and password=?;");
selectuser.setString(1, username);
selectuser.setString(2, password);
rs=selectuser.execute();
}
catch (SQLException sqle)
{
error = "SQLException: select failed, no vaild username or password";
throw new SQLException(error);
}
}
else
{
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
return ret;
} // end of checkUser()
问题时,在哪行代码处添加ret=1?
输入用户名和密码,然后在数据库中查找,如果找不到就返回0,否则返回1 ,可是,,,我应该由什么值来判断能否找到呢?谢谢,如下是我的代码:
public int checkUser(String username, String password ) throws SQLException, Exception
{
int ret;
if (con != null)
{
try
{
// create a prepared SQL statement
PreparedStatement selectuser;
selectuser = con.prepareStatement("SELECT * FROM user WHERE username=? and password=?;");
selectuser.setString(1, username);
selectuser.setString(2, password);
rs=selectuser.execute();
}
catch (SQLException sqle)
{
error = "SQLException: select failed, no vaild username or password";
throw new SQLException(error);
}
}
else
{
error = "Exception: Connection to database was lost.";
throw new Exception(error);
}
return ret;
} // end of checkUser()
问题时,在哪行代码处添加ret=1?
|
在catch{
return 0;
}
else
{
return 0;
}
return 0;
}
else
{
return 0;
}