使用JSP来实现用户的登陆注册的实例资料.docx

上传人:b****7 文档编号:23997067 上传时间:2023-05-23 格式:DOCX 页数:24 大小:32.16KB
下载 相关 举报
使用JSP来实现用户的登陆注册的实例资料.docx_第1页
第1页 / 共24页
使用JSP来实现用户的登陆注册的实例资料.docx_第2页
第2页 / 共24页
使用JSP来实现用户的登陆注册的实例资料.docx_第3页
第3页 / 共24页
使用JSP来实现用户的登陆注册的实例资料.docx_第4页
第4页 / 共24页
使用JSP来实现用户的登陆注册的实例资料.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

使用JSP来实现用户的登陆注册的实例资料.docx

《使用JSP来实现用户的登陆注册的实例资料.docx》由会员分享,可在线阅读,更多相关《使用JSP来实现用户的登陆注册的实例资料.docx(24页珍藏版)》请在冰豆网上搜索。

使用JSP来实现用户的登陆注册的实例资料.docx

使用JSP来实现用户的登陆注册的实例资料

本例需要的软件和运行环境:

1、Windows2000Server操作系统

2、jdk1.4

3、JCreator2.5(java源码编辑调试器,吐血推荐!

4、MacromediaJRunMX

5、MacromediaDreamweaverMX(非必需)

6、MySQL数据库(最好安装MySQLControlCenter)

一、数据库设计

用MySQLControlCenter打开MySQL数据库,新建数据库shopping,在其下新建表tbl_user,其中各字段设置如下:

二、编写连接数据库bean:

DBConn.java

//DBConn.java

//includerequiredclasses

importjava.sql.*;

//==========================================

//DefineClassDBConn

//==========================================

publicclassDBConn

{

 publicStringsql_driver="org.gjt.mm.mysql.Driver";

 publicStringsql_url="jdbc:

mysql:

//localhost:

3306";

 publicStringsql_DBName="shopping";

 publicStringuser="sa";

 publicStringpwd="";

 Connectionconn=null;

 Statementstmt=null;

 ResultSetrs=null;

 publicbooleansetDriver(Stringdrv)

 {

 this.sql_driver=drv;

 returntrue;

 }

 publicStringgetDriver()

 {

 returnthis.sql_driver;

 }

 publicbooleansetUrl(Stringurl)

 {

 this.sql_url=url;

 returntrue;

 }

 publicbooleansetDBName(Stringdbname)

 {

 this.sql_DBName=dbname;

 returntrue;

 }

 publicStringgetDBName()

 {

 returnthis.sql_DBName;

 }

 publicbooleansetUser(Stringuser)

 {

 this.user=user;

 returntrue;

 }

 publicStringgetUser()

 {

 returnthis.user;

 }

 publicbooleansetPwd(Stringpwd)

 {

 this.pwd=pwd;

 returntrue;

 }

 publicStringgetPwd()

 {

 returnthis.pwd;

 }

 publicDBConn()

 {

 try{

  Class.forName(sql_driver);//加载数据库驱动程序

  this.conn=DriverManager.getConnection(sql_url+"/"+sql_DBName+"?

user="+user+"&password="+pwd+"&useUnicode=true&characterEncoding=gb2312");

  this.stmt=this.conn.createStatement();

 }catch(Exceptione){

  System.out.println(e.toString());

 }

 }

               //执行查询操作

 publicResultSetexecuteQuery(StringstrSql)

 {

 try{

  this.rs=stmt.executeQuery(strSql);

  returnthis.rs;

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnnull;

 }catch(NullPointerExceptione){

  System.out.println(e.toString());

  returnnull;

 }

 }

               //执行数据的插入、删除、修改操作

 publicbooleanexecute(StringstrSql)

 {

 try{

  if(this.stmt.executeUpdate(strSql)==0)

   returnfalse;

  else

   returntrue;

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnfalse;

 }catch(NullPointerExceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

               //结果集指针跳转到某一行

 publicbooleanrs_absolute(introw)

 {

 try{

  this.rs.absolute(row);

  returntrue;

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

 publicvoidrs_afterLast()

 {

 try{

  this.rs.afterLast();

 }catch(SQLExceptione){

  System.out.println(e.toString());

 }

 }

 publicvoidrs_beforeFirst()

 {

 try{

  this.rs.beforeFirst();

 }catch(SQLExceptione){

  System.out.print(e.toString());

 }

 }

 publicvoidrs_close()

 {

 try{

  this.rs.close();

 }catch(SQLExceptione){

  System.out.print(e.toString());

 }

 }

 publicvoidrs_deleteRow()

 {

 try{

  this.rs.deleteRow();

 }catch(SQLExceptione){

  System.out.print(e.toString());

 }

 }

 publicbooleanrs_first()

 {

 try{

  this.rs.first();

  returntrue;

 }catch(SQLExceptione){

  System.out.print(e.toString());

  returnfalse;

 }

 }

 publicStringrs_getString(Stringcolumn)

 {

 try{

  returnthis.rs.getString(column);

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnnull;

 }

 }

               //此方法用于获取大段文本,

               //将其中的回车换行替换为

               //输出到html页面

 publicStringrs_getHtmlString(Stringcolumn)

 {

 try{

  Stringstr1=this.rs.getString(column);

  Stringstr2="\r\n";

  Stringstr3="
";

  returnthis.replaceAll(str1,str2,str3);

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnnull;

 }

 }

 

               //把str1字符串中的str2字符串替换为str3字符串

 privatestaticStringreplaceAll(Stringstr1,Stringstr2,Stringstr3)

 {

 StringBufferstrBuf=newStringBuffer(str1);

    intindex=0;

 while(str1.indexOf(str2,index)!

=-1)

 {

  index=str1.indexOf(str2,index);

  strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);

  index=index+str3.length();

   str1=strBuf.toString();

 }

 returnstrBuf.toString();

 }

 publicintrs_getInt(Stringcolumn)

 {

 try{

  returnthis.rs.getInt(column);

 }catch(SQLExceptione){

  System.out.println(e.toString());

  return-1;

 }

 }

 publicintrs_getInt(intcolumn)

 {

 try{

  returnthis.rs.getInt(column);

 }catch(SQLExceptione){

  System.out.println(e.toString());

  return-1;

 }

 }

 publicbooleanrs_next()

 {

 try{

  returnthis.rs.next();

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

               //判断结果集中是否有数据

 publicbooleanhasData()

 {

 try{

  booleanhas_Data=this.rs.first();  

  this.rs.beforeFirst();

  returnhas_Data;

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

 publicbooleanrs_last()

 {

 try{

  returnthis.rs.last();

 }catch(SQLExceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

 publicbooleanrs_previous()

 {

 try{

  returnthis.rs.previous();

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

               //main方法,调试用

 publicstaticvoidmain(Stringargs[])

 {

 try{

  DBConnmyconn=newDBConn();

  //myconn.setDBName("shopping");

  //myconn.DBConn();

  //myconn.execute("InsertIntotbl_test(id,name)values('10','shandaer')");

  //myconn.execute("Updatetbl_testsetname='yyyyyyyyyyyy'whereid=10");

  //myconn.execute("Deletefromtbl_testwhereid=1");

  ResultSetrs=myconn.executeQuery("select*fromtbl_userorderbyiddesclimit1");

  //booleanhasData=myconn.hasData();

  //System.out.println("hasdata:

"+hasData);

  //rs.first();

  while(myconn.rs.next()) 

  {

   intid=myconn.rs_getInt("id")+1;

   System.out.print(id);

   System.out.println(myconn.rs_getInt("id")+myconn.rs_getString("name"));

   

   //System.out.println('\n'+myconn.rs_getHtmlString("name"));

   //System.out.println(myconn.rs.getString("name")+myconn.rs_getInt

(1));

  }

 }catch(Exceptione){

  System.err.println(e.toString());

 }

 }

 

}

声明:

因为使用的是MySQL数据库,所以需要MySQL数据库的驱动

下载后请将org包放至DBConn.java所在目录下

以确保该bean能正常运行

 

三、编写用户注册的bean:

reg.java

//reg.java

//importrequiredclasses

importjava.sql.*;

publicclassreg

{

 publicintnewID=0;

 publicbooleanresult=false;

 publicbooleanreg(Stringusername,Stringpassword,Stringconfirm,Stringemail)

 {

 try{

  if(!

this.checkUser(username))

   returnfalse;

  if(!

this.checkPwd(password))

   returnfalse;

  if(!

this.verifyPwd(password,confirm))

   returnfalse;

  if(!

this.checkEmail(email))

   returnfalse;

  if(!

this.userNotExit(username))

   returnfalse;

  this.getNewID(); 

  this.result=this.register(username,password,confirm,email);

  returnthis.result;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }//Endbooleanreg

 

 publicbooleancheckUser(Stringuser)

 {

 try{  

  if(user.indexOf("'")!

=-1)

  {

   System.out.println("姓名中含有非法字符!

");

   returnfalse;

  }else

   returntrue;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

  }

 }

 

 publicbooleancheckPwd(Stringpwd)

 {

 try{

  if(pwd.indexOf("'")!

=-1)

  {

   System.out.println("密码中含有非法字符!

");

   returnfalse;

  }else

   returntrue;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

 

 publicbooleanverifyPwd(Stringpwd,Stringconfirm)

 {

 try{

  if(!

pwd.equals(confirm))

  {

   System.out.println("两次输入的密码不一致!

");

   returnfalse;

  }else

   returntrue;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

 

 publicbooleancheckEmail(Stringemail)

 {

 try{

  if(email.indexOf("'")!

=-1)

  {

   System.out.println("E-mail中含有非法字符!

");

   returnfalse;

  }else

   returntrue;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

 }

 }

 

 publicbooleanuserNotExit(Stringuser)

 {

 try{

  DBConnuserDBConn=newDBConn();

  userDBConn.executeQuery("select*fromtbl_userwherename='"+user+"'");

  if(userDBConn.rs_next())

  {

   System.out.println("用户名已存在,请选择其它的用户名!

");

   returnfalse;

  }else

   returntrue;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

  }

 }

 

 publicintgetNewID()

 {

 try{

  DBConnnewIDDBConn=newDBConn();

  newIDDBConn.executeQuery("select*fromtbl_userorderbyiddesclimit1");

  if(newIDDBConn.rs_next())

  {

   this.newID=newIDDBConn.rs_getInt("id")+1;

   System.out.println(this.newID);

  }else{

   this.newID=1;

  }

  returnthis.newID;

 }catch(Exceptione){

  System.out.println(e.toString());

  return-1;

  }   

 }

 

 publicintgetID()

 {

 returnthis.newID;

 }

 

 publicbooleanregister(Stringusername,Stringpassword,Stringconfirm,Stringemail)

 {

 try{

  DBConnregDBConn=newDBConn();

  StringstrSQL="insertintotbl_user(id,name,pwd,email)values('"+this.newID+"','"+username+"','"+password+"','"+email+"')";

  regDBConn.execute(strSQL);

  returntrue;

 }catch(Exceptione){

  System.out.println(e.toString());

  returnfalse;

  }

 }

 publicstaticvoidmain(Stringargs[])

 {

 try{

  

  regnewreg=newreg();  

  

  System.out.println(newreg.reg("sssssssss","ssssss","ssssss","imagebear@"));

  

  DBConnmyconn=newDBConn();

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1