[原创]Myeclipse+Weblogic+Mysql配置_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 2826 | 回复: 0   主题: [原创]Myeclipse+Weblogic+Mysql配置        下一篇 
geng.zhang
注册用户
等级:少校
经验:854
发帖:27
精华:0
注册:1970-1-1
状态:离线
发送短消息息给geng.zhang 加好友    发送短消息息给geng.zhang 发消息
发表于: IP:您无权察看 2014-12-9 15:38:26 | [全部帖] [楼主帖] 楼主

配置JDBC数据源,如下图:

北京联动北方科技有限公司

北京联动北方科技有限公司

数据源配置完成。

Myeclipse配置,如下图:

北京联动北方科技有限公司

将数据库的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
+---------+-----------+------------+------------+----------+


没有显示完全,代码存在一点问题,思路上没有错。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论