名词解释:JNDI的全称是java命名与目录接口(Java Naming and Directory Interface),是一个应用程序设计的API,为开发人员提供了查找和访问各种命名和目录服务的通用、统一的接口。我们可以把JNDI简单地理解为是一种将对象和名字绑定的技术,即指定一个资源名称,将该名称与某一资源或服务相关联,当需要访问其他组件和资源时,就需要使用JNDI服务进行定位,应用程序可以通过名字获取对应的对象或服务。

1.context.xml文件设置

  数据源:Tomcat根目录\conf\context.xml文件(没有直接创建也可以)或者在JSP项目WebRoot目录下的META-INF目录中创建一个context.xml文件,添加Context节点,如下:

 <Context>
<Resource name="jdbc/test" auth="Application" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="sa" password="1234"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://192.168.2.254:1433;DatabaseName=test" />
</Context>

name:指定Resource的JNDI名字。

auth:可以写成Container和Application。Container表示由容器创建Resource,Application表示由Web应用创建和管理Resource。

type:指定Resource所属的Java类名。

maxActive:指定数据库连接池中处于活动状态的数据库连接的最大数目。

maxIdle:指定数据库连接池中处于空闲状态的数据库连接的最大数目,取值为0表示不受限制。

maxWait:指定数据库连接池中数据库连接处于空闲状态的最长时间(以毫秒为单位),超出这时间将会抛出异常。

username:指定连接数据库的用户名。

password:指定连接数据库的口令。

driverClassName:指定连接数据库的JDBC驱动程序。

url:指定连接数据库的URL。

2.web.xml文件的配置

  在Web应用程序的WEB-INF/web.xml文件中的<web-app>节点下添加<resource-ref>元素,内容如下:

 <web-app>
...
<resource-ref>
<description>news DataSource</description>
<res-ref-name>jdbc/test</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

description:对所引用资源的说明。

res-ref-name:指定所引用资源的JNDI名字,与<Resource>元素中的name属性对应。

res-type:指定所引用资源的类名字,与<Resource>元素中的type属性对应。

res-auth:指定管理所引用资源的Manager,与<Resource>元素中的auth属性对应。

3.在Web应用中添加数据库连接jar文件。

4.编写使用数据源和JNDI资源,创建采用数据库连接池Connection对象的SqlHelper.java文件(在helper包中创建的),代码如下:

 package helper;
import java.lang.reflect.*;
import java.sql.*;
import java.util.*; import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource; public class SqlHelper { private static Connection getConnection() throws ClassNotFoundException,
SQLException {
Connection conn = null;
try {
Context ctx = new InitialContext();
// 获取与逻辑名相关联的数据源对象
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/test");
conn = ds.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
} public static int executeNonQuery(String cmdText)
throws ClassNotFoundException, SQLException {
int result = -1;
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
ps = con.prepareStatement(cmdText);
result = ps.executeUpdate();
}catch (Exception e) {
System.out.println(e);
} finally {
close(con, ps, null);
}
return result;
} public static int executeScalar(String cmdText) throws SQLException,
ClassNotFoundException {
int result = -1;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();
ps = con.prepareStatement(cmdText);
rs = ps.executeQuery();
if (rs.next()) {
result = rs.getInt(1);
}
}catch (Exception e) {
System.out.println(e);
} finally {
close(con, ps, rs);
}
return result;
} public static <T> List<T> executeList(Class<T> cls, String cmdText)
throws ClassNotFoundException, SQLException,
InstantiationException, IllegalAccessException {
List<T> list = new ArrayList<T>();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();
ps = con.prepareStatement(cmdText);
rs = ps.executeQuery();
while (rs.next()) {
T obj = executeResultSet(cls, rs);
list.add(obj);
}
} catch (Exception e) {
System.out.println(e);
}finally {
close(con, ps, rs);
}
return list;
} public static <T> T executeEntity(Class<T> cls, String cmdText)
throws SQLException, ClassNotFoundException,
InstantiationException, IllegalAccessException {
T obj = null;
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();
ps = con.prepareStatement(cmdText);
rs = ps.executeQuery();
while (rs.next()) {
obj = executeResultSet(cls, rs);
break;
}
} catch (Exception e) {
System.out.println(e);
}finally {
close(con, ps, rs);
}
return obj;
} private static <T> T executeResultSet(Class<T> cls, ResultSet rs)
throws InstantiationException, IllegalAccessException, SQLException {
T obj = cls.newInstance();
ResultSetMetaData rsm = rs.getMetaData();
int columnCount = rsm.getColumnCount();
// Field[] fields = cls.getFields();
Field[] fields = cls.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
String fieldName = field.getName();
for (int j = 1; j <= columnCount; j++) {
String columnName = rsm.getColumnName(j);
if (fieldName.equalsIgnoreCase(columnName)) {
Object value = rs.getObject(j);
field.setAccessible(true);
field.set(obj, value);
break;
}
}
}
return obj;
} private static void close(Connection con, PreparedStatement ps, ResultSet rs)
throws SQLException {
if (rs != null) {
rs.close();
rs = null;
}
if (ps != null ) {
ps.close();
ps = null;
}
if (con != null) {
con.close();
con = null;
}
}
}

5.搭建entity,dal,bll包。(三层是从net开发转过来的,也可以使用do包,dao包)。

6.JSP页面使用代码如下:

 <%@ page language="java" import="java.util.*,bll.*,entity.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>采用数据源和JNDI资源方式,创建并使用数据库连接池对象访问数据库实例。</title>
</head>
<body>
<div>
<h1>采用数据源和JNDI资源方式,创建并使用数据库连接池对象访问数据库实例。</h1>
<%
List<Student> list = StudentBLL.Select();
for (Student student : list) {
out.println(String.format(
"<ol><li>StudentId:%d</li><li>StudentName:%s</li><li>Phone:%s</li><li>Email:%s</li><li>Address:%s</li></ol>",
student.getStudentId(), student.getStudentName(),
student.getPhone(), student.getEmail(),
student.getAddress()));
}
%>
</div>
</body>
</html>

7.如果Context文件中auth属性设置为:Container,需要将数据库连接的jar文件复制到Tomcat的lib目录下。如果auth属性设置为:Application,则不需要复制到Tomcat的lib目录下。

8.部署运行界面大功告成。

05-11 11:38
查看更多