HttpServlet练习查

HttpServlet练习查代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.xiaoguan.oa.index;

import com.xiaoguan.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DeptListServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String contextPath=request.getContextPath();
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.print("<!DOCTYPE html>");
out.print("<html lang='ch'>");
out.print(" <head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门列表</title>");
out.print(" </head>");
out.print(" <body>");
out.print(" <h1 align='center'>部门列表</h1>");
out.print(" <hr>");
out.print(" <table border='1px' align='center' width='50%'>");
out.print(" <tr>");
out.print(" <th>序号</th>");
out.print(" <th>部门编号</th>");
out.print(" <th>部门名称</th>");
out.print(" <th>操作</th>");
out.print(" </tr>");
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
conn=DBUtil.getConnection();
String sql="select deptno,dname,loc from dept ";
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
int i=0;
while (rs.next()){
int deptno=rs.getInt("deptno");
String dname=rs.getString("dname");
String loc=rs.getString("loc");
out.print(" <tr>");
out.print(" <td>"+(++i)+"</td>");
out.print(" <td>"+deptno+"</td>");
out.print(" <td>"+dname+"</td>");
out.print(" <td>");
out.print(" <a href=''>删除</a>");
out.print(" <a href='edit.html'>修改</a>");
out.print(" <a href='"+contextPath+"/detail?deptno="+deptno+"'>详情</a>");
out.print(" </td>");
out.print(" </tr>");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
out.print(" </table>");
out.print(" <hr>");
out.print(" <a href='add.html'>新增部门</a><br>");
out.print(" </body>");
out.print("</html>");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.xiaoguan.oa.index;

import com.xiaoguan.oa.utils.DBUtil;
import jakarta.servlet.ServletException;
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.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DetailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out=resp.getWriter();
String deptno = req.getParameter("deptno");
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
out.print("<!DOCTYPE html>");
out.print("<html lang='ch'>");
out.print(" <head>");
out.print(" <meta charset='UTF-8'>");
out.print(" <title>部门详情</title>");
out.print(" </head>");
out.print(" <body>");
out.print(" <h1>部门详情</h1>");
out.print(" <hr>");


try {
conn= DBUtil.getConnection();
String sql="select deptno,dname,loc from dept where deptno=?";
ps=conn.prepareStatement(sql);
ps.setString(1,deptno);
rs=ps.executeQuery();
if(rs.next()){
String dname = rs.getString("dname");
String loc = rs.getString("loc");
out.print(" 部门编号<input type='text' value='"+deptno+"' readonly/><br>");
out.print(" 部门名称<input type='text' value='"+dname+"' readonly/><br>");
out.print(" 部门位置<input type='text' value='"+loc+"' readonly/><br>");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtil.close(conn,ps,rs);
}
out.print(" <input type='button' value='后退' onclick='window.history.back()'>");
out.print(" </body>");
out.print("</html>");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.xiaoguan.oa.utils;


import java.sql.*;
import java.util.ResourceBundle;

public class DBUtil {
private static ResourceBundle bundle=ResourceBundle.getBundle("resources.jdbc");
private static String driver=bundle.getString("driver");
private static String url=bundle.getString("url");
private static String username=bundle.getString("username");
private static String password=bundle.getString("password");
static {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,username,password);
}
public static void close(Connection conn, Statement ps,ResultSet rs){
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}