?? mregister.java
字號(hào):
/*
* This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/).
*/
package ch05.module;
import java.sql.*;
import java.util.Hashtable;
import javax.servlet.http.*;
import ch05.CommonConst;
/**
* 針對(duì)注冊(cè)頁面的后臺(tái)處理類
* @author ShenYK
* @version 1.0
*/
public class MRegister
{
public boolean registerNewUser ( HttpSession mySession,
String username,
String password,
String realname)
{
//設(shè)置用戶信息
Hashtable myValues = (Hashtable)mySession.getAttribute(CommonConst.VIEWID_REGISTER);
myValues.put("username", username);
myValues.put("realname", realname);
//嘗試注冊(cè)新用戶
try
{
//載入MySQL的JDBC驅(qū)動(dòng)類
Class.forName(CommonConst.DB_DRIVER_CLASSNAME);
//獲得數(shù)據(jù)庫連接
Connection conn = DriverManager.getConnection( CommonConst.DB_CONN_STRING );
Statement stmt = null;
ResultSet rs = null;
try
{
//檢查數(shù)據(jù)庫中是否已經(jīng)有該用戶了
stmt = conn.createStatement();
//執(zhí)行SQL語句
String sQuery = "select count(*) from user where username='" + username + "'";
rs = stmt.executeQuery( sQuery );
rs.next();
int iUserTmp =rs.getInt(1);
//用戶名已經(jīng)存在
if ( iUserTmp > 0 )
{
mySession.setAttribute("errMsg","用戶名已經(jīng)存在!");
return false;
}
sQuery = "insert into user values('" + username + "','" + password + "','"+ realname + "')";
stmt.executeUpdate( sQuery );
mySession.setAttribute("username", username);
mySession.setAttribute("realname",realname );
return true;
}
catch(Exception e)
{
e.printStackTrace();
mySession.setAttribute("errMsg","登錄數(shù)據(jù)庫時(shí)出現(xiàn)錯(cuò)誤!");
return false;
}
finally
{
try
{
rs.close();
stmt.close();
}catch(Exception ex)
{
}
}
}catch(Exception ex)
{
ex.printStackTrace();
mySession.setAttribute("errMsg","登錄數(shù)據(jù)庫時(shí)出現(xiàn)錯(cuò)誤!");
return false;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -