需求:

有一批企业的基本信息需要展示出来,要求一级页以列表形式展示企业清单,点击公司名称后进入二级页面,二级页面展示企业简介和几张图片。

实现效果:

开发环境:

Win7,Eclipse,Mysql

数据库表设计:

表字段说明
cpid 主键
cpname 公司名称
cpbody 公司简介
cpimg1 图片1路径
cpimg2 图片2路径
cpimg3 图片3路径
cpimg4 图片4路径

展示公司列表代码company.jsp

 <%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8" info="this is the company platform index page"%> <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>company</title> <style>
body {
margin: 0 auto;
} h1 {
margin: 0 auto;
line-height: 100px;
text-align: center;
color: #FFFFFF;
font-family: 微软雅黑;
} .container {
width: 1000px;
margin: 0 auto;
background: none;
height: 1000px;
} .cphead {
width: 1000px;
height: 100px;
background-color: #B40F0B;
margin: 0 auto;
} .cpintro {
width: 1000px;
height: 15px;
margin: 0 auto;
line-height: 15px;
color: #B4120F;
} .cplist {
width: 1000px;
margin: 0 auto;
height: 600px;
} .cplist table {
width: 100%;
} .cplist td {
height: 45px;
font-family: 微软雅黑;
font-size: 18px;
line-height: 40px;
} .cptdleft {
width: 3%;
} .cptdmiddle {
width: 85%;
} .cptdright {
text-align: center;
} .cplist a {
text-decoration: none;
color: #000000;
} .cplist a:hover {
text-decoration: underline;
color: #F10A0E;
} .bluefont {
color: blue;
font-style: bold;
} .bluefont a {
text-decoration: underline;
color: blue;
}
</style> </head> <body>
<jsp:include page="cphead.jsp"></jsp:include>
<div class="container">
<div class="cplist" id="cplistheight">
<table>
<%
try {
//注册数据驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_gongxiang", "root",
"123456");
//创建statement
Statement stmt = conn.createStatement();
//执行查询 ResultSet rs = stmt.executeQuery("select * from tb_company"); int intPageSize; //一页显示的记录数
int intRowCount; //记录的总数
int intPageCount; //总页数
int intPage; //待显示的页码
String strPage;
int i;
//设置一页显示的记录数
intPageSize = 10;
//取得待显示的页码
strPage = request.getParameter("page");
//判断strPage是否等于null,如果是,显示第一页数据
if (strPage == null) {
intPage = 1;
} else {
//将字符串转换为整型
intPage = java.lang.Integer.parseInt(strPage);
}
if (intPage < 1) {
intPage = 1;
}
//获取记录总数
rs.last();
intRowCount = rs.getRow();
//计算机总页数
intPageCount = (intRowCount + intPageSize - 1) / intPageSize;
//调整待显示的页码
if (intPage > intPageCount)
intPage = intPageCount;
if (intPageCount > 0) {
//将记录指针定位到待显示页的第一条记录上
rs.absolute((intPage - 1) * intPageSize + 1);
}
//下面用于显示数据
i = 0;
while (i < intPageSize && !rs.isAfterLast()) {
%> <tr>
<td class="cptdleft"><img src="data:images/14.jpg"></td>
<td class="cptdmiddle"><a
href="cp1.jsp?cpid=<%=rs.getString(1)%>" target="_blank"><%=rs.getString(2)%></a></td>
<td class="cptdright">2015-06-20</td> </tr>
<%
rs.next();
i++;
}
//关闭连接、释放资源
rs.close();
stmt.close();
conn.close();
%>
<tr>
<td colspan="2" align="center">共<span class="bluefont"><%=intRowCount%></span>个记录,分<span
class="bluefont"><%=intPageCount%></span>页显示,当前页是:第<span
class="bluefont"><%=intPage%></span>页 <span class="bluefont">
<%
for (int j = 1; j <= intPageCount; j++) {
out.print("&nbsp;&nbsp;<a href='company.jsp?page=" + j
+ "'>" + j + "</a>");
}
%>
</span> <%
} catch (Exception e) {
e.printStackTrace();
}
%>
</td>
</tr> </table> </div> <jsp:include page="footer.jsp"></jsp:include>
</div>
</body>
</html>

