href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml" rel="File-List" /> href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx" rel="themeData" /> href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml" rel="colorSchemeMapping" />
配置JNDI连接池步骤:
1
在tomcat/cof/context.xml的<context> </context>中配置如下
<!-- The contents of this file will be loaded for each web application -->
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname=""/>
-->
<Resource name="mysql"
auth="Container"
type="javax.sql.DataSource" factory="org.apache.commons.dbcp.BasicDataSourceFactory"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/phyllis?useUnicode=true&characterEncoding=gb2312"
username="root"
password="root"
maxActive="20"
maxIdle="10"
maxWait="-1"/>
</Context>
2
在你的项目的web.xml文件中配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<resource-ref>
<description>shuju</description>
<res-ref-name>mysql</res-ref-name>、、///这个名字在java文件中要用到,绑定context、//(DataSource)context.lookup(“java:comp/env/mysql”);”java:comp/env”是固定的,mysql就是这里的res-ref-name在的值
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
注意以上的文件中不可有多余的标记,想多个 > < 等标记都会报错
3
import javax.sql.DataSource;
import javax.naming.*;
import java.sql.*;
public class DBConnection {
private static DataSource ds=null;
static { init();}
public static void init(){
try{
Context context=new InitialContext();//create naming encironment
if(context==null)
throw new Exception (" not Context");
ds=(DataSource)context.lookup("java:comp/env/mysql");
}catch(Exception e){
e.printStackTrace();
}
}
public static Connection getConnection()throws SQLException{
if(ds==null)
throw new SQLException(" DataSource is null");
else
System.out.print("success return ds");
return ds.getConnection();
}
}
4
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class TestNaming {
public static Vector getAll(){
Connection con;
Statement stmt;
Vector vector=new Vector();
try{
con=DBConnection.getConnection();
String sql="select id from member";
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
vector.add(rs.getObject(1));
//System.out.println(rs.getString(1)+" "+rs.getString(2));
}
}catch(SQLException e){
e.printStackTrace();
}
return vector;
}
}
注意这里的context是servlet中的类,所以要到web中发布,不能编写一个java static类测试