在Jsp上用Bean和Servlet联合实现用户注册
发布时间:2023-05-19 15:14:46 所属栏目:教程 来源:
导读:本例需要的软件和运行环境:
1、windows2000 server操作系统
2、jdk1.4
3、jcreator2.5(java源码编辑调试器)
4、macromedia jrun mx
5、macromedia dreamweaver mx(非必需)
6、mysql数据
1、windows2000 server操作系统
2、jdk1.4
3、jcreator2.5(java源码编辑调试器)
4、macromedia jrun mx
5、macromedia dreamweaver mx(非必需)
6、mysql数据
本例需要的软件和运行环境: 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){ system.out.println(e.tostring()); return false; } } //判断结果集中是否有资料 public boolean hasdata() { try{ boolean has_data = this.rs.first(); this.rs.beforefirst(); return has_data; }catch(sqlexception e){ system.out.println(e.tostring()); return false; } } public boolean rs_last() { try{ return this.rs.last(); }catch(sqlexception e){ system.out.println(e.tostring()); return false; } } public boolean rs_previous() { try{ return this.rs.previous(); }catch(exception e){ system.out.println(e.tostring()); return false; } } //main方法,调试用 public static void main(string args[]) { try{ dbconn myconn = new dbconn(); //myconn.setdbname("shopping"); //myconn.dbconn(); //myconn.execute("insert into tbl_test(id,name) values('10','shandaer')"); //myconn.execute("update tbl_test set name='yyyyyyyyyyyy' where id=10"); //myconn.execute("delete from tbl_test where id=1"); resultset rs = myconn.executequery("select * from tbl_user order by id desc limit 1"); //boolean hasdata = myconn.hasdata(); //system.out.println("has data:" + hasdata); //rs.first(); while (myconn.rs.next()) { int id = 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(exception e){ system.err.println(e.tostring()); } } } (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