公司详情页代码cp1.jsp

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%> <%
// out.println( request.getParameter("cpid"));
String aa= request.getParameter("cpid");
//注册数据驱动
Class.forName("com.mysql.jdbc.Driver");
//获取数据库连接
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/db_gongxiang", "root",
"123456");
//创建statement
Statement stmt = conn.createStatement();
//执行查询 ResultSet rs = stmt
.executeQuery("select * from tb_company where cpid="+aa); %> <%
while (rs.next()) {
%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=rs.getString(2)%></title>
<!--
<script language="Javascript">
document.oncontextmenu=new Function("event.returnValue=false"); //禁止右键
document.onselectstart=new Function("event.returnValue=false"); //禁止复制文字
</script>
-->
<style>
body {
margin: 0 auto;
} h1 {
margin: 0 auto;
line-height: 100px;
text-align: center;
color: #FFFFFF;
font-family: 微软雅黑;
} .cpname {
margin: 0 auto;
text-align: center;
color: #B4120F;
} .container {
width: 980px;
margin: 0 auto;
background: none; } .cphead {
width: 980px;
height: 100px;
background-color: #B40F0B;
margin: 0 auto;
} .cpintro {
width: 980px;
height: 15px;
margin: 0 auto;
line-height: 15px;
color: #B4120F;
} .cpdetail {
width: 980px;
margin: 0 auto;
height: auto;
} .cpdetailtop {
width: 90%;
font-size:18px;
margin: 0 auto;
font-size: 18px;
line-height: 32px;
font-family: 微软雅黑;
} .cpimg {
height:auto;
} </style>
</head>
<body>
<div class="container">
<div class="cphead">
<h1>全国居民主食加工企业展示平台</h1>
</div>
<div class="cpintro">
<h3>
<img src="data:images/16.jpg">&nbsp;全国居民主食加工企业
</h3>
</div>
<hr width="980px" color="#B40F0B">
<div class="cpdetail">
<!--公司详情开始-->
<h3>公司简介</h3>
<div class="cpdetailtop">
<!-- 上部div文字介绍--> <h3 class="cpname"><%=rs.getString(2)%></h3>
<p>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<%=rs.getString(3)%></p> </div>
<div class="cpimg">
<!--下部div图片滚动效果-->
<h3>公司形象</h3>
<table align="center">
<tr>
<td><img src="<%=rs.getString(4)%>" width="220px"
height="150px" alt="图像加载失败"></td>
<td><img src="<%=rs.getString(5)%>" width="220px"
height="150px" alt="图像加载失败"></td>
<td><img src="<%=rs.getString(6)%>" width="220px"
height="150px" alt="图像加载失败"></td>
<td><img src="<%=rs.getString(7)%>" width="220px"
height="150px" alt="图像加载失败"></td>
</tr>
</table>
</div> </div>
<!--公司详情结束--> <jsp:include page="footer.jsp"></jsp:include>
</div> <%
}
%>
</body>
</html>

现在处于jsp学习比较初级的阶段,代码中肯定有需要改进的地方,希望博客园的园友们不吝赐教.

