学习日记:servlet通过JavaBean传值给jsp,连接数据库实现增加功能

servlet通过JavaBean传值给jsp,连接数据库实现增加功能

数据库建表
在这里插入图片描述
通过jsp页面给toob表添加一条记录

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cn;

/**
 *
 * @author 86187
 */
public class Student {
    private int id;
    private String stuno;
    private String className;

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the stuno
     */
    public String getStuno() {
        return stuno;
    }

    /**
     * @param stuno the stuno to set
     */
    public void setStuno(String stuno) {
        this.stuno = stuno;
    }

    /**
     * @return the className
     */
    public String getClassName() {
        return className;
    }

    /**
     * @param className the className to set
     */
    public void setClassName(String className) {
        this.className = className;
    }
}

<%-- 
    Document   : insetjsp
    Created on : 2021-6-21, 15:21:09
    Author     : 86187  ${messsage}
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="./insertServlet" method="post">
            <input type="text" name="id" value="<%=request.getAttribute("message")%>"/>
            <input type="text" name="stuno" value="<%=request.getAttribute("message")%>"/>
            <input type="text" name="className" value="<%=request.getAttribute("message")%>"/>
            <input type="submit" value="添加"/>
        </form>
          
           
    </body>
</html>

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import com.cn.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
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 javax.servlet.http.HttpSession;

/**
 *
 * @author 86187
 */
@WebServlet(name = "insertServlet", urlPatterns = {"/insertServlet"})
public class insertServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id=request.getParameter("id");
        String stuno=request.getParameter("stuno");
        String className=request.getParameter("className");
        Connection conn=null;
        PreparedStatement pstmt=null;
        int num=0;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3396/01db", "root", "123456");
            String sqlstr="insert into Toob values(?,?,?)";
            pstmt=conn.prepareStatement(sqlstr);
            pstmt.setInt(1,Integer.parseInt(id));                  
            pstmt.setString(2,stuno);       
            pstmt.setString(3,className);
            num=pstmt.executeUpdate();
        
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            if (pstmt!=null){
                try{pstmt.close();
                } catch (SQLException ex) {
                    Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            if (conn!=null){
                try{conn.close();
                }catch (SQLException ex) {
                    Logger.getLogger(insertServlet.class.getName()).log(Level.SEVERE, null, ex);
                }
               
                
            }
        }
            if(num>0){
            request.setAttribute("message","插入成功");
            }else
            {
            HttpSession session=request.getSession();
            session.setAttribute("message","插入不成功");
            response.sendRedirect("./insertjsp.jsp");
            }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值