######################################################################################################
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;
}
}
#########################################################################################