Tomcat6.0中配置数据源的方法
1. 打开%CATALINA_HOME%/conf/server.xml文件。在<Host>标签里面添加如下代码:
<Context path="/DB2" docBase="D:/JSP/DB2" debug="5" reloadable="true" crossContext="true"> <Resource name="jdbc/bookstore" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="-1" username="sa" password="123456" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=bookstore"/> </Context>
|
注:Tomcat5.5以下的版本的配置方法和该方法不同,如下:
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs"
prefix="localhost_log." suffix=".txt" timestamp="true"/>
<Context path="/DB2" docBase="D:/JSP/DB2" debug="0" reloadable="true">
<Resource name="jdbc/bookstore" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/bookstore">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>mao123</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:sqlserver://localhost:1433;DatabaseName=bookstore</value>
</parameter>
</ResourceParams>
</Context>
2. 在D:/JSP/DB2/src目录下编辑源文件:
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.ArrayList;
import javax.sql.*;
import javax.naming.*;
public class CreateDBServlet2 extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException
{
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
resp.setContentType("text/html;charset=gb2312");
try
{
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/bookstore");
conn=ds.getConnection();
stmt=conn.createStatement();
stmt.executeUpdate("use bookstore");
rs=stmt.executeQuery("select * from bookinfo");
ResultSetMetaData rsMeta=rs.getMetaData();
int columnCount=rsMeta.getColumnCount();
PrintWriter out=resp.getWriter();
out.println("<html><head><title>bookinfo数据表");
out.println("</title></head><body>");
out.println("<table border=1>");
out.println("<caption>表中的数据</catption>");
while(rs.next())
{
out.println("<tr>");
for(int i=1;i<=columnCount;i++)
{
out.println("<td>"+rs.getString(i)+"</td>");
}
out.println("</tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
out.close();
}
catch(NamingException ne)
{
ne.printStackTrace();
}
catch(SQLException se)
{
se.printStackTrace();
}
finally
{
if(stmt!=null)
{
try
{
stmt.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
stmt=null;
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException se)
{
se.printStackTrace();
}
conn=null;
}
}
}
}
3. 编译该源文件将生成的CreateDBServlet2.class 文件保存到D:/JSP/DB2/WEB-INF/classes/目录下面。
4. 将SQL Server 2005的JDBC驱动文件sqljdbc.jar复制到 D:/JSP/DB2/WEB-INF/lib路径下面。
5. 在D:/JSP/DB2/WEB-INF目录下新建Servlet配置文件web.xml 编写如下代码:
<?xml version="1.0" encoding="gb2312"?>
<web-app 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"
version="2.4">
<servlet>
<servlet-name>CreateDB</servlet-name>
<servlet-class>CreateDBServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CreateDB</servlet-name>
<url-pattern>/cdb2</url-pattern>
</servlet-mapping>
<resource-ref>
<descryiption>DB Connection</descryiption>
<res-ref-name>jdbc/bookstore</res-ref-name>
<res-type>javax.sql.DataSource </res-type>
<res-auth>Container </res-auth>
</resource-ref>
</web-app>
6. 用SQL Server Management Studio新建数据库bookstore和表bookinfo,在里面插入一些数据。
7. 在浏览器中输入:http://localhost:8000/DB2/cdb2 将得到数据表中的数据