用ajax检测用户名是否存在从mysql数据库里查询版本
大体思路是这样:本案例利用三层架构模式,但是service层没有过多的业务逻辑,只是规范架构而已
第一步:创建一个add.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 5 <html> 6 <head> 7 8 <title>验证插入的ID是否可以使用</title> 9 10 <script type="text/javascript" 11 src="${pageContext.request.contextPath}/my.js"></script> 12 13 <script type="text/javascript"> 14 window.onload = function() { 15 //得到id=t的文本框 16 var txt = document.getElementById("id"); 17 18 19 20 //给文本框注册一个失去焦点事件 21 txt.onblur = function() { 22 23 //获取文本框中的信息 24 var value = txt.value; 25 26 if(value==null||value==""){ 27 28 document.getElementById("s").innerHTML = "id不能为空"; 29 txt.focus(); 30 }else{ 31 32 //第一步:得到XMLHttpRequest对象. 33 var xmlhttp = getXmlHttpRequest(); 34 //2.设置回调函数 35 xmlhttp.onreadystatechange = function() { 36 37 //5.处理响应数据 当信息全部返回,并且是成功 38 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 39 40 var msg = xmlhttp.responseText; 41 42 document.getElementById("s").innerHTML = msg; 43 44 } 45 }; 46 47 xmlhttp.open("POST", "${pageContext.request.contextPath}/CheakId"); 48 xmlhttp.setRequestHeader("content-type", 49 "application/x-www-form-urlencoded"); 50 51 xmlhttp.send("id=" + value); 52 } 53 } 54 }; 55 </script> 56 57 </head> 58 59 <body> 60 <form action="${pageContext.request.contextPath}/Add" method="post"> 61 <table> 62 <tr> 63 <td>添加的id号:</td> 64 <td><input type="text" id="id" name="id"><span id="s"></span></td> 65 </tr> 66 <tr> 67 <td>添加的name:</td> 68 <td><input type="text" name="name"></td> 69 </tr> 70 <tr> 71 <td>添加的authod:</td> 72 <td><input type="text" name="authod"></td> 73 </tr> 74 <tr> 75 <td>添加的price:</td> 76 <td><input type="text" name="price"></td> 77 </tr> 78 <tr> 79 <td>添加的number:</td> 80 <td><input type="text" name="number"></td> 81 </tr> 82 <tr> 83 84 <td><input type="submit" value="添加"> 85 </td> 86 </tr> 87 88 </table> 89 </form> 90 </body> 91 </html>
my.js里判断了浏览器版本,并创建相应的xmlHttpRequest对象
1 function getXmlHttpRequest() { 2 var xmlhttp = null; 3 4 if (window.XMLHttpRequest) { 5 xmlhttp = new XMLHttpRequest(); // 针对于现在的浏览器包括IE7以上版本 6 } else if (window.ActiveXObject) { 7 // 针对于IE5,IE6版本 8 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 9 } 10 return xmlhttp; 11 }
第二步,创建addServlet.servlet
package com.web; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.service.AddService; public class AddServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setHeader("Content-Type", "text/html;charset=utf-8"); String id = request.getParameter("id"); String name = request.getParameter("name"); String authod = request.getParameter("authod"); String price = request.getParameter("price"); int number = Integer.valueOf(request.getParameter("number")); AddService as = new AddService(); try { boolean b = as.add(id,name,authod,price,number); if(b){ response.sendRedirect(request.getContextPath()+"/FindAllBooks"); }else{ response.getWriter().write("添加失败,请检查你添加的ID是否重复"); } } catch (SQLException e) { response.getWriter().write("添加失败"); e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
第三步:创建addService.java
1 package com.service; 2 3 import java.sql.SQLException; 4 5 import com.dao.AddDao; 6 7 public class AddService { 8 9 public boolean add(String id, String name, String authod, String price, 10 int number) throws SQLException { 11 12 AddDao ad = new AddDao(); 13 14 15 return ad.add(id,name,authod,price,number); 16 } 17 18 }第四步:创建addDao.java
1 package com.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.ArrayList; 8 import java.util.List; 9 10 import com.mchange.v2.c3p0.ComboPooledDataSource; 11 12 public class AddDao { 13 14 public boolean add(String id, String name, String authod, String price, 15 int number) throws SQLException { 16 17 ComboPooledDataSource cds = new ComboPooledDataSource(); 18 19 Connection con = cds.getConnection(); 20 21 List<String> ids = new ArrayList<String>(); 22 23 String checkIdSql = "select * from books"; 24 PreparedStatement checkIdpst = con.prepareStatement(checkIdSql); 25 26 ResultSet checkIdrs = checkIdpst.executeQuery(); 27 28 while (checkIdrs.next()) { 29 30 ids.add(checkIdrs.getString("id")); 31 32 } 33 34 System.out.println("dao中查到的ids::::" + ids); 35 36 System.out.println("---Dao:::" + id + name + authod + price + number); 37 38 String sql = "insert into books(id,name,authod,price,number) values(?,?,?,?,?)"; 39 40 PreparedStatement pst = con.prepareStatement(sql); 41 42 pst.setString(1, id); 43 pst.setString(2, name); 44 pst.setString(3, authod); 45 pst.setString(4, price); 46 pst.setInt(5, number); 47 48 boolean b = ids.contains(id); 49 50 if (b == false) { 51 52 int i = pst.executeUpdate(); 53 54 // 包含是真 ,不包含是假 55 if ((i != 0) && (b == false)) { 56 57 return true; 58 59 } else { 60 61 return false; 62 } 63 64 } else { 65 66 return false; 67 68 } 69 70 } 71 72 }
声明:Dao中用到了c3p0连接数据库技术
在src目录下添加c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///bookmanagesystem</property> <property name="user">root</property> <property name="password">123456</property> </default-config> </c3p0-config>
此外项目要导入数据库所需要的jar包,自行导入即可。
效果如图所示:
在这里,我的addServlet.servlet里添加成功后,转到了一个查询的servlet里
response.sendRedirect(request.getContextPath()+"/FindAllBooks");
这里你可以自行转到相应页面,提示添加成功与否。