package com.chen.ying; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class CheckId extends HttpServlet { final String DBDIVRE="com.mysql.jdbc.Driver"; final String URL="jdbc:mysql://localhost:3306/test"; final String USER="root"; final String PASSWORD="1995228"; Connection conn=null; PreparedStatement pstmt=null; ResultSet rs=null; public void doGet(HttpServletRequest req,HttpServletResponse res){ int userid=Integer.parseInt(req.getParameter("userid")); try{ System.out.println(userid); PrintWriter out=res.getWriter();//得到回应的输出流 Class.forName(DBDIVRE); conn=DriverManager.getConnection(URL,USER,PASSWORD); String sql="select userid from person where userid=?"; pstmt=conn.prepareStatement(sql); pstmt.setInt(1, userid); rs=pstmt.executeQuery(); if(rs.next()){ System.out.println("用户ID重复"); out.print("true"); } else{ System.out.println("ID可用"); out.print("false"); } }catch(Exception e){ System.out.println(e); } } public void doPost(HttpServletRequest req,HttpServletResponse res){ this.doGet(req, res); } }