将前端数据传到数据库

//建立一个servlet

public class Getblog extends HttpServlet {


/处理前端传来的数据
 public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  request.getParameter("blog_title");
  request.getParameter("blog_body");

try {
   getdb(title,body);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 

//连接数据库
 public void getdb(String title,String body)throws Exception{
  Class.forName("com.mysql.jdbc.Driver");
  Connection conn=DriverManager.getConnection(url,user,password);
  PreparedStatement stat=conn.prepareStatement("insert into 用户博客(标题,博客) values(?,?)");
  stat.setString(1,title);
  stat.setString(2, body);
 }
}

你可能感兴趣的:(将前端数据传到数据库)