<Context path="/Test" docBase="Test" debug="0" reloadable="true" crossContext="true">
<Resource name="jdbc/sittc" auth="Container" type="com.microsoft.jdbcx.sqlserver.SQLServerDataSource"/>
<ResourceParams name="jdbc/sittc">
<parameter>
<name>factory</name>
<value>com.microsoft.jdbcx.sqlserver.SQLServerDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbcx.sqlserver.SQLServerDataSource</value>
</parameter>
<parameter>
<name>driverName</name>
<value>SQLServer</value>
</parameter>
<parameter>
<name>description</name>
<value>My DataSource</value>
</parameter>
<parameter>
<name>serverName</name>
<value>127.0.0.1</value>
</parameter>
<parameter>
<name>portNumber</name>
<value>1433</value>
</parameter>
<parameter>
<name>selectMethod</name>
<value>cursor</value>
</parameter>
<parameter>
<name>databaseName</name>
<value>****</value>
</parameter>
<parameter>
<name>user</name>
<value>******</value>
</parameter>
<parameter>
<name>password</name>
<value>*******</value>
</parameter>
<parameter>
<name>loginTimeout</name>
<value>3000</value>
</parameter>
</ResourceParams>
</Context>
另外还要在WEB-INF 的web.xml里
在<web-app>
中加这一段
<resource-ref>
<description>My DataSource</description>
<res-ref-name>jdbc/sittc</res-ref-name>
<res-type>com.microsoft.jdbcx.sqlserver.SQLServerDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<res-ref-name>要改成servlet.xml里所写的
连接程式:
package com.richer.db;
import java.sql.*;
import javax.sql.DataSource;
import java.util.*;
import javax.naming.*;
/**
*
* @author Administrator
*/
public class ConnectionMySqlDB {
private Connection conn=null;
public Statement stmt=null;
public Statement stmt1=null;
public void CreateConnection(){
try{
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds=(DataSource)envContext.lookup("jdbc/MySqlDB");
conn=ds.getConnection();
stmt=conn.createStatement();
stmt1=conn.createStatement();
}catch(NamingException ne){
System.out.println("NamingException:"+ne.toString());
}catch(SQLException se){
System.out.println("SQLException:"+se.toString());
}
}
public void freeConnection(){
try{
if(conn!=null){
conn.close();
conn=null;
}
if(stmt!=null){
stmt.close();
stmt=null;
}
if(stmt1!=null){
stmt1.close();
stmt1=null;
}
}catch(SQLException se){
System.out.println("Close DataBase Exception:"+se.toString());
}
}
}