我在META-INF目录下的context.xml如下:<?xml version="1.0" encoding="UTF-8"?>
<Context path="/DbHelper" docBase="DbHelper" auth="Container" debug= "0 " reloadable= "true "> >
<Resource name="jdbc/DbHelper" scope="Shareable"
type="javax.sql.DataSource"
url="jdbc:mysql://127.0.0.1:3306/newdatabase"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="root"
maxWait="-1" maxIdle="-1" maxActive="0" />
</Context>
在WEB-INF目录下的web.xml如下在<web-app></web-app>中间加入这段:<resource-ref>
<res-ref-name>jdbc/DbHelper</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后我的测试代码如下:<%@page contentType="text/html;charset=utf-8 "pageEncoding="utf-8" %>
<%@page import="java.sql.*" %>
<%@page import="DbPackage.*"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.sql.DataSource"%><html>
<body>
<%
DataSource ds = null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/DbHelper");
conn=ds.getConnection();
if(conn==null){out.println("FUCK!!");}
statement=conn.createStatement();
String sql="select * from STUDENT";
rs=statement.executeQuery(sql);
if(ds!=null)
{ out.println("Success!");
}
if(conn!=null)
{
conn.close();
}
if(rs!=null)
{
rs.close();
}
if(statement!=null)
{
statement.close();
}
}
catch(SQLException e)
{out.println("ERROR:"+e); }
finally
{
if(conn!=null)
{
conn.close();
}
if(rs!=null)
{
rs.close();
}
if(statement!=null)
{
statement.close();
}
}
%> </body>
</html>
执行结果是不成功的,抛出的错误是以下这个:
ERROR:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver' 请问为什么会抛出load不了driver的错误呢?我测试过数据库连接是正常的,在WEB-INF/lib目录下也有MYSQL的驱动,但是用这种方式就连接不上,请问这是什么问题呢?
请大臣们给我解释一下,不胜言谢!!
又或者谁有能用的连接池,可以分享一下吗?谢谢!
<Context path="/DbHelper" docBase="DbHelper" auth="Container" debug= "0 " reloadable= "true "> >
<Resource name="jdbc/DbHelper" scope="Shareable"
type="javax.sql.DataSource"
url="jdbc:mysql://127.0.0.1:3306/newdatabase"
driverClassName="com.mysql.jdbc.Driver"
username="root" password="root"
maxWait="-1" maxIdle="-1" maxActive="0" />
</Context>
在WEB-INF目录下的web.xml如下在<web-app></web-app>中间加入这段:<resource-ref>
<res-ref-name>jdbc/DbHelper</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后我的测试代码如下:<%@page contentType="text/html;charset=utf-8 "pageEncoding="utf-8" %>
<%@page import="java.sql.*" %>
<%@page import="DbPackage.*"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="javax.sql.DataSource"%><html>
<body>
<%
DataSource ds = null;
Connection conn=null;
Statement statement=null;
ResultSet rs=null;
try
{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/DbHelper");
conn=ds.getConnection();
if(conn==null){out.println("FUCK!!");}
statement=conn.createStatement();
String sql="select * from STUDENT";
rs=statement.executeQuery(sql);
if(ds!=null)
{ out.println("Success!");
}
if(conn!=null)
{
conn.close();
}
if(rs!=null)
{
rs.close();
}
if(statement!=null)
{
statement.close();
}
}
catch(SQLException e)
{out.println("ERROR:"+e); }
finally
{
if(conn!=null)
{
conn.close();
}
if(rs!=null)
{
rs.close();
}
if(statement!=null)
{
statement.close();
}
}
%> </body>
</html>
执行结果是不成功的,抛出的错误是以下这个:
ERROR:org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver' 请问为什么会抛出load不了driver的错误呢?我测试过数据库连接是正常的,在WEB-INF/lib目录下也有MYSQL的驱动,但是用这种方式就连接不上,请问这是什么问题呢?
请大臣们给我解释一下,不胜言谢!!
又或者谁有能用的连接池,可以分享一下吗?谢谢!
你要把数据库驱动包拷贝到tomcat里面,
这样不行的。因为我漏了打一句“我已经将MYSQL的驱动放到tomact下的common/lib下了”再附加一句:我用的TOMCAT版本是6.0希望有人能帮助我~~~
<?xml version="1.0" encoding="UTF-8"?>
<Context reloadable="true" crossContext="true">
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Resource
name="jdbc/uumvs"
type="javax.sql.DataSource"
maxActive="1000"
maxIdle="30"
maxWait="10000"
removeAbandoned="true"
removeAbandonedTimeout="60"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/uumvs?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true"
username="vutc"
password="vutc"
/>
</Context>ps:
maxIdle,最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连
接将被标记为不可用,然后被释放。设为0表示无限制。
MaxActive,连接池的最大数据库连接数。设为0表示无限制。
maxWait ,最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示
无限制。*maxActive:最大连接数据库连接数,设 0 为没有限制
*maxIdle:最大等待连接中的数量,设 0 为没有限制
*maxWait:最大等待毫秒数, 单位为 ms, 超过时间会出错误信息一般把maxActive设置成可能的并发量就行了maxActive、maxIdle和maxWait参数:
maxActive是最大激活连接数,这里取值为20,表示同时最多有20个数据库连
接。maxIdle是最大的空闲连接数,这里取值为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,随时处于待命状态。MaxWait是最大等待秒钟数,这里取值-1,表示无限等待,直到超时为止,也可取值9000,表示9秒后超时。
Connection conn = null;
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象 DataSource ds = (DataSource)envCtx.lookup("jdbc/uumvs"); conn = ds.getConnection();
return conn;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
LS的解释的很清楚楼上的获得链接的方式有点像EJB
依赖查找,感觉还是spring的ioc比较好
把driverClassName="com.mysql.jdbc.Driver"
换成 driverClassName="org.gjt.mm.mysql.Driver"