程序员社区

【Servlet-1】Servlet中编写JDBC连接数据库

index.html文件:

<html>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a href="/23_Servlet_laodu/system/list">显示员工信息</a>
</body>
</html>

web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
<!--除非你用注释的方式,否则这个每次都需要你手动填写-->
<!--
servlet-name:可以随便起个名字
servlet-class:类的相对路径
url-pattern:相对路径
-->
    <servlet>
        <servlet-name>listEmp</servlet-name>
        <servlet-class>Servlet.ListEmpServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>listEmp</servlet-name>
        <url-pattern>/system/list</url-pattern>
    </servlet-mapping>
</web-app>

ListEmpServlet文件:

package Servlet;

import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class ListEmpServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {

    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest request, ServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.print(" <html>");
        out.print(" <head>");
        out.print("     <meta charset='UTF-8'>");
        out.print("    <title>Title</title>");
        out.print("     </head>");
        out.print("     <body>");
        out.print("     <h3 align='center'>员工列表</h3>");
        out.print("     <hr width='60%'>");
        out.print("     <table border='1'align='center'width='50%'>");
        out.print("         <tr align='center'>");
        out.print("        <th>序号</th>");
        out.print("        <th>员工编号</th>");
        out.print("        <th>员工姓名</th>");
        out.print("        <th>员工薪水</th>");
        out.print("      </tr>");

        //JDBC
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3366/bjpowernode","root","root");
            String sql = "select empno,ename,sal from emp";
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            int i=0;
            while(rs.next()){
                String empno = rs.getString("empno");
                String ename = rs.getString("ename");
                String sal = rs.getString("sal");
                out.print("      <tr align='center'>");
                out.print("        <td>"+(++i)+"</td>");
                out.print("        <td>"+empno+"</td>");
                out.print("        <td>"+ename+"</td>");
                out.print("        <td>"+sal+"</td>");
                out.print("      </tr>");
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(rs!=null){
                try {
                    rs.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
            if(ps!=null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        out.print(" </table>");
        out.print(" </body>");
        out.print(" </html>");
    }

    @Override
    public String getServletInfo() {
        return null;
    }
    @Override
    public void destroy() {
    }
}

在这里插入图片描述

赞(0) 打赏
未经允许不得转载:IDEA激活码 » 【Servlet-1】Servlet中编写JDBC连接数据库

相关推荐

  • 暂无文章

一个分享Java & Python知识的社区