在Jsp中用Bean和Servlet联合实现用户注册

2008-02-23 07:58:59来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

本例需要的软件和运行环境:
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;
}
}

//此方法用于获取大段文本,
//将其中的回车换行替换为

//输出到html页面
public String rs_getHtmlString(String column)
{
try{
String str1 = this.rs.getString(column);
String str2 = "\r\n";
String str3 = "
";
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){

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:高性能、高弹性的JSP和Servlet的性能优化

下一篇:在JSP-Servlet开发中导入事件驱动技术