[转载]创建struts1.2 + Hibernate3.0 Web工程(三)

创建struts1.2 + Hibernate3.0 Web工程(三)

5. 调试工程

如果本地机器已经安装了Tomcat5,那么可以在MyEclilpse的环境下调试工程了。

指定Tomcat5 的web application service 如下图:

20060208104459652.jpg

将Enable选项打开,并且指定Tomcat的安装目录。

配置目前的工程到Tomcat中去。

20060208104459805.jpg

点选工具栏上的被红线圈出的按钮

在弹出来的对话框中选择,我们创建的工程,和添加Tomcat5 的web application service

20060208104459336.jpg

然后就可以按下工具栏上的启动服务按钮,调试这个Web工程了。

20060208104459955.jpg

每一次改动工程中的文件后,要想看看改动后的效果,那么就在配置工程中,重新发布这个工程到tomecat5去。

6. Tomcat下如何配置mysql的数据库连接池

6.1 配置server.xml

配置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.
--&gt

maxActive
100

<!-- Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
--&gt

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.
--&gt

maxWait
10000

<!-- MySQL dB username and password for dB connections --&gt

username
root


password


<!-- Class name for mm.mysql JDBC driver --&gt

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.
--&gt

urljdbc:mysql://192.168.0.208:3306
/db_test_account?autoReconnect=true


注意:

*其中蓝色字体表示你这个应用的路径和别名,也就是你访问自己配置的这个web站点的名字,注意区分大小写,必须一致,否则系统无法正常运行(例:http://localhost:8080/DBTest)

*其中红色字体表示数据源的名字,注意将会被web.xml和你访问数据库的程序调用

6.2 配置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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值