?? mlogin.java
字號:
/*
* This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/).
*/
package ch05.module;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import javax.servlet.http.HttpSession;
import ch05.*;
/**
* 針對登錄頁面的后臺處理類
* @author ShenYK
* @version 1.0
*/
public class MLogin
{
public boolean getUserInfo ( HttpSession mySession,
String username,
String password )
{
//設置用戶信息
Hashtable myValues = (Hashtable)mySession.getAttribute(CommonConst.VIEWID_LOGIN);
myValues.put("username", username);
//嘗試查找用戶
try
{
//載入MySQL的JDBC驅動類
Class.forName(CommonConst.DB_DRIVER_CLASSNAME);
//獲得數據庫連接
Connection conn = DriverManager.getConnection( CommonConst.DB_CONN_STRING );
Statement stmt = null;
ResultSet rs = null;
try
{
//檢查數據庫中是否已經有該用戶了
stmt = conn.createStatement();
//執行SQL語句
String sQuery = "select realname from user "
+ "where username='" + username + "' "
+ "and password='" + password +"'";
rs = stmt.executeQuery( sQuery );
if ( rs.next() )
{
mySession.setAttribute("username",username);
mySession.setAttribute("realname",rs.getString(1) );
return true;
}
else
{
mySession.setAttribute("errMsg","用戶名密碼不正確!");
return false;
}
}
catch(Exception e)
{
e.printStackTrace();
mySession.setAttribute("errMsg","登錄數據庫時出現錯誤!");
return false;
}
finally
{
try
{
rs.close();
stmt.close();
}catch(Exception ex)
{
}
}
}
catch(Exception ex)
{
ex.printStackTrace();
mySession.setAttribute("errMsg","登錄數據庫時出現錯誤!");
return false;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -