如何在Docker中测验Jsp连接数据库mysql的操作(制作成一个镜像)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 如何在Docker中测验Jsp连接数据库mysql的操作(制作成一个镜像) 在docker中运行tomcat和数据库的操作流程 首先导入镜像(tomcatdababase。tar是我们之前保存的镜像) 然后运行容器 之后启动mysql服务 启动tomcat服务 外部检测 mysql启动成功 外部.

如何在Docker中测验Jsp连接数据库mysql的操作(制作成一个镜像)

在docker中运行tomcat和数据库的操作流程

  1. 首先导入镜像(tomcatdababase。tar是我们之前保存的镜像)
  2. 然后运行容器
  3. 之后启动mysql服务
    这里写图片描述
  4. 启动tomcat服务
    这里写图片描述
  5. 外部检测
    这里写图片描述
    mysql启动成功
  6. 外部检测tomcat
    这里写图片描述
  7. docker 环境已经搭好

现在进行显示网页

  1. 注意要将自己的tomcat文件夹的lib文件夹下,放置一个
    mysql的jar包
    这里写图片描述
  2. 首先布置数据库(mydb.person)
-- 删除表
DROP TABLE person ;
-- 建立person表
CREATE TABLE person
(
    -- 生成一个流水号,观察显示的记录数
    id int AUTO_INCREMENT NOT NULL PRIMARY KEY ,
    -- 用户的登陆ID
    uid varchar(32) ,
    -- 用户的真实姓名
    name varchar(32) ,
    -- 用户的登陆密码
    password varchar(20)
) ;
-- 插入测试数据
INSERT INTO person(uid,name,password) VALUES ('Avaya01','Jeremy01','pwd01') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya02','Jeremy02','pwd02') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya03','Jeremy03','pwd03') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya04','Jeremy04','pwd04') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya05','Jeremy05','pwd05') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya06','Jeremy06','pwd06') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya07','Jeremy07','pwd07') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya08','Jeremy08','pwd08') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya09','Jeremy09','pwd09') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya10','Jeremy10','pwd10') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya11','Jeremy11','pwd11') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya12','Jeremy12','pwd12') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya13','Jeremy13','pwd13') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya14','Jeremy14','pwd14') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya15','Jeremy15','pwd15') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya16','Jeremy16','pwd16') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya17','Jeremy17','pwd17') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya18','Jeremy18','pwd18') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya19','Jeremy19','pwd19') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya20','Jeremy20','pwd20') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya21','Jeremy21','pwd21') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya22','Jeremy22','pwd22') ;
INSERT INTO person(uid,name,password) VALUES ('Avaya23','Jeremy23','pwd23') ;
  1. test_db.Jsp 文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<head>
    <title>分页显示</title>
