概述
1.原理
prep1 = conn.prepareStatement("select * from youkexinxi where youkexingming=?");
prep1.setString(1,e.getyoukexingming());
ResultSet rs = prep1.executeQuery();
rs.next()函数
传入需要注册的姓名,使用rs.next()函数返回查询结果,若是有结果,则返回true,若是没有结果,则返回false
2.具体代码实现
2.1daoImpl层
public boolean addyoukexinxi(youkexinxi e) throws Exception {
Connection conn = null;
PreparedStatement prep = null,prep1=null;
String sql ="insert into youkexinxi(youkebianhao,youkexingming,xingbie,shenfengzhenghao,lianxifangshi,youkemima)" +
"values(null,?,?,?,?,?)";
conn = DBUtil.getConnection();
prep1 = conn.prepareStatement("select * from youkexinxi where youkexingming=?");
System.out.println("要注册的姓名是:"+e.getyoukexingming());
prep1.setString(1,e.getyoukexingming());
ResultSet rs = prep1.executeQuery();
System.out.println("查询已注册的结果是:"+rs.next());
if(rs.next()){
//out.println("该用户名已经被注册!");
//response.sendRedirect("register.jsp");
System.out.println("用户已被注册");
DBUtil.close(conn);
return false;
}
else{
prep = conn.prepareStatement(sql);
prep.setString(1, e.getyoukexingming());
prep.setString(2, e.getxingbie());
prep.setString(3, e.getshenfengzhenghao());
prep.setString(4, e.getlianxifangshi());
prep.setString(5, e.getyoukemima());
prep.executeUpdate();
DBUtil.close(conn);
return true;
}
}
在dao层我们可以返回是否添加成功的结果,再在Service层和controller层做进一步的处理。
2.2service层
public Result addyoukexinxi(youkexinxi e) throws Exception {
Result result = new Result();
youkexinxiDaoImpl dao = new youkexinxiDaoImpl();
if(dao.addyoukexinxi(e)==true){
result.setStatus(0);
result.setMessage("添加成功");
result.setData(e);
System.out.println("注册在Service层运行成功");
}
else{
result.setStatus(1);
result.setMessage("添加成功");
result.setData(e);
System.out.println("注册在Service层运行失败,注册失败");
}
return result;
}
将是否查重的结果传递到Service层后,Service继续向下一层传递
2.3controller层
if("zhuce.you".equals(url)) {
System.out.println("进入添加");
youkexinxi e = new youkexinxi();
e.setyoukexingming(request.getParameter("youkexingming"));
e.setxingbie(request.getParameter("xingbie"));
e.setshenfengzhenghao(request.getParameter("shenfengzhenghao"));
e.setlianxifangshi(request.getParameter("lianxifangshi"));
e.setyoukemima(request.getParameter("youkemima"));
System.out.println("游客密码是:"+request.getParameter("youkemima"));
System.out.println("游客的完整信息是:"+e);
Result a=null;
try {
a=service.addyoukexinxi(e);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(a.getStatus()==0){
response.sendRedirect("/demo/registersuccess.jsp");
System.out.println("注册成功");
}
if(a.getStatus()==1){
response.sendRedirect("/demo/reregister.jsp");
System.out.println("重名,注册失败");
}
//response.sendRedirect("/demo/e-login.jsp");
}
用这个状态判断是否重名,再决定转向不同的JSP页面
3.参考
https://zhidao.baidu.com/question/229074276.html
https://zhidao.baidu.com/question/54366814.html
https://zhidao.baidu.com/question/1609697956765503067.html
https://zhidao.baidu.com/question/1366281413639445259.html
最后
以上就是无聊柠檬为你收集整理的JavaWeb检测注册内容是否在数据库中有相同的内容1.原理2.具体代码实现3.参考的全部内容,希望文章能够帮你解决JavaWeb检测注册内容是否在数据库中有相同的内容1.原理2.具体代码实现3.参考所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复