package servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Insert extends HttpServlet {
/**
* Constructor of the object.
*/
public Insert() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* 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 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("
A Servlet");
out.println(" ");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" ");
out.println("");
out.flush();
out.close();
}
/**
* The doPost method of the servlet.
*
* 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 {
boolean flag;
try {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("驱动包加载失败。");
System.exit(0);
}
// 建立与数据库的连接
Connection con=DriverManager.getConnection("jdbc:mysql:/localhost/org.lyy"
,"root","123456");
// 发送语句
Statement stmt = con.createStatement();
request.setCharacterEncoding("UTF-8");
String SID = request.getParameter("SID");
String SName = request.getParameter("SName");
String SSex = request.getParameter("SSex");
String SNum = request.getParameter("SNum");
String SPhone = request.getParameter("SPhone");
String SSubject = request.getParameter("SSubject");
String SCollege = request.getParameter("SCollege");
String PlaceOfBirth = request.getParameter("PlaceOfBirth");
String SPhoto = request.getParameter("SPhoto");
String sql = "select * from Student where SID='" + SID + "'";
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
flag = false;
} else {
sql = "INSERT INTO Student(SID,SName,SSex,SNum ,SPhone,SSubject,SCollege,PlaceOfBirth,SPhoto) "
+ "VALUES ('"
+ SID
+ "','"
+ SName
+ "','"
+ SSex
+ "','"
+ SNum
+ "','"
+ SPhone
+ "','"
+ SSubject
+ "','"
+ SCollege
+ "','"
+ PlaceOfBirth
+ "','"
+ SPhoto + "')";
// 更新数据库,每一次对数据库进行操作后,都记得更新数据库;
stmt.executeUpdate(sql);
flag = true;
}
response.setContentType("text/html;charset=UTF-8"); // 定义页面显示类型与编码方式
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("
添加成功");
out.println("");
out.println(" ");
out.println("
");
if (flag == true) {
out.println("添加成功,可以继续添加... ...");
String trans = "";
out.println(trans);
} else {
out.println("添加的用户已经存在,请重新添加... ...");
String trans = "";
out.println(trans);
}
out.println("
");
out.println(" ");
out.println("");
out.flush();
out.close();
// 记住要close掉创建的对象和顺序
stmt.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Initialization of the servlet.
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
网上貌似没有正确的