</head>
<body>
<center>
    <h1>人员列表</h1>
    <hr>
    <br>
    <%!
        final String jspUrl = "list_person_false_05.jsp" ;
    %>
    <%
        // 定义如下分页变量
        // 1、定义没页要显示的记录数
        int lineSize = 10 ;
        // 2、定义一个当前是第几页
        int currentPage = 1 ;
        // 计算出总页数
        int pageSize = 0 ;
        // 总记录数 / 每页显示的记录数
        int allRecorders = 30 ;
    %>
    <%
        // 接收传过来的当前页
        try
        {
            currentPage = Integer.parseInt(request.getParameter("cp")) ;
        }
        catch(Exception e)
        {}
    %>
    <%
        final String DBDRIVER = "com.mysql.jdbc.Driver" ;
        final String DBURL = "jdbc:mysql://127.0.0.1:3306/mydb" ;
        final String DBUSER = "root" ;
        final String DBPASSWORD = "123456" ;
        Connection conn = null ;
    %>
    <%
        try
        {
            Class.forName(DBDRIVER) ;
            conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ;
             if(!conn.isClosed()) 
             {System.out.println("Succeeded connecting to the Database!");}
           else{
            System.out.println("not Succeeded connecting to the Database!");
            }

            PreparedStatement pstmt = null ;
            String sql = "SELECT COUNT(id) from person" ;

            pstmt = conn.prepareStatement(sql) ;
            ResultSet rs = pstmt.executeQuery() ;
            if(rs.next())
            {
                allRecorders = rs.getInt(1) ;
            }
            rs.close() ;
            pstmt.close() ;

            // 计算总页数
            pageSize = (allRecorders+lineSize-1)/lineSize ;

            sql = "SELECT id,uid,name,password FROM person" ;
            pstmt = conn.prepareStatement(sql) ;
            rs = pstmt.executeQuery() ;
    %>
    <script language="javaScript">
        function openPage(curpage)
        {
            document.spage.cp.value = curpage ;
            // alert(cupage) ;
            document.spage.submit() ;
        }
        function selOpenPage()
        {
            document.spage.cp.value = document.spage.selpage.value ;
            document.spage.submit() ;
        }
    </script>
    <form name="spage" action="<%=jspUrl%>">
        <input type="button" value="首页" onClick="openPage(1)" <%=currentPage==1?"disabled":""%>>
        <input type="button" value="上一页" onClick="openPage(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>>
        <input type="button" value="下一页" onClick="openPage(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>>
        <input type="button" value="尾页" onClick="openPage(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>>
        <input type="hidden" name="cp" value="">
        <font color="red" size="5"><%=currentPage%></font>
        /
        <font color="red" size="5"><%=pageSize%></font>
        跳转到
            <select name="selpage" onChange="selOpenPage()">
            <%
                for(int x=1;x<=pageSize;x++)
                {
            %>
                <option value="<%=x%>" <%=currentPage==x?"selected":""%>><%=x%></option>
            <%
                }   
            %>
            </select></form>
    <table border="1" width="80%">
    <tr>
        <td>编号</td>
        <td>登陆名称</td>
        <td>姓名</td>
        <td>密码</td>
        <td colspan="2">操作</td>
    </tr>
    <%
            int i = 0 ;
            for(int x=0;x<(currentPage-1)*lineSize;x++)
            {
                rs.next();
            }
            // 对于输出代码之前要求按显示的页数空出
            for(int x=0;x<lineSize;x++)
            {
                if(rs.next())
                {
                    i++ ;
                    int id = rs.getInt(1) ;
                    String userid = rs.getString(2) ;
                    String name = rs.getString(3) ;
                    String password = rs.getString(4) ;
        %>
                <tr>
                    <td><%=id%></td>
                    <td><%=userid%></td>
                    <td><%=name%></td>
                    <td><%=password%></td>
                    <td>更新</td>
                    <td>删除</td>
                </tr>
        <%
                }
            }
            rs.close() ;
            pstmt.close() ;
            if(i==0)
            {
    %>
                <tr>
                    <td colspan="6">没有任何数据!!</td>
                </tr>
    <%
            }
    %>
    </table>
    <%
        }
        catch(Exception e)
        {
    %>
            <h2>系统出错!!!</h2>
    <%
        }
        finally
        {
            conn.close() ;
        }
    %>
</center>
</body>
</html>

将此Jsp文件放置在

/usr/local/apache-tomcat-8.5.30/webapps/my

然后在
这里写图片描述

在可以和host通信的机器上,进行测试

这里写图片描述

原文地址http://www.bieryun.com/3398.html

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
22天前
|
存储 SQL 关系型数据库
【MySQL】4. 表的操作
【MySQL】4. 表的操作
21 0
|
1月前
|
SQL 关系型数据库 MySQL
|
21天前
|
缓存 关系型数据库 MySQL
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
MySQL查询优化:提速查询效率的13大秘籍(合理使用索引合并、优化配置参数、使用分区优化性能、避免不必要的排序和group by操作)(下)
|
3天前
|
运维 前端开发 Devops
云效产品使用报错问题之流水线打包docker镜像时报网络代理有问题如何解决
本合集将整理呈现用户在使用过程中遇到的报错及其对应的解决办法,包括但不限于账户权限设置错误、项目配置不正确、代码提交冲突、构建任务执行失败、测试环境异常、需求流转阻塞等问题。阿里云云效是一站式企业级研发协同和DevOps平台,为企业提供从需求规划、开发、测试、发布到运维、运营的全流程端到端服务和工具支撑,致力于提升企业的研发效能和创新能力。
|
12天前
|
关系型数据库 MySQL 数据库
Docker安装MySQL
Docker安装MySQL
27 1
|
15天前
|
应用服务中间件 Docker 容器
docker 镜像常用命令
docker 镜像常用命令
35 0
|
15天前
|
关系型数据库 MySQL 数据库
docker自定义安装mysql 5.7
docker自定义安装mysql 5.7
23 0
|
15天前
|
Linux Shell 虚拟化
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
linux 部署docker容器虚拟化平台(二)--------docker 镜像制作方法
26 0
|
22天前
|
存储 Kubernetes API
Docker拉取镜像或者kubectl出现的这个解决方案x509: certificate signed by unknown authority
Docker拉取镜像或者kubectl出现的这个解决方案x509: certificate signed by unknown authority
55 2
|
23天前
|
Linux Docker 容器
Linux彻底卸载Docker包括运行拉取的镜像
Linux彻底卸载Docker包括运行拉取的镜像
24 1