jsp执行数据库查询并分页的更多相关文章

  1. c# 数据库编程(通过SqlCommand 执行数据库查询)

    前面一篇文章,我们介绍了如何在c#中对数据库进行更新操作.主要是利用SqlCommand 对象的ExecuteNonQuery方法. 这篇文章介绍,如何进行查询操作.本文给出的例子仍然是针对sql s ...

  2. 自定义mysql类用于快速执行数据库查询以及将查询结果转为json文件

    由于每次连接数据库进行查询比较麻烦,偶尔还需要将查询结果转为json格式的文件, 因此暂时定义一个mysql的类,将这些常用的方法进行封装,便于直接调用(代码如下,个人用,没写什么注释). 注:导入了 ...

  3. osql执行数据库查询命令并保存到txt文件

    osql -Usa -P123 -d AppBox -Q "select * from Menus where sortindex > 1000" -o e:\xxx.txt ...

  4. 基于Mysql数据库的SSM分页查询

    前言: Hello,本Y又来了,"分页"在我们使用软件的过程中是一个很常见的场景,比如博客园对于每个博主的博客都进行了分页展示.可以简单清晰的展示数据,防止一下子将过多的数据展现给 ...

  5. .NET平台开源项目速览(7)关于NoSQL数据库LiteDB的分页查询解决过程

    在文章:这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑) 与 .NET平台开源项目速览(3)小巧轻量级NoSQL文件数据库LiteDB中,介绍了LiteDB的基本使用情况以及部 ...

  6. mongodb基础系列——数据库查询数据返回前台JSP(二)

    上篇博客论述了,数据库查询数据返回前台JSP.博客中主要使用Ajax调用来显示JSON串,来获取其中某一个字段,赋给界面中的某一个控件. 那这篇博客中,我们讲解,把后台List传递JSP展示. Lis ...

  7. mongodb基础系列——数据库查询数据返回前台JSP(一)

    经过一段时间停顿,终于提笔来重新整理mongodb基础系列博客了. 同时也很抱歉,由于各种原因,没有及时整理出,今天做了一个demo,来演示,mongodb数据库查询的数据在JSP显示问题. 做了一个 ...

  8. 【SSH网上商城项目实战05】完成数据库的级联查询和分页

    转自:https://blog.csdn.net/eson_15/article/details/51320212 上一节我们完成了EasyUI菜单的实现.这一节我们主要来写一下CategorySer ...

  9. 数据库中表的复杂查询&amp;分页

    一.数据库中表的复杂查询 1)连接查询 1.0连接的基本的语法格式: from TABLE1 join_type TABLE2 [on (join_condition)][where (query_c ...

随机推荐

  1. spserver 开源服务器框架研究与分析

    网上开源的C/C++服务器框架 还是比较少的. 最近研究了 spserver , 里面用了较多的设计模式,使用设计模式的目的是把不变的东西和可变的东西分离并且封装起来,避免以后修改代码, 应用设计模式 ...

  2. Red Hat Enterprise Linux 7的新功能

     简介红帽最新版本的旗舰平台交付显著增强的可用性. 性能和可靠性. 丰富的新功能为架构. 系统管理员和开发人员提供所需的资源以更高效地进行创新和管理.架构师: 红帽® 企业 Linux® 7 适合 ...

  3. EBS查询用户客户化的文件配置

    select pro.profile_option_name, pro.user_profile_option_name, lev.level_type type, --lev.level_code, ...

  4. Inorder Successor in BST 解答

    Question Given a binary search tree and a node in it, find the in-order successor of that node in th ...

  5. hdu 5288 OO’s Sequence(计数)

    Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...

  6. Unity SendMessage方法

    我们今天研究下SendMessage方法, 如果我们需要执行某一个组件的方法时候可以使用SendMessage gameObject.SendMessage("A"); 即可通知当 ...

  7. Rainmeter 雨滴桌面 主题分享

    说明 先安装主程序 Rainmeter-3.1.exe,然后安装 Techzero_1.0.rmskin,打开主题管理应用主题就可以. 下载 http://pan.baidu.com/s/1i3zI3 ...

  8. Atitit. 最佳实践 QA----减少cpu占有率--cpu占用太高怎么办

    Atitit. 最佳实践 QA----减少cpu占有率--cpu占用太高怎么办 跟个磁盘队列长度雅十,一到李80%走不行兰.... 1. 寻找线程too 多的.关闭... Taskman>> ...

  9. 在Unity3d编辑器中加入菜单以及菜单项

    在引用UZGUI插件时,u3d编辑器的菜单条发生了变化,新增了菜单和菜单项,于是乎自己也像尝试一下,看了EZGUI的About_EZ_GUI脚本文件后,结果大出我所料,原来SO EASY! using ...

  10. 《JavaScript 闯关记》之事件

    JavaScript 程序采用了异步事件驱动编程模型.在这种程序设计风格下,当文档.浏览器.元素或与之相关的对象发生某些有趣的事情时,Web 浏览器就会产生事件(event).例如,当 Web 浏览器 ...