在tomcat6与tomcat5下是不同的。tomcat5下的网上到处都是,不说了。呵呵
1.在项目的WebRoot/META-INF/下建立 content.xml文件
文件内容为:<Context reloadable="true">
<Resource name="jdbc/test" auth="Container"
type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433;instance=SQLEXPRESS;DatabaseName=test"
username="sa" password="123456" maxActive="200" maxIdle="30"
maxWait="-1"/>
</Context>
2.在Dao中这样调用就可以了:
private DataSource dataSource;
Context ctx;
try
{
ctx = new InitialContext();
dataSource=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
}
catch (NamingException e)
{
e.printStackTrace();
}
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try
{
conn=getDataSource().getConnection();
}
catch (SQLException e)
{
throw e;
}
finally
{
closeResultSet(rs);
closePreparedStatement(pstmt);
closeConnection(conn);
}
1.在项目的WebRoot/META-INF/下建立 content.xml文件
文件内容为:<Context reloadable="true">
<Resource name="jdbc/test" auth="Container"
type="javax.sql.DataSource" driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://localhost:1433;instance=SQLEXPRESS;DatabaseName=test"
username="sa" password="123456" maxActive="200" maxIdle="30"
maxWait="-1"/>
</Context>
2.在Dao中这样调用就可以了:
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;
private DataSource dataSource;
Context ctx;
try
{
ctx = new InitialContext();
dataSource=(DataSource)ctx.lookup("java:comp/env/jdbc/test");
}
catch (NamingException e)
{
e.printStackTrace();
}
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try
{
conn=getDataSource().getConnection();
}
catch (SQLException e)
{
throw e;
}
finally
{
closeResultSet(rs);
closePreparedStatement(pstmt);
closeConnection(conn);
}