在Jsp中用Bean和Servlet聯合實現使用者註冊
最後更新:2017-02-28
來源:互聯網
上載者:User
js|servlet|使用者註冊 本例需要的軟體和運行環境:
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());
}
}
}