日期:2014-05-16 浏览次数:20597 次
package com.jj.dao; import java.sql.*; import com.jj.util.DBConnection; public class PersonDAO { public boolean isExist(String username) { Connection conn = DBConnection.getConnection(); try { String query = "SELECT * FROM people WHERE name='" + username + "'"; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query); rs.next(); if (rs.getRow() == 0) { return false; } else { return true; } } catch (SQLException e) { e.printStackTrace(); return true; } } }
package com.jj.dwr; import com.jj.dao.*; public class PersonDAOAjax { public boolean isExist(String username) { PersonDAO personDAO = new PersonDAO(); return personDAO.isExist(username); } }
package com.jj.pojo; public class Person { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.jj.util; import java.sql.Connection; import java.sql.DriverManager; public class DBConnection { public static Connection getConnection() { String uri = "jdbc:mysql://localhost/Test"; String name = "root"; String pwd = ""; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(uri, name, pwd); return conn; } catch (Exception e) { e.printStackTrace(); return null; } } }
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Register</title> </head> <body> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript" src="dwr/interface/PersonDAOAjax.js"></script> <script type="text/javascript"> function show(boolean){ var name = document.getElementById("name"); if(boolean){ ${"error"}.style.visibility = "visible"; name.style.borderStyle = "solid"; name.style.borderColor = "red"; dwr.util.setValue("name",""); } else { ${"error"}.style.visibility = "hidden"; name.style.borderStyle = ""; name.style.borderColor = ""; } } function validate(){ var name = dwr.util.getValue("name"); PersonDAOAjax.isExist(name,show); } </script> <h1> Register </h1> <p> username <input type="text" id="name" onchange="validate()" /> <span id="error" style="visibility: hidden">This username has been used by others</span> <br> password <input type="password" id="password" /> </p> </body> </html>
?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <create javascript="PersonDAOAjax" creator="new"> <param name="class" value="com.jj.dwr.PersonDAOAjax"></param>