//此方法用于获取大段文本,
//将其中的回车换行替换为<br>
//输出到html页面
public String rs_getHtmlString(String column)
{
try{
String str1 = this.rs.getString(column);
String str2 = "\r\n";
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();
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 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());
}
}
}
声明:因为使用的是MySQL数据库,所以需要MySQL数据库的驱动
下载后请将org包放至DBConn.java所在目录下
以确保该bean能正常运行
三、编写用户注册的bean:reg.java
//reg.java
//import required classes
import java.sql.*;
public class reg
{
public int newID = 0;
public boolean result = false;
public boolean reg(String username,String password,String confirm,String email)
{
try{
if(!this.checkUser(username))
return false;
if(!this.checkPwd(password))
return false;
if(!this.verifyPwd(password,confirm))
return false;
if(!this.checkEmail(email))
return false;
if(!this.userNotExit(username))
return false;
this.getNewID();
this.result = this.register(username,password,confirm,email);
return this.result;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}//End boolean reg
public boolean checkUser(String user)
{
try{
if(user.indexOf("'")!=-1)
{
System.out.println("姓名中含有非法字符!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean checkPwd(String pwd)
{
try{
if(pwd.indexOf("'")!=-1)
{
System.out.println("密码中含有非法字符!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean verifyPwd(String pwd,String confirm)
{
try{
if(!pwd.equals(confirm))
{
System.out.println("两次输入的密码不一致!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean checkEmail(String email)
{
try{
if(email.indexOf("'")!=-1)
{
System.out.println("E-mail中含有非法字符!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public boolean userNotExit(String user)
{
try{
DBConn userDBConn = new DBConn();
userDBConn.executeQuery("select * from tbl_user where");
if(userDBConn.rs_next())
{
System.out.println("用户名已存在,请选择其它的用户名!");
return false;
}else
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}
public int getNewID()
{
try{
DBConn newIDDBConn = new DBConn();
newIDDBConn.executeQuery("select * from tbl_user order by id desc limit 1");
if(newIDDBConn.rs_next())
{
this.newID = newIDDBConn.rs_getInt("id") + 1;
System.out.println(this.newID);
}else{
this.newID = 1;
}
return this.newID;
}catch(Exception e){
System.out.println(e.toString());
return -1;
}
}
public int getID()
{
return this.newID;
}
public boolean register(String username,String password,String confirm,String email)
{
try{
DBConn regDBConn = new DBConn();
String strSQL = "insert into tbl_user(id,name,pwd,email) values('" + this.newID +"','" + username + "','" + password + "','" + email + "')";
regDBConn.execute(strSQL);
return true;
}catch(Exception e){
System.out.println(e.toString());
return false;
}
}