需求:

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

实现效果:

开发环境:

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. MS Build参考

    以下链接很详细的讲述了Build方面相关的知识,顺带连Link的参数都解释了,以后不知道就来这里查一查了. http://msdn.microsoft.com/zh-CN/library/ee8624 ...

  2. geoip 添加一列,add_field =>["[geoip][request_time]","%{request_time}"]

    "message" => " 10.171.246.184 [11/Sep/2016:14:42:53 +0800] \"GET /wechat/home ...

  3. c++ 03

    一.面向对象编程 1.什么是对象?什么是对象编程? 1)万物皆对象 2)世界是由一组相互之间紧密联系的对象组成的. 3)通过将对象按照属性和行为共性进行分类,达到将具体事物进行抽象的效果. 4)通过程 ...

  4. 链表的基本操作(Basic Operations on a Linked List)

    链表可以进行如下操作: 创建新链表 增加新元素 遍历链表 打印链表 下面定义了对应以上操作的基本函数. 创建新链表 新链表创建之后里面并没有任何元素,我们要为数据在内存中分配节点,再将节点插入链表.由 ...

  5. python海明距离 - 5IVI4I_I_60Y的日志 - 网易博客

    python海明距离 - 5IVI4I_I_60Y的日志 - 网易博客 python海明距离   2009-10-01 09:50:41|  分类: Python |  标签: |举报 |字号大中小  ...

  6. [转]Google2012.9.24校园招聘会笔试题

    代码: [cpp] view plaincopy //转载请标明出处,原文地址:http://blog.csdn.net/hackbuteer1/article/details/8017703 boo ...

  7. 常用的Eclipse快捷键

    alt+shift+r 修改名字 ctrl+shift+r 查找源类 Eclipse快捷键功能1. [ALT+/]   --->提示此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不 ...

  8. Linux下用命令格式化U盘

    1.找到U盘位置(已挂载) sudo fdisk -l 如图,我的在/dev/sdc4 2.格式化U盘 sudo mkfs -t vfat /dev/sdc4 -t 后面是格式化为哪种文件系统格式,v ...

  9. Myeclipse安装破解

  10. IoC容器Autofac正篇之依赖注入(六)

    依赖注入,这个专业词我们可以分为两个部分来理解: 依赖,也就是UML中描述事物之间关系的依赖关系,依赖关系描述了事物A在某些情况下会使用到事物B,事物B的变化会影响到事物A: 注入,医生通过针头将药物 ...