[原创]weblogic配置连接池并显示数据_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 1876 | 回复: 0   主题: [原创]weblogic配置连接池并显示数据        下一篇 
huan.xiong
注册用户
等级:上尉
经验:508
发帖:18
精华:0
注册:1970-1-1
状态:离线
发送短消息息给huan.xiong 加好友    发送短消息息给huan.xiong 发消息
发表于: IP:您无权察看 2015-7-13 11:11:43 | [全部帖] [楼主帖] 楼主

weblogic配置连接池并显示数据:
1、创建web项目:

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

######################################################################################################
Index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page import="com.landingbj.bean.*"%>
<%@ page import="com.landingbj.db.*" %>
<%@ page import="com.landingbj.dao.*" %>
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>weblogic 数据库连接池 测试<br>
<tableborder="1"cellpadding="2"cellspacing="2">
<tr><td>id</td><td>name</td><td>password</td></tr>
<%
ArrayList<Student> stus=new StudentDao().getAll();
Iterator it=stus.iterator();
System.out.println("id\t\t name\t\t password");
while(it.hasNext())
{
      Student stu=(Student)it.next();
      out.print("<tr><td>"+stu.getId()+"</td><td>"+stu.getName()+"</td><td>"+stu.getPassword()+"</td><tr>");
}
%>
</body>
</html>
#################################################################
Student.java
package com.landingbj.bean;
publicclass Student {
      privateint id;
      private String name;
      private String password;
      public Student()
      {
            super();
      }
      public Student(int id,String name,String password)
      {
            this.id=id;
            this.name=name;
            this.password=password;
      }
      publicint getId()
      {
            return id;
      }
      public String getName()
      {
            return name;
      }
      public String getPassword()
      {
            return password;
      }
}
#############################################################################################################
StudentDao.java
package com.landingbj.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.landingbj.bean.Student;
import com.landingbj.db.DBconnect;
import com.sun.org.apache.xml.internal.resolver.Catalog;
public class StudentDao {
      private Connection conn=null;
      private PreparedStatement ps=null;
      private ResultSet rs=null;
      private Student stu=null;
      private ArrayList<Student> stus=null;
      public ArrayList<Student> getAll()
      {
            conn=DBconnect.getConn();
            stus=new ArrayList<Student>();
            try
            {
                  ps=conn.prepareStatement("select * from tb1");
                  rs=ps.executeQuery();
                  while(rs.next())
                  {
                        stu=new Student(rs.getInt(1),rs.getString(2),rs.getString(3));
                        stus.add(stu);
                  }
            }
            catch (Exception e) {
                  // TODO: handle exception
            }finally
            {
                  try
                  {
                        if(null!=rs)
                        {
                              rs.close();
                        }
                        if(null!=ps)
                        {
                              ps.close();
                        }
                        if(null!=conn)
                        {
                              conn.close();
                        }
                  }catch (Exception e) {
                  // TODO: handle exception
            }
      }
      return stus;
}
}
###################################################################################################
DBconnect.java
package com.landingbj.db;
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
publicclass DBconnect {
      privatestatic Connection conn;
      /**

      * 连接数据库

      */
      publicstatic Connection getConn()
      {
            try
            {
                  Context ctx=new InitialContext();
                  //???是weblogic 的数据源名称
                  DataSource ds=(DataSource) ctx.lookup("pool2");
                  conn=ds.getConnection();
            }catch (Exception e)
            {
                  e.printStackTrace();
            }
            returnconn;
      }
}
#########################################################################################


配置数据源:

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

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

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

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

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

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

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

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

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

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

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

显示数据库中的表:

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




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