如果本地机器已经安装了Tomcat5,那么可以在MyEclilpse的环境下调试工程了。
指定Tomcat5 的web application service 如下图:
将Enable选项打开,并且指定Tomcat的安装目录。
配置目前的工程到Tomcat中去。
点选工具栏上的被红线圈出的按钮
在弹出来的对话框中选择,我们创建的工程,和添加Tomcat5 的web application service
然后就可以按下工具栏上的启动服务按钮,调试这个Web工程了。
每一次改动工程中的文件后,要想看看改动后的效果,那么就在配置工程中,重新发布这个工程到tomecat5去。
配置Tomcat的server.xml文件,路径:【TOMCAT_HOME】commonlib下的server.xml文件在之前加入以下内容以添加JNDI数据源:
DBTest" docBase="DBTest"
debug="5" reloadable="true" crossContext="true">
prefix="localhost_DBTest_log." suffix=".txt"
timestamp="true"/>
jdbc/TestDB"
auth="Container"
type="javax.sql.DataSource"/>
jdbc/TestDB">
factory
org.apache.commons.dbcp.BasicDataSourceFactory
<!-- Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
-->
maxActive
100
<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
-->
maxIdle
30
<!-- Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
-->
maxWait
10000
<!-- MySQL dB username and password for dB connections -->
username
root
password
<!-- Class name for mm.mysql JDBC driver -->
driverClassName
org.gjt.mm.mysql.Driver
<!-- The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
-->
urljdbc:mysql://192.168.0.208:3306
/db_test_account?autoReconnect=true
注意:
*其中蓝色字体表示你这个应用的路径和别名,也就是你访问自己配置的这个web站点的名字,注意区分大小写,必须一致,否则系统无法正常运行(例:http://localhost:8080/DBTest)
*其中红色字体表示数据源的名字,注意将会被web.xml和你访问数据库的程序调用
配置Web用程序的web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
br /> "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
MySQL Test App
DB Connection
jdbc/TestDB
javax.sql.DataSource
Container
6.3 访问数据库的程序片段
package swt.db.DBUtility;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;
public class DBTest {
String foo = "Not Connected";
int bar = -1;
public void init() {
try{
Context ctx = new InitialContext();
if(ctx == null )
throw new Exception("Boom - No Context");
DataSource ds =
(DataSource)ctx.lookup(
"java:comp/env/jdbc/TestDB");
if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) {
foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery(
" select UserName from t_account ");
if(rst.next()) {
foo=rst.getString(1);
bar=208;
}
conn.close();
}
}
}catch(Exception e) {
e.printStackTrace();
}
}
public String getFoo() { return foo; }
public int getBar() { return bar;}
}
6.4 Jsp页面(index.jsp)
DB Test
DBTest tst = new DBTest();
tst.init();
%>
Results
Foo
Bar
启动Tomcat在浏览器上输入http://localhost:8080/DBTest
备注:
连接池配置(Database Connection Pool (DBCP) Configurations)
DBCP使用的是Jakarta-Commons Database Connection Pool 要使用连接池需要如下的组件即jar文件
Jakarta-Commons DBCP 1.1 对应commons-dbcp-1.1.jar。
Jakarta-Commons Collections 2.0 对应commons-collections.jar。
Jakarta-Commons Pool 1.1 对应commons-pool-1.1.jar。
这三个jar文件要与你的JDBC驱动程序一起放到【TOMCAT_HOME】commonlib目录下以便让tomcat和你的web应用都能够找到。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/374079/viewspace-131230/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/374079/viewspace-131230/