TOMCAT 数据源配置的问题

meran 2011-08-22 04:44:57
我是按如下配置的


<?xml version='1.0' encoding='utf-8' >
<Context >


<Resource name="jdbc/sqlconnpool"

auth="Container"

type="javax.sql.DataSource"

driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/store"

username="root"

password="root"

maxActive="100"

maxIdle="30"

maxWait="10000" />


</Context>




上面是tomcat 下面的 context

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ServletTest</display-name>


<resource-ref>

<description>DB Connection</description>

<res-ref-name>jdbc/sqlconnpool</res-ref-name>

<res-type>javax.sql.DataSource</res-type>

<res-auth>Container</res-auth>

</resource-ref>

</web-app>

报错是[code=Java]
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'



感觉是读不到参数啊。。。求拯救


[/code]
...全文
156 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
beiouwolf 2011-08-24
  • 打赏
  • 举报
回复
汗了...你server.xml里没配过...那你那些链接配置写哪里的...
写在server.xml里,像这样

<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">


<Context docBase="E:/tools/tomcat/webapps/product" path="/product" debug="5" reloadable="true" crossContext="true">
<Resource name="jdbc/logreport"
auth="Container"
description="logreport"
driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
maxPoolSize="10"
minPoolSize="2"
acquireIncrement="2"
user="sa"
password="123456"
factory="org.apache.naming.factory.BeanFactory"
type="com.mchange.v2.c3p0.ComboPooledDataSource"
idleConnectionTestPeriod="10"
preferredTestQuery="select 1"
testConnectionOnCheckin="true"
autoCommitOnClose="true"
jdbcUrl="jdbc:sqlserver://10.0.0.44:1433;databaseName=log_report"/>


</Context>

</Host>
meran 2011-08-23
  • 打赏
  • 举报
回复

<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">

<!--APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
<Listener className="org.apache.catalina.core.JasperListener" />
<!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>

<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">

<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
maxThreads="150" connectionTimeout="20000"
redirectPort="8443" />

<!-- Define a SSL HTTP/1.1 Connector on port 8443
This connector uses the JSSE configuration, when using APR, the
connector should be using the OpenSSL style configuration
described in the APR documentation -->
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->

<!-- Define an AJP 1.3 Connector on port 8009 -->
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->

<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">

<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->

<!-- The request dumper valve dumps useful debugging information about
the request and response data received and sent by Tomcat.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.RequestDumperValve"/>
-->

<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>

<!-- Define the default virtual host
Note: XML Schema validation will not work with Xerces 2.2.
-->
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">

<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->

<!-- Access log processes all example.
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
-->

</Host>
</Engine>
</Service>
</Server>

是server 这个吧? 没配过。。。
meran 2011-08-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 beiouwolf 的回复:]
你的lookup怎么写的
ctx.lookup("java:comp/env/jdbc/sqlconnpool")
[/Quote]
嗯 是的
wenjie4892543 2011-08-23
  • 打赏
  • 举报
回复
上面 paneyjiang的估计是对的!!
wenjie4892543 2011-08-23
  • 打赏
  • 举报
回复
不懂!!
beiouwolf 2011-08-23
  • 打赏
  • 举报
回复
你的tomcat配置文件贴全看看
是不是没把context段放在Host段里面
beiouwolf 2011-08-22
  • 打赏
  • 举报
回复
你的lookup怎么写的
ctx.lookup("java:comp/env/jdbc/sqlconnpool")
meran 2011-08-22
  • 打赏
  • 举报
回复
真心不给力,还是

org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
这个错误
meran 2011-08-22
  • 打赏
  • 举报
回复
放了。 感觉是读不到第一个配置文件啊
beiouwolf 2011-08-22
  • 打赏
  • 举报
回复
tomcat版本多少
低于5.0的要把jdbc的包放在tomcat/common/lib下
5.0及以上把包放在tomcat/lib下

softroad 2011-08-22
  • 打赏
  • 举报
回复
我一般配置文件config.properties

url=jdbc:sqlserver://10.144.10.134:1433;Databasename=Test;SendStringParametersAsUnicode=False;
user=test
pwd=test

proxyport=8080
proxyhostname=proxy.test.com
softroad 2011-08-22
  • 打赏
  • 举报
回复
百度一般不行,得直接上google
paneyjiang 2011-08-22
  • 打赏
  • 举报
回复


1 下载Tomcat最新版本
下载地址:http://tomcat.apache.org/
2 下载mysql最新版本以及最近版本的驱动程序
下载地址:http://dev.mysql.com/downloads
http://dev.mysql.com/downloads/connector
并将下载的mysql-connector-java-5.1.0-bin.jar a连接文件放到$CATALINA_HOME/lib/下。
3 安装mysql数据库
4 修改$CATALINA_HOME/conf/ context.xml,<Context></Context>之间加入如下内容

<!-- maxActive: 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.
-->
<!-- maxIdle: Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
-->
<!-- maxWait: 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.
-->
<!-- username and password: MySQL dB username and password for dB connections -->
<!-- driverClassName: Class name for the old mm.mysql JDBC driver is
org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
-->
<!-- url: 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.
-->


<Resource name="jdbc/mldn"

auth="Container"

type="javax.sql.DataSource"

maxActive="100"

maxIdle="30"

maxWait="10000"

username="root"

password="admin"

driverClassName="com.mysql.jdbc.Driver"

url="jdbc:mysql://localhost:3306/mldn?autoReconnect=true"/>

此时要注意修改自己的数据库的用户名和密码
我建立的
数据库:mldn
用户名:root
密码:admin
5 修改工程目录下的web.xml文件添加如下
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mldn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
6 创建一个java类
package com.test;

package com.test;



import javax.naming.*;

import javax.sql.*;

import java.sql.*;



public class DBTest {

String foo = "Not Connected";

int bar = -1;

String name=null;

String password=null;

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/mldn");

if (ds != null) {

Connection conn = ds.getConnection();

if (conn != null) {

foo = "Got Connection " + conn.toString();

Statement stmt = conn.createStatement();

ResultSet rst = stmt.executeQuery(" select * from admin ");

if (rst.next()) {

foo = rst.getString(1);

bar = 208;

name=rst.getString(2);

password=rst.getString(3);

}

conn.close();

}

}

} catch (Exception e) {

e.printStackTrace();

}

}



public String getFoo() {

return foo;

}



public int getBar() {

return bar;

}

public String getName() {

return name;

}



public String getPassword() {

return password;

}

}


7 编辑index.jsp

<%@ page language="java" pageEncoding="GB2312"%>

<%@ page import="com.test.*"%>

<html>

<head>

<title>DB Test</title>

</head>

<body>

<%

DBTest tst = new DBTest();

tst.init();

%>

<h2>

Results

</h2>

Foo

<%=tst.getFoo()%><br>

Bar

<%=tst.getBar()%>

<br>

姓名:<%=tst.getName() %>

<br>

密码:<%=tst.getPassword() %>

</body>

</html>

meran 2011-08-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 softroad 的回复:]
百度下么
[/Quote]
这就是百度的结果。。 失败了
softroad 2011-08-22
  • 打赏
  • 举报
回复
百度下么
romanitc 2011-08-22
  • 打赏
  • 举报
回复
没有搞过这样滴,友情帮顶!
meran 2011-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 softroad 的回复:]
没这样配过,帮顶。
[/Quote]

你是咋样配的 给我贴一下吧
softroad 2011-08-22
  • 打赏
  • 举报
回复
没这样配过,帮顶。

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