<script language="javascript">
var http_request = false;
function createRequest(url) {
//初始化对象并发出XMLHttpRequest请求
http_request = false;
if (window.XMLHttpRequest) { // Mozilla或其他除IE以外的浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
http_request.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
}
}
if (!http_request) {
alert("不能创建XMLHTTP实例!");
return false;
}
http_request.onreadystatechange = alertContents; //指定响应方法
//发出HTTP请求
http_request.open("GET", url, true);
http_request.send(null);
}
function alertContents() { //处理服务器返回的信息
if (http_request.readyState == 4) {
if (http_request.status == 200||http_request.status==0) {
alert(http_request.responseText);
} else {
alert('您请求的页面发生错误!');
}
}}
function checkName() {
var username = form1.username.value;
if(username=="") {
window.alert("请添写用户名!");
form1.username.focus();
return false;
}
else {
createRequest('checkName.jsp?username='+username);
}
}
</script> <td width="293" id="td1"><input type="text" id="username" name="username"/> <input type="button" value="检测用户名" onClick="checkName()"></td>
通过Onclick调用函数,转入checkName.jsp文件,如下:<%//request.setCharacterEncoding("GB2312");
String username=request.getParameter("username");
try{
Class.forName("com.mysql.jdbc.Driver");
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
// try{
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/hrm","root","root");
/* }catch (SQLException e) {
e.printStackTrace();
}*/
Statement stmt=conn.createStatement();
String sql="select * from userinfo where userName='"+username+"'";
ResultSet rs=stmt.executeQuery(sql);
if (rs.next()){
out.println("很报歉!用户名"+username+"已经被注册!");
}else{
out.println("祝贺您!用户名"+username+"没有被注册!");
}
rs.close();
conn.close();
stmt.close();%>为什么不管我输入什么,都提示没有被注册,我输入的东西数据库有,我用servlet检测时就是正确的,但是用这个不知道为什么不对
怎么去掉这个引号?
这是sql语句
sql="select * from userinfo where userName='"+username+"'";
这个 username 有可能是空值 还有可能是乱码 如果都不是那你就 打印sql语句 到数据库执行下就知道是代码的问题还是数据哭的问题了 如果数据库的sql是对的 那就可能是链接数据库时候的编码跟数据库不一致导致查询出错 你可以用characterEncoding=数据库编码 eg: con = DriverManager.getConnection("jdbc:mysql:///db?characterEncoding=utf-8", "root", "123456"); 先找到问题的所在 希望对你有帮住
select * from userInfo where userName='111'
将他考到mysql中运行,找不到记录,然后我把引号去掉select * from userInfo where userName=111
运行,就可已找到记录
select * from userInfo where userName='111'
select * from userInfo where userName=111
一般这两句都应该能查到数据(中午只有第一句能查到)
你检测下你的 '' 引号 是英文状态下的么
"select * from userInfo where userName=' "+username+" '";
这样写就对了,
"select * from userinfo where userName='"+username+"'";
这样写不对