我一直在谷歌搜索一段时间,我仍然找不到我的问题的答案.. 这是:我想知道是否有方法或方法来获取数据库的价值,然后比较它...我不确定如何解释它...所以我想我会告诉你我得到了什么远我的代码。 BTW我使用netbean来制作这个程序并使用odbc数据库(mircosoft访问)。我也在代码中使用try catch但是idk如何让它显示..
下面的程序并没有真正按照我想要的方式工作......因为我在比较时遇到了问题。 提前谢谢。
if(request.getParameter("username")!=null && request.getParameter("username") !=""
&& request.getParameter("password")!=null && request.getParameter("password")!=""){
String user = request.getParameter("username").toString();
String pass = request.getParameter("password").toString();
String check = "SELECT AccountType FROM Testing WHERE Username='"+user+"' AND Password ='"+pass+"'";
rs = stmt.executeQuery(check);
String info = rs.getString(check); // trying to get the AccountType and store it into a string
while(rs.next()){
if(info != null && info !=""){ //checks to see if the account exist in the database
if(info.equals("Admin")){ //checks to see if AccountType is "Admin"
response.sendRedirect("AdminConsole.jsp");
}else
response.sendRedirect("UserConsole.jsp");
}else
response.sendRedirect("ErrorPage2.jsp");
}
}else
response.sendRedirect("ErrorPage.jsp");
connection.close();
}
答案 0 :(得分:0)
拜托,请不要这样做。您不应该从代码中执行SQL,也不应该以纯文本格式存储密码。您应该做的是调用一个参数化过程,该过程将用户名/密码作为参数,然后返回角色(或您想要的任何其他内容)。密码仍应至少散列。
像这样:http://www.daniweb.com/software-development/csharp/threads/87556
在MS Access中,存储过程称为存储查询: http://msdn.microsoft.com/en-us/library/aa140021(v=office.10).aspx
相关部分:
PROC:
create procedure ValidateUserLogin
@UserName varchar(30)
, @Password varchar(30)
as
begin
if exists (select * from UsersTable as ut
where ut.UserName = @UserName AND ut.Password = @Password)
select 1;
else
select 0;
end
类
private bool IsValidatedUser( string username, string password ) {
try {
bool rv = false;
using ( SqlConnection con = new SqlConnection( connectionString ) ) {
using ( SqlCommand cmd = new SqlCommand() ) {
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "ValidateUserLogin";
cmd.Parameters.Add( "@UserName", SqlDbType.VarChar, 30 ).Value = username;
cmd.Parameters.Add( "@Password", SqlDbType.VarChar, 30 ).Value = password;
rv = Convert.ToBoolean( cmd.ExecuteScalar() );
con.Close();
}
}
return rv;
}
catch ( Exception ex ) {
// Log errors
throw;
}
}