?????????myeclipse+tomcat7.0+server');" target="_self">sqlserver2008??????????????
???????崠?裺
????1????server.xml???????????????sql server 2008?????????????£?
??????<GlobalNamingResources> </GlobalNamingResources>????м???
<Resource
name="jdbc/DBPool"
type="javax.sql.DataSource"
password="aaaaaa"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:sqlserver://localhost:1433;DataBaseName=COFFEE"
maxActive="4"/>
?????????????name????????????????”jdbc/XXX”??????
????type??”javax.sql.DataSource”;
????password??????????????
????driveClassName?????????????
????maxIdle????????????????????????????????????????????????????????????????????????0??????????
????MaxActive?????????????????????????0??????????
????maxWait ??????????????????????????佫??????????-1??????????
????2???????web??ó????web.xml????????????ο??????£?
??????<web-app></web-app>????м???
<resource-ref>
<description>DB Connection Pool</description>
<res-ref-name>jdbc/DBPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
????????????? description???????????
????res-ref-name???ο???????????????????????name??
????res-type??????????”javax.sql.DataSource”??
????res-auth??”Container”??
????res-sharing-scope??”Shareable”??
????3????tomcat???μ?context.xml???????????????????£?
??????<Context></Context>?м???
<ResourceLink
name="jdbc/DBPool"
type="javax.sql.DataSource"
global="jdbc/DBPool"/>
?????????????name?????2?????3????????name?????????res-ref-name???
????type??????”javax.sql.DataSource”??
????global???name???
????4???????
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Test {
private static DataSource pool;
public static void main(String[] args) {
Context env = null;
try {
env = (Context) new InitialContext().lookup("java:comp/env");
pool = (DataSource)env.lookup("jdbc/DBPool");
if(pool==null)
System.err.println("'DBPool' is an unknown DataSource");
} catch(NamingException ne) {
ne.printStackTrace();
}
Connection conn;
try {
conn = pool.getConnection();
String sql = "select * from allbook";
PreparedStatement ps;
ps = conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
System.out.println(rs.getString("BOOKNAME"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}