数据库连接:学生信息管理系统

风流意气都作罢 提交于 2019-12-01 20:37:21
 
 
package Student;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBUtil {
 public static String db_url = "jdbc:mysql://localhost:3306/student?characterEncoding=utf8";
 public static String db_user = "root";
 public static String db_pass = "xjmwan1314";
 public static Connection getConn () {
  Connection conn = null;
  try {
   Class.forName("com.mysql.jdbc.Driver");
   conn = DriverManager.getConnection(db_url, db_user, db_pass);
  } catch (Exception e) {
   e.printStackTrace();
  }
  return conn;
 }
}
 

 

 

 

StudentOperate.java

package Student;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import Student.DBUtil;
/**
 * Servlet implementation class StudentOperate
 */
@WebServlet("/operate")
public class StudentOperate extends HttpServlet {
 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  super.service(request, response);
  String method=request.getParameter("method");
  if(method.equals("register"))
  {
   register(request,response);
   //   response.getWriter().append("okkk").append(request.getContextPath());
  }
  if(method.equals("add"))
  {
   add(request,response);
   //   response.getWriter().append("okkk").append(request.getContextPath());
  }
 }
 
 private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  String name=request.getParameter("name");
//  String num=request.getParameter("num");
  name=new String(name.getBytes("ISO-8859-1"),"UTF-8");//服务器默认使用 ISO-8859-1 编码接受参数,手动转为UTF-8
  String english=request.getParameter("english");
  String math=request.getParameter("math");
  String computer=request.getParameter("computer");
  Connection con = DBUtil.getConn();
  PreparedStatement stmt=null;
  String sql = "insert into student(name,english,math,computer)values(?,?,?,?)";
  try {
   stmt = con.prepareStatement(sql);
   stmt.setString(1,name);
   stmt.setString(2,english);
   stmt.setString(3,math);
   stmt.setString(4,computer);
   //stmt.execute();
   int i=stmt.executeUpdate();
   if(i==1) {
    request.setAttribute("message", "添加成功!");
    request.getRequestDispatcher("add.jsp").forward(request,response);
   }
   else
   {
    request.setAttribute("message", "添加失败!");
    request.getRequestDispatcher("add.jsp").forward(request,response);
   }
   stmt.close();
   con.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");
  String account=request.getParameter("account");
  account=new String(account.getBytes("ISO-8859-1"),"UTF-8");
  
  String code=request.getParameter("code");
  
  String sex=request.getParameter("sex");
  sex=new String(sex.getBytes("ISO-8859-1"),"UTF-8");
  
  String name=request.getParameter("name");
//  String num=request.getParameter("num");
  name=new String(name.getBytes("ISO-8859-1"),"UTF-8");//服务器默认使用 ISO-8859-1 编码接受参数,手动转为UTF-8
  
  String num=request.getParameter("num");
  
  String email=request.getParameter("email");
  
  String college=request.getParameter("college");
  college=new String(college.getBytes("ISO-8859-1"),"UTF-8");
  
  String major=request.getParameter("major");
  major=new String(major.getBytes("ISO-8859-1"),"UTF-8");
  
  String cclass=request.getParameter("class");
  cclass=new String(cclass.getBytes("ISO-8859-1"),"UTF-8");
  
  String year=request.getParameter("year");
  
  String home=request.getParameter("home");
  home=new String(home.getBytes("ISO-8859-1"),"UTF-8");
  
  String other=request.getParameter("other");
  other=new String(other.getBytes("ISO-8859-1"),"UTF-8");
 
  Connection con = DBUtil.getConn();
  PreparedStatement stmt=null;
  String sql = "insert into register(account,code,sex,name,num,email,college,major,class,year,home,other)values(?,?,?,?,?,?,?,?,?,?,?,?)";
  try {
   stmt = con.prepareStatement(sql);
   stmt.setString(1,account);
   stmt.setString(2,code);
   stmt.setString(3,sex);
   stmt.setString(4,name);
   stmt.setString(5,num);
   stmt.setString(6,email);
   stmt.setString(7,college);
   stmt.setString(8,major);
   stmt.setString(9,cclass);
   stmt.setString(10,year);
   stmt.setString(11,home);
   stmt.setString(12,other);
   //stmt.execute();
   int i=stmt.executeUpdate();
   if(i==1) {
    request.setAttribute("message", "添加成功!");
    request.getRequestDispatcher("regist.jsp").forward(request,response);
   }
   else
   {
    request.setAttribute("message", "添加失败!");
    request.getRequestDispatcher("regist.jsp").forward(request,response);
   }
   stmt.close();
   con.close();
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 
 
 
 
 
 
 
 
 
 private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentOperate() {
        super();
        // TODO Auto-generated constructor stub
    }
 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  
 }
 /**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  */
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  doGet(request, response);
 }
}

 

 

 

 

 

 

 

 rejist.java

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

结果截图

 

 

 

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!