日期:2014-05-17 浏览次数:20808 次
package com.lyq; import java.io.IOException; import java.io.PrintWriter; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ReServlet extends HttpServlet { private Connection conn; public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); String userName=request.getParameter("userName"); String passWord=request.getParameter("passWord"); String sex=request.getParameter("sex"); String question=request.getParameter("question"); String answer=request.getParameter("answer"); String email=request.getParameter("email"); if(conn!=null){ try{ String sql="insert into tb_user(userName,passWord,sex,question,answer,email) values(userName,passWord,sex,question,answer,email)"; PreparedStatement ps=conn.prepareStatement(sql); ps.executeUpdate(); PrintWriter out=response.getWriter(); out.print(userName+"注册成功!"); } catch(Exception e){ e.printStackTrace(); } } else{ response.sendError(500, "数据库连接错误"); } } public void init() throws ServletException { // Put your code here super.init(); try{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/text"; conn=DriverManager.getConnection(url, "root", "knight"); } catch(Exception e){ e.printStackTrace(); } } }