修改Tomcat 5.5\conf目录下的context.xml,加入下列
<Resource name="jdbc/mobile" type="javax.sql.DataSource" password="neusoft" driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2" maxWait="5000" username="sa" url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=OK"
maxActive="4" />
maxIdle="2" maxWait="5000" username="sa" url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=OK"
maxActive="4" />
其中,以后只修改mobile为你的工程名,url中为你的数据库连接字符串
下面是读取连接的Java代码
package beans;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Cone {
public Connection init() {
Connection conn = null;
try
{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mobile");
if (ds != null)
{
conn = ds.getConnection();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
}
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class Cone {
public Connection init() {
Connection conn = null;
try
{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/mobile");
if (ds != null)
{
conn = ds.getConnection();
}
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
}