标签:tomcat
它相当于客户端程序和连接池的中介,想要获得连接池中的连接对象,必须建立一个与该连接池相应的数据源,然后通过该数据源获得连接。
import javax.naming.*; public class TestJNDI { public static void main(String[] args) { try { Context ctx=new InitialContext(); Object object=ctx.lookup(“JNDIName”); //根据JNDI名查找绑定的对象 String str=(String) object; //强制转换 } catch(NamingException e) { e.printStackTrace(); } catch(ClassCastException e) { e.printStackTrace(); } } }
Context ctx = new InitialContext(); DataSource ds = (DataSource)ctx.lookup("jdbc/EmployeeDB"); Connection con = ds.getConnection("myPassword", "myUserName"); 或者: Context ctx = new InitialContext(); ConnectionPoolDataSource ds = (ConnectionPoolDataSource)ctx.lookup("jdbc/EmployeeDB"); PooledConnection con = ds.getConnection("myPassword", "myUserName");
try {… } catch() {… } finally { if(con!=null) con.close(); }
Jakarta-Commons DBCP 1.1 对应commons-dbcp-1.1.jar。
Jakarta-Commons Collections 2.0 对应commons-collections.jar。
Jakarta-Commons Pool 1.1 对应commons-pool-1.1.jar。<Context path="/WebMis" docBase="WebMis" debug="0" reloadable="true"> <Resource name="jdbc/webmis" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/webmis"> <parameter> <name> factory </name> <value>org.apache.commons.dbcp.BasicDataSourceFactory </value> </parameter> <parameter> <name> driverClassName </name> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver </value> </parameter> <parameter> <name> url </name> <value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=DataBase </value> </parameter> <parameter> <name> username </name> <value> sa </value> </parameter> <parameter> <name> password </name> <value> </value> </parameter> <parameter> <name> maxActive </name> <value> 20 </value> </parameter> <parameter> <name> maxIdle </name> <value>10</value> </parameter> <parameter> <name>maxWait</name> <value>-1</value> </parameter> <parameter> <name>removeAbandoned</name> <!-- Abandoned DB connections are removed and recycled --> <value>true</value> </parameter> <parameter> <name>removeAbandonedTimeout</name> <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. --> <value>60</value> </parameter> <parameter> <name>logAbandoned</name> <!-- Log a stack trace of the code which abandoned --> <value>false</value> </parameter> </ResourceParams> </Context>
try { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource)envCtx.lookup("jdbc/webmis"); Connection con=ds.getConnection(); } catch (NamingException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }
<!-- Database Config start --> <resource-ref> <description>connectDB test</description> <res-ref-name>jdbc/webmis</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> <!-- Database Config end -->
<Context path="/mywebapp" docBase="C:/mywebapp" debug="0" reloadable="true"> <Resource name="jdbc/mybonusds" auth="Container" type="javax.sql.DataSource"/> <ResourceParams name="jdbc/mybonusds"> <parameter> <name>factory</name> <value>org.apache.commons.dbcp.BasicDataSourceFactory</value> </parameter> <parameter> <name>driverClassName</name> <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value> </parameter> <parameter> <name>url</name> <value> jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=DataBonus </value> </parameter> <parameter> <name>username</name> <value>sa</value> </parameter> <parameter> <name>password</name> <value></value> </parameter> <parameter> <name>maxActive</name> <value>20</value> </parameter> <parameter> <name>maxIdle</name> <value>10</value> </parameter> <parameter> <name>maxWait</name> <value>-1</value> </parameter> <parameter> <name>removeAbandoned</name> <!-- Abandoned DB connections are removed and recycled --> <value>true</value> </parameter> <parameter> <name>removeAbandonedTimeout</name> <!-- Use the removeAbandonedTimeout parameter to set the number of seconds a DB connection has been idle before it is considered abandoned. --> <value>60</value> </parameter> <parameter> <name>logAbandoned</name> <!-- Log a stack trace of the code which abandoned --> <value>false</value> </parameter> </ResourceParams> </Context>
<%--字符集设为"gb2312",使动态页面支持中文--%> <%@ page contentType="text/html; charset=GB2312"%> <!-- 这里使用一个字串变量 ("PAGETITLE") 保持题目和主标题的一致性。--> <html> <head> <title> <%= pagetitle %> </title> </head> <body bgcolor=#FFFFFF> <font face="Helvetica"> <h2> <font color=#DB1260> <%= pagetitle %> </font> </h2> <!-- 导入必要的类和类库 --> <%@ page import=" javax.naming.*, java.sql.*, javax.sql.DataSource "%> <!-- 声明一个类方法 --> <%! //声明变量 //标题 String pagetitle = "这是JSP调用数据库的例子"; %> <!-- 下面这些代码将被插入到servlet中 --> <% java.sql.Connection conn= null; java.sql.Statement stmt =null; java.sql.ResultSet rs=null; try { // 通过JNDI获取主接口 Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource)envCtx.lookup("jdbc/mybonusds"); conn=ds.getConnection(); stmt = conn.createStatement(); //执行SQL语句 stmt.execute("select * from 奖金"); //取得结果集 rs = stmt.getResultSet(); %> <table border="1"> <tr> <td width="60" height="20"><% out.print("编号"); %></td> <td width="80" height="20"><% out.print("姓名"); %></td> <td width="200" height="20"><% out.print("发奖名称"); %></td> <td width="100" height="20"><% out.print("金额"); %></td> <td width="200" height="20"><% out.print("备注"); %></td> </tr> <% while (rs.next()) { %> <tr> <td width="60" height="20"><% out.print(rs.getString("编号")); %></td> <td width="80" height="20"><% out.print(rs.getString("姓名")); %></td> <td width="200" height="20"><% out.print(rs.getString("发奖名称")); %></td> <td width="100" height="20"><% out.print(rs.getString("金额")); %></td> <td width="200" height="20"><% out.print(rs.getString("备注")); %></td> </tr> <% } %> </table> <% // Catch exceptions } catch (Exception e) { } finally { if (rs != null) { try{rs.close();}catch(Exception ignore){}; } if (stmt != null) { try{stmt.close();}catch(Exception ignore){}; } if (conn != null) { try{conn.close();}catch(Exception ignore){}; } %> <% } %> </font> </body> </html>
标签:tomcat
原文地址:http://blog.csdn.net/u011794238/article/details/44059419