Servlet jdbc 改、删

image.png

DAO

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Dao {
    public Connection getConnection() {
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
                        //?useUnicode=true&characterEncoding=utf-8&useSSL=false为了解决报错Establishing
                        // SSL connection without server's identity verification is not recommended
            String url="jdbc:mysql://localhost:3306/javaweb?useUnicode=true&characterEncoding=utf-8&useSSL=false";
            String username="root";
            String password="root";
            conn=DriverManager.getConnection(url,username,password);
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
}

update

package com.baozi;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="UpdateServlet",urlPatterns="/UpdateServlet") 
public class UpdateServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.valueOf(request.getParameter("id"));
        int bookCount=Integer.valueOf(request.getParameter("bookCount"));
        Dao dao=new Dao();
        Connection conn=dao.getConnection();
        try {
            response.setContentType("text/html");
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            String sql="update tb_books set bookCount=? where id=?;";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1, bookCount);
            ps.setInt(2, id);
            ps.executeUpdate();
            ps.close();
            conn.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("QueryAll");
        
    }

}

delete

package com.baozi;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name="DeleteServlet",urlPatterns="/DeleteServlet")
public class DeleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        int id=Integer.valueOf(request.getParameter("id"));
        Dao dao=new Dao();
        Connection conn=dao.getConnection();
        try {
            response.setContentType("text/html");
            request.setCharacterEncoding("UTF-8");
            response.setCharacterEncoding("UTF-8");
            String sql="delete from tb_books where id=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
            ps.close();
            conn.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        response.sendRedirect("QueryAll");
        
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
}

你可能感兴趣的:(Servlet jdbc 改、删)