JNDI(Java Naming and Directory Interface,Java命名和目录接口)
2011-12-13 20:51
681 查看
使用JNDI相对与JDBC来说是他的灵活性,程序员不需要关心“具体的数据库后台是什么?JDBC驱动程序是什么?JDBC URL格式是什么?访问数据库的用户名和口令是什么?”等等这些问题,程序员编写的程序应该没有对
JDBC 驱动程序的引用,没有服务器名称,没有用户名称或口令
—— 甚至没有数据库池或连接管理。而是把这些问题交给J2EE容器来配置和管理,程序员只需要对这些配置和管理进行引用即可。
例子:
在tomcat6.0下配置context.xml, 将标签写到<Resource />
中
使用JNI的例子:
JDBC 驱动程序的引用,没有服务器名称,没有用户名称或口令
—— 甚至没有数据库池或连接管理。而是把这些问题交给J2EE容器来配置和管理,程序员只需要对这些配置和管理进行引用即可。
例子:
在tomcat6.0下配置context.xml, 将标签写到<Resource />
中
<Resource name="jdbc/student" auth="Container" type="javax.sql.DataSource" password="root" username="root" driverClassName="org.gjt.mm.mysql.Driver" url="jdbc:mysql://localhost/student" maxActive="100" maxIdle="30" maxWait="5000"/> </Resource>
使用JNI的例子:
try { c = new InitialContext(); ds = (DataSource) c.lookup("java:comp/env/jdbc/student"); System.out.println("===================="); conn = ds.getConnection(); System.out.println("===================="); s = conn.createStatement(); rs = s.executeQuery("select* from student"); ArrayList al = new ArrayList(); pw.println("<head><title></title></head><body>"); while(rs.next()) { al.add(new Student(rs.getInt(1),rs.getString("sname"))); } req.setAttribute("students", al); pw.println("</body>"); pw.println("</html>"); } catch (Exception e) { e.printStackTrace(); }
相关文章推荐
- 关于jndi(Java Naming and Directory Interface)java的命名与目录实例的接口
- JNDI(The Java Naming and Directory Interface,Java命名和目录接口
- JNDI(Java Naming and Directory Interface,Java命名和目录接口)
- JNDI(Java Naming and Directory Interface,Java命名和目录接口)
- JNDI Java的目录和命名接口(JAVA NAMING AND DERECTORY INTERFACE)
- JDNI数据共享(Java Naming and Directory Interface,Java命名和目录接口)
- Java华丽转身 —— J2EE规范(一) JNDI Java Naming and Directory Interface
- JNDI (The Java Naming and Directory Interface)基本概念
- JNDI(Java Naming and Directory Interface)介绍
- JNDI (Java Naming and Directory Interface)
- J2EE学习从菜鸟变大鸟之四 JNDI(Java Naming and Directory Interface)
- J2EE学习从菜鸟变大鸟之四 JNDI(Java Naming and Directory Interface)
- JNDI(Java Naming and Directory Interface)
- JNDI(Java Naming and Directory Interface)
- JNDI JNDI(Java Naming and Directory Interface)
- JNDI(Java Naming Directory Interface)
- [精华] JNDI(Java 命名和目录接口)
- JNDI Java Name and Directory Interface
- JNDI(Java 命名和目录接口)
- Java中的命名与目录接口JNDI基本操作方法概览