以一个验证登陆为例子
这里是界面层一般叫UIL
protected void Button1_Click(object sender, EventArgs e)
{
List<User>Users = BAL.GetUserInfo(txtUserName.Text,txtPassword.Text)
if(Users.Length >0)
{
Response.Write("登陆成功")
}
else
{
Response.Write("登陆失败")
}
}
以下是逻辑层代码,业务逻辑层一般叫BLL
public static List<User>GetUserInfo(string user,string password)
{
string newPassword = GetMD5Hash(password)//这里对密码进行加密处理,数据库中存放的是经过MD5加密后的密,业务逻辑层一般都是处理复杂的逻辑.例如加密逻辑
List<User>Users = DAL.GetUserInfo(user,newPassword)
return Users
}
以下是数据访问层代码,数据访问层一般叫DAL
public static List<User>GetUserInfo(string user,string password)
{
List<User>Users = new List<User>()
string sql = "select * from User where Password = '"+password+"' and User = '"+user+"'"//写where子句的时候把Password放前面.因为Password经过加密,所以可以防止SQL注入攻击
SqlDataAdapter da = new SqlDataAdapter(sql,"这里是数据库连接字符串")
DataSet ds = new DataSet()
da.Fill(ds)
for(int i=0i<ds.Tables[0].Rows.Counti++)
{
User user = new User(ds.Tables[0].Rows[i]["ID"].ToString(),ds.Tables[0].Rows[i]["User"].ToString(),ds.Tables[0].Rows[i]["Password"].ToString())
Users.Add(user)
}
return Users
}
还会有一个Model层.叫做模板层.是数据表结构的印射.Model层是共用层,其他三层都要用到.
比如数据库中有张表User,里面有3个字段ID,User,Password
那么在模板层中应该有一个类,数据库中User表的一行对应一个User对象,一张表对应User对象的集合.
public class User
{
string ID
string User
string Password
//重载构造函数
User(string id,string user,string password)
{
this.ID=id
this.User=user
this.Password=password
}
}
//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释String sql = "select username,password from account"
String user = request.getParameter("user")
String pass = request.getParameter("password")
int j = 0
Connection conn = null
PreparedStatement ps = null
ResultSet rs = null
try {
conn = JDBCTools1.getConnection()
ps = conn.prepareStatement(sql)
rs = ps.executeQuery()
//从表中查询获取所有账户的用户名&密码的ResultSet 对象
while(rs.next()){
int i = 0
String username[] = new String[10]//用户名数组
String password[] = new String[10]//密码数组
username[i] = rs.getString(1)
password[i] = rs.getString(2)
if(user.equals(username[i])&&pass.equals(password[i])){//比对
response.getWriter().print("you are welcome!")
j++
}else if(user.equals(username[i])&&!pass.equals(password[i])){
response.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n")
response.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right")
j++
}else{
continue
}
i++
}
if(j == 0){
response.getWriter().println("Your username may not be properly")
}
} catch (Exception e) {
e.printStackTrace()
}finally{
JDBCTools1.release(rs, ps, conn)
}
//这是我JDBCTools的getConnection方法
getConnection{
String driverClass = oracle.jdbc.driver.OracleDriver
String jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl
//你的数据库的用户名密码
String user = null
String password = null
// 通过反射创建Driver对象
Class.forName(driverClass)
return DriverManager.getConnection(jdbcUrl, user, password)}
//这是我JDBCTools的release方法
public static void release(ResultSet rs, Statement statement,
Connection conn) {
if (rs != null) {
try {
rs.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
if (statement != null) {
try {
statement.close()
} catch (Exception e2) {
e2.printStackTrace()
}
}
if (conn != null) {
try {
conn.close()
} catch (Exception e2) {
e2.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)