连接数据库与jsp的 servlet
Servlet.java
package ch1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class Servlet extends HttpServlet {
/**
* Constructor of the object.
*/
public Servlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("处理get请求...");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("处理post请求...");
try{
String driverName="com.mysql.jdbc.Driver";
String name="root";
String password="sz11381972hch";
String dbName="hzs";
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+name+"&password="+password;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url);
String Zhanghao=request.getParameter("zhanghao");
String Mima=request.getParameter("mima");
System.out.println(Zhanghao);
System.out.println(Mima);
String sql="select * from haas1 where id=? and password=?";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt.setString(1,Zhanghao);
pstmt.setString(2,Mima);
ResultSet rs=pstmt.executeQuery();
int a=0;
while (rs.next())
{
a++;
RequestDispatcher rd=request.getRequestDispatcher("/body.jsp");
rd.forward(request, response);
}
if(a==0)
{
RequestDispatcher rd=request.getRequestDispatcher("/fail.jsp");
rd.forward(request, response);
}
if(pstmt!=null){pstmt.close();}
if(conn!=null){conn.close();}
}
catch(Exception e){}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
servlet2.java
package ch1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
public class Servlet2 extends HttpServlet {
/**
* Constructor of the object.
*/
public Servlet2() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("处理get请求...");
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
String driverName="com.mysql.jdbc.Driver";
String name="root";
String password="sz11381972hch";
String dbName="hzs";
String url1="jdbc:mysql://localhost:3306/"+dbName;
String url2="?user="+name+"&password="+password;
String url3="&useUnicode=true&characterEncoding=UTF-8";
String url=url1+url2+url3;
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection(url);
String Zhanghao2=request.getParameter("zhanghao2");
String Name2=request.getParameter("name2");
String Mima2=request.getParameter("mima2");//变量信息zhuanhuan
String sql2="select * from haas1 where id=?";
PreparedStatement pstmt2=conn.prepareStatement(sql2);
pstmt2.setString(1,Zhanghao2);
ResultSet n2=pstmt2.executeQuery();
int k=0;
while (n2.next())
{
RequestDispatcher rd=request.getRequestDispatcher("/fail2.jsp");
rd.forward(request, response);
k++;
}
if(k==0)
{
String sql3="insert into haas1(id,name,password) values(?,?,?)";
PreparedStatement pstmt3=conn.prepareStatement(sql3);
pstmt3.setString(1,Zhanghao2);
pstmt3.setString(2,Name2);
pstmt3.setString(3,Mima2);
int n=pstmt3.executeUpdate();
if (n!=0)
{
RequestDispatcher rd=request.getRequestDispatcher("/success.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd=request.getRequestDispatcher("/fail3.jsp");
rd.forward(request, response);
}
}
}
catch(Exception e){}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
浙公网安备 33010602011771号