日期:2014-05-18  浏览次数:20714 次

判断用户名是否可用,帮帮忙,谢谢了!
一个用户注册,输入用户名,在页面上点击按钮,提交到数据库中,判断这个用户名是否可用,然后在页面,弹出一条信息。数据库是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);