当前位置:编程学习 > JS >>

在jsp中用bean和servlet联合实现用户注册、登录

作者:imagebear
版权:imagebear

本例需要的软件和运行环境:
1、Windows2000 Server操作系统
2、jdk1.4
3、JCreator2.5(java源码编辑调试器,吐血推荐!)
4、Macromedia JRun MX
5、Macromedia Dreamweaver MX(非必需)
6、MySQL数据库(最好安装MySQL Control Center)

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


二、编写连接数据库bean:DBConn.java


//DBConn.java

//include required classes
import java.sql.*;

//==========================================
// Define Class DBConn
//==========================================
public class DBConn
{
 public String sql_driver = "org.gjt.mm.mysql.Driver";
 public String sql_url = "jdbc:mysql://localhost:3306";
 public String sql_DBName = "shopping";
 public String user = "sa";
 public String pwd = "";

 Connection conn = null;
 Statement stmt = null;
 ResultSet rs = null;

 public boolean setDriver(String drv)
 {
  this.sql_driver = drv;
  return true;
 }

 public String getDriver()
 {
  return this.sql_driver;
 }

 public boolean setUrl(String url)
 {
  this.sql_url = url;
  return true;
 }

 public boolean setDBName(String dbname)
 {
  this.sql_DBName = dbname;
  return true;
 }

 public String getDBName()
 {
  return this.sql_DBName;
 }

 public boolean setUser(String user)
 {
  this.user = user;
  return true;
 }

 public String getUser()
 {
  return this.user;
 }

 public boolean setPwd(String pwd)
 {
  this.pwd = pwd;
  return true;
 }

 public String getPwd()
 {
  return this.pwd;
 }

 public DBConn()
 {
  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(Exception e){
   System.out.PRintln(e.toString());
  }
 }

                //执行查询操作
 public ResultSet executeQuery(String strSql)
 {
  try{
   this.rs = stmt.executeQuery(strSql);
   return this.rs;
  }catch(SQLException e){
   System.out.println(e.toString());
   return null;
  }catch(NullPointerException e){
   System.out.println(e.toString());
   return null;
  }
 }

                //执行数据的插入、删除、修改操作
 public boolean execute(String strSql)
 {
  try{
   if(this.stmt.executeUpdate(strSql) == 0)
    return false;
   else
    return true;
  }catch(SQLException e){
   System.out.println(e.toString());
   return false;
  }catch(NullPointerException e){
   System.out.println(e.toString());
   return false;
  }
 }

                //结果集指针跳转到某一行
 public boolean rs_absolute(int row)
 {
  try{
   this.rs.absolute(row);
   return true;
  }catch(SQLException e){
   System.out.println(e.toString());
   return false;
  }
 }

 public void rs_afterLast()
 {
  try{
   this.rs.afterLast();
  }catch(SQLException e){
   System.out.println(e.toString());
  }
 }

 public void rs_beforeFirst()
 {
  try{
   this.rs.beforeFirst();
  }catch(SQLException e){
   System.out.print(e.toString());
  }
 }

 public void rs_close()
 {
  try{
   this.rs.close();
  }catch(SQLException e){
   System.out.print(e.toString());
  }
 }

 public void rs_deleteRow()
 {
  try{
   this.rs.deleteRow();
  }catch(SQLException e){
   System.out.print(e.toString());
  }
 }

 public boolean rs_first()
 {
  try{
   this.rs.first();
   return true;
  }catch(SQLException e){
   System.out.print(e.toString());
   return false;
  }
 }

 public String rs_getString(String column)
 {
  try{
   return this.rs.getString(column);
  }catch(SQLException e){
   System.out.println(e.toString());
   return null;
  }
 }

                //此方法用于获取大段文本,
                //将其中的回车换行替换为<br>
                //输出到html页面
 public String rs_getHtmlString(String column)
 {
  try{
   String str1 = this.rs.getString(column);
   String str2 = " ";
   String str3 = "<br>";
   return this.replaceAll(str1,str2,str3);
  }catch(SQLException e){
   System.out.println(e.toString());
   return null;
  }
 }
 
                //把str1字符串中的str2字符串替换为str3字符串
 private static String replaceAll(String str1,String str2,String str3)
 {
  StringBuffer strBuf = new StringBuffer(str1);
     int index=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();
  }
  return strBuf.toString();
 }

 public int rs_getInt(String column)
 {
  try{
   return this.rs.getInt(column);
  }catch(SQLException e){
   System.out.println(e.toString());
   return -1;
  }
 }

 public int rs_getInt(int column)
 {
  try{
   return this.rs.getInt(column);
  }catch(SQLException e){
   System.out.println(e.toString());
   return -1;
  }
 }

 public boolean rs_next()
 {
  try{
   return this.rs.next();
  }catch(SQLException e){
   System.out.println(e.toString());
   return false;
  }
 }

                //判断结果集中是否有数据
 public boolean hasData()
 {
  try{
   boolean has_Data = this.rs.first();  
 &

补充:Web开发 , Jsp ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,