首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

判断用户名是否可用,帮帮忙,多谢了

2012-03-13 
判断用户名是否可用,帮帮忙,谢谢了!一个用户注册,输入用户名,在页面上点击按钮,提交到数据库中,判断这个用

判断用户名是否可用,帮帮忙,谢谢了!
一个用户注册,输入用户名,在页面上点击按钮,提交到数据库中,判断这个用户名是否可用,然后在页面,弹出一条信息。数据库是SQL,请问这个怎么实现啊,帮帮忙了啊,谢谢了!!

[解决办法]
getUser(String loginName) {

UserVO retval = null;

Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {

String sql =
"Select LOGINID From TBLOGIN LG ";
sql +=
" Where LOGINNAME = ? ";

conn = ConnectionPool.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, loginName);

rs = pstmt.executeQuery();

if (rs.next())
{
request.setAttribute( "error ", new ErrorMessage( "用户名已经存在!请重新输入! "));
return mapping.findForward( "error ");

}、、后台逻辑
[解决办法]
<jsp:useBean id= "DBconn " class= "Bean.conn.DBconn " scope= "page "/>
<%

String username=request.getParameter( "user ");
String password=request.getParameter( "pwd ");

String sql= "select * from users where USERS= ' "+username+ " ' And PWD= ' "+password+ " ' ";
ResultSet rs=DBconn.executeQuery(sql);
if(rs.next())
{

out.print( " <script> ");
out.print( "alert( '登陆成功! '); ");
out.print( "window.location.href= 'all.jsp '; ");
out.print( " </script> ");

}

else{

out.print( " <script> ");
out.print( "alert( '用户名或者密码错误,请重新登陆 '); ");
out.print( "window.location.href= 'index.htm '; ");
out.print( " </script> ");
}
rs.close();
DBconn.closeStmt();
DBconn.closeConn();
%>

DBconn.java :


package Bean.conn;

import java.sql.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class DBconn
{

String sDBDriver;
String sConnStr;
private Connection conn;
private Statement stmt;
ResultSet rs;

public DBconn()
{

try
{
Class.forName( "org.logicalcobwebs.proxool.ProxoolDriver ");
//Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver ").newInstance();
//conn=java.sql.DriverManager.getConnection( "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=caiwu2;SelectMethod=direct ", "db2admin ", "pass1009 ");
conn = DriverManager.getConnection( "proxool.Jxva ");
}
catch(Exception e)
{
System.err.println( "guest(): " + e.getMessage());
}
}

public ResultSet executeQuery(String s)
{
rs = null;
try
{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(s);
}
catch(SQLException sqlexception)
{
System.err.println( "查询出错: " + sqlexception.getMessage()+ "出错语句为: "+s);
}
return rs;
}

public void executeUpdate(String s)
{

try
{

PreparedStatement statement = conn.prepareStatement(s);


statement.executeUpdate();
}
catch(SQLException sqlexception)
{
System.err.println( "执行出错: " + sqlexception.getMessage()+ "出错语句为: "+s);
}
}

public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
}
}

public void closeConn()
{
try
{
conn.close();
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
}
}

public String getTime()
{
String s = " ";
try
{
SimpleDateFormat simpledateformat = new SimpleDateFormat( "yyyy-M-d HH:ss ");
s = simpledateformat.format(new Date());
}
catch(Exception exception)
{
System.out.println(exception.getMessage());
}
return s;
}

public String ex_chinese(String str)
{
if(str == null)
str = " ";
else
try
{
str = new String(str.getBytes( "iso-8859-1 "));
}
catch(Exception ex) { }
return str;
}
}



[解决办法]
首先在servlet中获得数据库中的用户列表,然后在jsp中做判断,使用alert弹出提示框
[解决办法]
写一个验证类,大概内容就是一个反查,就是根据用户所添的用户名你在数据库中进行查询
在JSP页面中,实例化验证类,如过用户输入的用户名在数据库中已经存在,则输出javascript代码,alert出一个对话框提示不可用,也可以直接out.print( "不可用 ");如果可用,则让他接着往下走。
这块你要是会AJAX的话,做出来的效果会更好!介意你学习一下!
[解决办法]
<%=error==null? " ":error.getMessage()%>
有问题!用标准标签库输出
[解决办法]
用Ajax做比较好你可以在用户输入框的onblur中向业务层传入用户名,然后业务层会给你返回一个responseXML 或responseText,通过判断你就可以知道是否可用了,
public String isExist(String usrname);
public List getUser();
[解决办法]
up ls
[解决办法]
用ajax就可以了
传递参数给jsp页面或者servlet都可以
<script language=javaScript>
function checkUser(valueid,msgobj){
var http_request=false;
var valueobj=document.getElementById(valueid);
if(window.XMLHttpRequest){
http_request=new XMLHttpRequest();
if(http_request.overrideMimeType){
http_request.overrideMimeType( "text/html ");
}
}
else if(window.ActiveXObject){
try{
http_request=new ActiveXObject( "Xsxml2.XMLHTTP ");
}
catch(e){
try{
http_request=new ActiveXObject( "Microsoft.XMLHTTP ");
}
catch(e){
}
}
}
if(!http_request){
msgobj.innerHTML= " <font color=red> <b> 您的浏览器不支持XMLhttp,系统无法检测用户名的可用性! </b> </font> ";
return false;
}
http_request.onreadystatechange=function processRequest(){
if(http_request.readyState <4){msgobj.innerHTML= " <font color=green> <b> 系统正在检测,请稍等…… </b> </font> ";}
if(http_request.readyState==4){
if(http_request.status==200){
if(http_request.responseText==1){


msgobj.innerHTML= " <font color=green> <b> 该用户名可以注册. </b> </font> ";
document.reg.pass.readOnly=false;

}
else{
msgobj.innerHTML= " <font color=red> <b> 该用户名已经被使用,请另外选择! </b> </font> ";}

}
else{
msgobj.innerHTML= " <font color=red> <b> 页面出现异常,检测被中止,系统已经中止注册! </b> </font> ";
}
}
};
http_request.open( "GET ", "checkUser.jsp?username= "+valueobj.value,true);
http_request.send(null);
}
</script>
<input type= "text " id= "username "> <div id= "msg "> </div>
<input type= "button " value= "检测用户名 " onclick= "checkUser( "username ", "msg ") ">

热点排行