如何在Docker中测验Jsp连接数据库mysql的操作(制作成一个镜像)http://www.bieryun.com/3398.html
在docker中运行tomcat和数据库的操作流程
- 首先导入镜像(tomcatdababase。tar是我们之前保存的镜像)
- 然后运行容器
- 之后启动mysql服务
- 启动tomcat服务
- 外部检测
mysql启动成功 - 外部检测tomcat
- docker 环境已经搭好
现在进行显示网页
- 注意要将自己的tomcat文件夹的lib文件夹下,放置一个
mysql的jar包
- 首先布置数据库(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') ;
- 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通信的机器上,进行测试