将数据库的JDBC驱动程序包加入到CLASSPATH中,这边用的是mysql,对应包见上。
然后就是在myeclipse中建立web应用程序,相应的代码见下:
建立一个实体类命名为ItemDTO.java,代码如下:
package com.zg.pet.dto;
publicclass ItemDTO {
private String ItemId;
private String itemName;
private String itemPrice;
private String itemStand;
private String itemDes;
public ItemDTO(String itemId, String itemName, String itemPrice,
String itemStand, String itemDes) {
super();
ItemId = itemId;
this.itemName = itemName;
this.itemPrice = itemPrice;
this.itemStand = itemStand;
this.itemDes = itemDes;
}
public String getItemId() {
returnItemId;
}
publicvoid setItemId(String itemId) {
ItemId = itemId;
}
public String getItemName() {
returnitemName;
}
publicvoid setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemPrice() {
returnitemPrice;
}
publicvoid setItemPrice(String itemPrice) {
this.itemPrice = itemPrice;
}
public String getItemStand() {
returnitemStand;
}
publicvoid setItemStand(String itemStand) {
this.itemStand = itemStand;
}
public String getItemDes() {
returnitemDes;
}
publicvoid setItemDes(String itemDes) {
this.itemDes = itemDes;
}
}
相对应的就有一个在页面上显示商品信息的TBItemDAO.java类,代码如下:
package com.zg.pet.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zg.pet.dto.ItemDTO;
public class TBItemDAO {
public List<ItemDTO> listitems(){
List<ItemDTO> item=new ArrayList<ItemDTO>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db_pet","root","mysql");
String sql="select * from tb_item";
PreparedStatement ps=conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
String id=rs.getString("item_id");
String name=rs.getString("item_name");
String price=rs.getString("item_price");
String stand=rs.getString("item_stand");
String des=rs.getString("item_des");
ItemDTO items=new ItemDTO(id,name,price,stand,des);
item.add(items);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return item;
}
}
实现的servlet命名为AdminItemListServlet.java,代码如下:
package com.zg.pet.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zg.pet.dao.TBItemDAO;
import com.zg.pet.dto.ItemDTO;
public class AdminItemListServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
TBItemDAO idao=new TBItemDAO();
List<ItemDTO> items=idao.listitems();
request.setAttribute("its", items);
request.getRequestDispatcher("admin_item_list.jsp").forward(request, response);
}
}
相应的jsp页面admin_item_list.jsp页面代码如下:
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'admin_item_list.jsp' starting page</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<p>商品列表页面</p>
<tablewidth="80%"align="center"border="2"cellpadding="0"cellspacing="0">
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品规格</th>
<th>商品描述</th>
<th>操作</th>
</tr>
<c:forEach items=”${its}” var=”item”>
<tr>
<td>${item.itemId}</td>
<td>${item.itemName}</td>
<td>${item.itemPrice}</td>
<td>${item.itemStand}</td>
<td>${item.itemDes}</td>
<td>
<ahref="AdminItemQueryServlet?id=${item.itemId}">修改</a>
<ahref="AdminItemDeleteServlet?id=${item.itemId}"onclick="javascript:return confirm('你确定删除吗')">删除</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
然后,将此web程序打包成war包,部署到weblogic服务器上,在网页中输入http://wsl:7001/petshop/admin_item_list.jsp,显示的内容如下:
注:在mysql中,表的名字为tb_item,如下:
mysql> select * from tb_item;
+---------+-----------+------------+------------+----------+
item_id item_name item_price item_stand item_des
+---------+-----------+------------+------------+----------+
115031 wanglaoji 30 wanglaoji buhaohe
12036 wahaha 20 wahaha henhaohe
+---------+-----------+------------+------------+----------+
没有显示完全,代码存在一点问题,思路上没有错。