链接http://junherry.iteye.com/admin/blogs/518539
在eclipse中,我们配置好tomcat服务器后,在左侧的项目一览表中会出现server的信息,连接数据源,需要我们更改里面的三个配置文件。假设我们在mysql中建了一个名为cms的数据库,我们把数据源的名字叫做jdbc/mysql。
首先是服务器上下文配置信息context.xml,在这里我们要在其元素<context></context>内添加数据源的配置信息:
<Resource name="jdbc/mysql" driverClassName="com.mysql.jdbc.Driver" maxActive="10" maxIdle="2" maxWait="300" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/cms?autoReconnect=true" username="root" password="admin" validationQuery="select * from student"/>
然后是server.xml. 在这里我们要在其元素<GlobalNamingResources><GlobalNamingResources>内添加数据源的配置信息:
<Resource name="jdbc/mysql" driverClassName="com.mysql.jdbc.Driver" maxActive="10" maxIdle="2" maxWait="300" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/cms?autoReconnect=true" username="root" password="admin" validationQuery="select * from student"/>
然后,再更改web.xml中的配置信息,在其元素<web-app></web-app>中添加下面的信息:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
我们可以写一个servlet,在其中测试一下数据源是否连通。
Context ctx = null;
DataSource ds = null;
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
ctx = new InitialContext();
if (ctx == null){
out.println("no context");
return;
}
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
if (ds != null)
conn = ds.getConnection();
stmt = conn.createStatement();
String strSql = "select * from student";
rs = stmt.executeQuery(strSql);
while (rs.next()) {
out.println(rs.getString(2));
}
} catch (Exception ex) {
ex.printStackTrace();
out.println(ex.toString());
} finally {
try{
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
if (ctx != null) ctx.close();
}catch(){}
}
分享到:
相关推荐
windows7操作系统下 Eclipse_64bit_+Tomcat6.0配置
tomcat6.0安装包 和 Eclipse的tomcat插件(3.2.1版本)
tomcatPluginV32.zip解压缩以后直接放在eclipse的plugin文件夹下,打开eclipse-》window-》preferences选择tomcat,设置tomcat6.0的位置
提供哦你了Eclipse3.4+Lomboz3.3+Tomcat6.0配置的下载地址,方便大家下载。
在 eclipse\plugins 路径下解压就ok了。
配置Eclipse 3.3 + tomcat 6.0 + lomboz 3.3进行Web开发, 只希望给某些需要的人一点帮助,特意搜集和制作了这个文档,内有已经验证过的安全资源好的下载地址,完毕
eclipse-jee-luna-SR2-win32-x86_64支持jdk1.7以上+tomcat6.0+tomcat7.0以上版本
tomcat6.0源码,可直接导入eclipse运行,包含相关需要的jar
tomcat5.5.25+mysql5.0.45+jdk1.0.5.13+eclipse3.3.1+myeclipse6.0配置
这是自己做的配置方案,主要给不想用myeclipse的朋友提供方便
tomcat6.0服务器,可以在上面运行java代码或者集成eclipse进行代码开发
Eclipse3.3+Tomcat6.0的Struts登陆
windows XP+Eclipse 3.3+tomcat 6.0+lomboz3.3.doc 可以,我测试过了
ECLIPSE3.3+tomcat6.0+lomboz3.3安装图解
Tomcat6.0+Eclipse3.3+Lomboz3.3图文配置详解
刚刚试过可以的,网上很多都不行,希望能帮到需要的人
Tomcat6.0+Eclipse3.3+Lomboz3.3图文配置详解,仅供大家参考
Tomcat6.0+Eclipse3.3+Lomboz3.3图文配置详解.doc WORD文档
Eclipse64bit+Tomcat6.0设置.pdf
Tomcat6.0+Eclipse3.3+Lomboz3.3图文配置详解.doc