|
楼主 |
发表于 2023-4-24 19:08:21
|
显示全部楼层
package coco;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
public class DeptListServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<!DOCTYPE html>");
writer.println("<html lang=\"en\">");
writer.println("<head>");
writer.println("<meta charset=\"UTF-8\">");
writer.println(" <title>Title</title>");
writer.println(" <script>");
writer.println(" function update(city1){");
writer.println(" var city2 = prompt('请输入修改后的城市名:');");
writer.println(" if(window.confirm('你确定要将' + city1 + '修改为' + city2 + '吗?')){");
writer.println(" alert('ok,正在执行操作');");
writer.println(" document.location.href='/Servlet4/dept/update?city1=' + city1 + 'city2=' + city2;");
writer.println(" }else {");
writer.println(" alert('取消成功');");
writer.println(" }");
writer.println(" }");
writer.println(" function Delete(department_id){");
writer.println(" if(window.confirm('确定要执行删除操作吗?')){");
writer.println(" alert('Ok,正在执行删除操作');");
writer.println(" document.location.href='/Servlet4/dept/delete?department_id='+'department_id'");
writer.println(" }else {");
writer.println(" alert('已确认取消');");
writer.println(" }");
writer.println(" }");
writer.println(" </script>");
writer.println("</head>");
writer.println("<body>");
writer.println(" <table border='1px' cellspacing='0' cellpadding='30px'>");
writer.println(" <tr>");
writer.println(" <th>department_id</th>");
writer.println(" <th>department_name</th>");
writer.println(" <th>manager_id</th>");
writer.println(" <th>city</th>");
writer.println(" <th colspan='3'>Function</th>");
writer.println(" </tr>");
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myemployees","root","123456");
preparedStatement = connection.prepareStatement("select department_id,department_name,manager_id,city from departments a left join locations b on a.location_id=b.location_id");
resultSet = preparedStatement.executeQuery();
while (resultSet.next()){
String department_id = resultSet.getString("department_id");
String department_name = resultSet.getString("department_name");
String manager_id = resultSet.getString("manager_id");
String City = resultSet.getString("city");
writer.println(" <tr>");
writer.println(" <td>"+ department_id +"</td>");
writer.println(" <td>"+ department_name +"</td>");
writer.println(" <td>"+ manager_id +"</td>");
writer.println(" <td>"+ City +"</td>");
writer.println(" <td><a href='javascript:void (0)' onclick= 'Delete(" +department_id+")' target='_black'>update</a></td>>");
writer.println(" <td><a href='javascript:void (0)' onclick= 'update("+City+")' target='_black'>delete</a></td>");
writer.println(" <td><a href='/Servlet4/dept/detail?department_id=' + " + department_id +" target='_black'>详情</a></td>");
writer.println(" </tr>");
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
writer.println(" </table>");
writer.println("</body>");
writer.println("</html>");
}
} |
|