需求:

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

实现效果:

开发环境:

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. 《Programming WPF》翻译 第6章 4.应用程序全球化

    原文:<Programming WPF>翻译 第6章 4.应用程序全球化 如果你打算发布你的应用程序到全球各地,你可能需要为不同地区的用户界面准备不同的版本.至少,这需要解决将文本翻译成适 ...

  2. esxi5.5 安装,虚拟机复制

    尝试在vmware workstation上安装hadoop,感觉太慢了. 好在家里的台式机配置还可以,所以就想在它上面虚拟出几台服务器出来. 台式机配置如下: 虚拟出来三个应该没问题了吧. 第一步, ...

  3. CDT+Eclipse代码自动提示

    1.查看GCC的版本:$gcc -v————————————————————————gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC)———————— ...

  4. SNMP配置和常用命令OID(转)

    博文转至:http://blog.csdn.net/susu09new/article/details/12835191 OID值表示的意义(中文) .1.3.6.1.2.1.1操作系统相关的信息,其 ...

  5. jquery IE6 select.val() bug报错解决办法

    原文地址:http://hi.baidu.com/kinghmx/item/395dbac3261292dcef183b52 最近在写一个页面,在出了ie6外的所有浏览器中都正常(ie7,8,9,  ...

  6. UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>

    L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  7. Hdu5785-Interesting(回文串处理)

    Problem Description Alice get a string S. She thinks palindrome string is interesting. Now she wanna ...

  8. Mysql数据库乱码与编码问题筛查

    最近接连遇到数据库编码问题,让你的系统本来像个美丽的姑娘却忽然发现她不识字一样难受,其实很多时候是编码的问题,而mysql(特别地)设计编码的地方很多,在这里做一个筛查: 1 mysql编码 用下面的 ...

  9. jdbc资料收集

    1.Hibernate史上最简单的Hibernate入门简介http://blog.csdn.net/doodoofish/article/details/43207/ jdbc不足 尽管JDBC在J ...

  10. Juqery 中使用 ajax

    从 test.js 载入 JSON 数据,附加参数,显示 JSON 数据中一个 name 字段数据. jQuery 代码: $.getJSON("test.js", { name: ...