今天,我来教大家怎么实现Java+Ajax实现用户名重复检验。
实体类代码:package com.hqj.dao;public class User { private int id; private String name; private String password; /** * */ public User() { super(); // TODO Auto-generated constructor stub } /** * @param id * @param name * @param password */ public User(int id, String name, String password) { super(); this.id = id; this.name = name; this.password = password; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; }}复制代码数据库操作类代码:
/** * */package com.hqj.db;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/** * @author HuangQinJian 上午9:21:23 2017年4月24日 */public class DBConnection { public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager .getConnection("jdbc:mysql://localhost/system?user=root&password=729821"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public static PreparedStatement prepare(Connection conn, String sql) { PreparedStatement pstmt = null; try { if (conn != null) { pstmt = conn.prepareStatement(sql); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static PreparedStatement prepare(Connection conn, String sql, int autoGenereatedKeys) { PreparedStatement pstmt = null; try { if (conn != null) { pstmt = conn.prepareStatement(sql, autoGenereatedKeys); } } catch (SQLException e) { e.printStackTrace(); } return pstmt; } public static Statement getStatement(Connection conn) { Statement stmt = null; try { if (conn != null) { stmt = conn.createStatement(); } } catch (SQLException e) { e.printStackTrace(); } return stmt; } public static ResultSet getResultSet(Statement stmt, String sql) { ResultSet rs = null; try { if (stmt != null) { rs = stmt.executeQuery(sql); } } catch (SQLException e) { e.printStackTrace(); } return rs; } public static void executeUpdate(Statement stmt, String sql) { try { if (stmt != null) { stmt.executeUpdate(sql); } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Connection conn) { try { if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(Statement stmt) { try { if (stmt != null) { stmt.close(); stmt = null; } } catch (SQLException e) { e.printStackTrace(); } } public static void close(ResultSet rs) { try { if (rs != null) { rs.close(); rs = null; } } catch (SQLException e) { e.printStackTrace(); } }}复制代码
上面的数据库操作代码相当于一个工具类,大家可以直接使用,不过要记得改数据库账号,密码以及数据库表名:
conn = DriverManager .getConnection("jdbc:mysql://localhost/system?user=root&password=729821");复制代码service类代码:
/** * */package com.hqj.service;import java.util.List;import com.hqj.dao.User;/** * @author HuangQinJian 上午9:26:26 2017年4月24日 */public interface UserService { public String checkUserName(String username);}复制代码serviceImpl类代码:
/** * */package com.hqj.serviceImpl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.List;import com.hqj.dao.User;import com.hqj.db.DBConnection;import com.hqj.service.UserService;/** * @author HuangQinJian 上午9:29:14 2017年4月24日 */public class UserServiceImpl implements UserService { private Connection conn = null; private Statement stmt = null; private PreparedStatement pstmt = null; DBConnection dbConnection = new DBConnection(); @Override public String checkUserName(String username) { conn = DBConnection.getConn(); stmt = DBConnection.getStatement(conn); String sql = "select * from user where name=" + "'" + username + "'"; System.out.println("用户查询时的SQL:" + sql); String str = null; try { pstmt = conn.prepareStatement(sql); if (pstmt.executeQuery().next() == true) { str = "用户名已存在!"; } else { str = "用户名可用!"; } } catch (SQLException e) { e.printStackTrace(); } return str; }}复制代码后台代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="com.hqj.serviceImpl.UserServiceImpl"%><% String username = request.getParameter("username"); UserServiceImpl u = new UserServiceImpl(); out.println(u.checkUserName(username));%>复制代码
前端代码:
利用原生Ajax实现<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ page import="com.hqj.dao.User"%><%@ page import="com.hqj.serviceImpl.UserServiceImpl"%>Insert title here
<%=user.getName()%>
<%=user.getPassword()%>
复制代码在这里有几个点需要注意:
1、要注意创建xmlhttp语句的正确性!
2、xmlhttp.send("username=" + username);是发送给后台的(服务器)的数据!因为后台需要前端传送的数据进行判断!
3、注意区分xmlhttp.responseText与responseXML的区别!
responseText | 获得字符串形式的响应数据。 |
responseXML | 获得 XML 形式的响应数据。 |
如果来自服务器的响应并非 XML,请使用 responseText 属性;如果来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,请使用 responseXML 属性。
因为在我的代码中后台返回的是String类型,所以必须用responseText。我刚开始时就是因为这个出错了!
来一个 responseXML 的例子:
xmlDoc=xmlhttp.responseXML;txt="";x=xmlDoc.getElementsByTagName("ARTIST");for (i=0;i