jsp+servlet+jdbc 存档

68 阅读1分钟

maven

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>p1123</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>p1123 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

pojo

package test.pojo;

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

public class Address {
 public String index;
 public String address;
 public String address2;
 public String district;
 public String cityId;
 public String postalCode;
 public String phone;
 public String lastUpdate;

 Connection connection;

 public static final String URL = "jdbc:mysql://localhost:3306/address";
 public static final String USER = "addressUser";
 public static final String PASSWORD = "20020708zxr";

 public String getIndex() {
  return index;
 }

 public void setIndex(String index) {
  this.index = index;
 }

 public String getAddress() {
  return address;
 }

 public void setAddress(String address) {
  this.address = address;
 }

 public String getAddress2() {
  return address2;
 }

 public void setAddress2(String address2) {
  this.address2 = address2;
 }

 public String getDistrict() {
  return district;
 }

 public void setDistrict(String district) {
  this.district = district;
 }

 public String getCityId() {
  return cityId;
 }

 public void setCityId(String cityId) {
  this.cityId = cityId;
 }

 public String getPostalCode() {
  return postalCode;
 }

 public void setPostalCode(String postalCode) {
  this.postalCode = postalCode;
 }

 public String getPhone() {
  return phone;
 }

 public void setPhone(String phone) {
  this.phone = phone;
 }

 public String getLastUpdate() {
  return lastUpdate;
 }

 public void setLastUpdate(String lastUpdate) {
  this.lastUpdate = lastUpdate;
 }

 public Connection getConnection() {
  return connection;
 }

 public void setConnection(Connection connection) {
  this.connection = connection;
 }

 public Address(String index, String address, String address2, String district,
         String cityId, String postalCode, String phone, String lastUpdate) {
  this.index = index;
  this.address = address;
  this.address2 = address2;
  this.district = district;
  this.cityId = cityId;
  this.postalCode = postalCode;
  this.phone = phone;
  this.lastUpdate = lastUpdate;
 }

 public Address(){
  try {
   Class.forName("com.mysql.cj.jdbc.Driver");
   connection = DriverManager.getConnection(URL,USER,PASSWORD);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

pojoDriver

package test.pojo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AddressDriver {
 public static Address address = new Address();
 public static List<Address> getAddresses () throws Exception{
  Connection connection = address.getConnection();
  Statement statement = connection.createStatement();
  ResultSet resultSet = statement.executeQuery("SELECT * FROM address");
  List<Address> addresses = new ArrayList<>();
  while (resultSet.next()){
   addresses.add(new Address(resultSet.getString("index"),
     resultSet.getString("address"),resultSet.getString("address2"),
     resultSet.getString("district"),resultSet.getString("cityId"),
     resultSet.getString("postalCode"),resultSet.getString("phone"),
     resultSet.getString("lastUpdate")));
  }
  return addresses;
 }
 public static void deleteAddress(String index) throws Exception{
  Connection connection = address.getConnection();
  String sql = "delete from address where `index` = "+Integer.parseInt(index);
  System.out.println(sql);
  Statement statement = connection.createStatement();
  statement.executeUpdate(sql);

//  PreparedStatement preparedStatement = connection.prepareStatement(sql);
//  preparedStatement.setString(1,index);
//  preparedStatement.execute();
  System.out.println(AddressDriver.getAddresses());
 }
}

jsp

<%@ page import="java.util.List" %>
<%@ page import="test.pojo.Address" %>
<%@ page import="test.pojo.AddressDriver" %>
<%--
  Created by IntelliJ IDEA.
  User: 19318
  Date: 2022/11/9
  Time: 23:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div>Address List</div>
<%
    List<Address> addresses = (List<Address>) request.getAttribute("addresses");
%>
<div>
    <form method="post" action="DeleteServlet">
        <table border="1" cellspacing="0" width="800">
            <tr>
                <td>index</td>
                <td>Address</td>
                <td>Address2</td>
                <td>District</td>
                <td>cityId</td>
                <td>PostalCode</td>
                <td>Phone</td>
                <td>LastUpdate</td>
                <td>Delete</td>

            </tr>
            <%
                for (int i = 0; i < addresses.size(); i++) {
            %>
            <tr>
                <td><%=addresses.get(i).getIndex()%>
                </td>
                <td><%=addresses.get(i).getAddress()%>
                </td>
                <td><%=addresses.get(i).getAddress2()%>
                </td>
                <td><%=addresses.get(i).getDistrict()%>
                </td>
                <td><%=addresses.get(i).getCityId()%>
                </td>
                <td><%=addresses.get(i).getPostalCode()%>
                </td>
                <td><%=addresses.get(i).getPhone()%>
                </td>
                <td><%=addresses.get(i).getLastUpdate()%>
                </td>
                <td><a href="DeleteServlet?index=<%=addresses.get(i).getIndex()%>">delete</a></td>
            </tr>
            <%
                }
            %>
        </table>
    </form>
</div>

<script>
    function del(index){
        console.log(index)

    }
</script>
</body>
</html>

servlet

package test.servlet;

import test.pojo.Address;
import test.pojo.AddressDriver;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "DeleteServlet", value = "/DeleteServlet")
public class DeleteServlet extends HttpServlet {
 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

  if (request.getParameter("index")!=null){
   try {
    AddressDriver.deleteAddress(request.getParameter("index"));
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
  request.setCharacterEncoding("utf-8");
  List<Address> addresses;
  try {
   addresses = AddressDriver.getAddresses();
   request.setAttribute("addresses",addresses);
   request.getRequestDispatcher("index.jsp").forward(request,response);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  request.setCharacterEncoding("utf-8");

 }
}

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <servlet>
        <!-- 类名 -->
        <servlet-name>DeleteServlet</servlet-name>
        <!-- 所在的包 -->
        <servlet-class>test.servlet.DeleteServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>DeleteServlet</servlet-name>
        <!-- 访问的网址 -->
        <url-pattern>/DeleteServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>DeleteServlet</welcome-file> <!--直接改为servlet路径-->
    </welcome-file-list>
</web-app>

image.png