当前位置: 代码迷 >> Java Web开发 >> 求助,数据库连接池配置问题
  详细解决方案

求助,数据库连接池配置问题

热度:154   发布时间:2006-08-04 15:33:21.0
求助,数据库连接池配置问题


在conf文件夹下中的server.xml中的
<host>和</host>中加入下面的代码
:
<Context path="" docBase="ROOT" debug="0">
<Resource name="jdbc/sqlserver" auth="Container" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/sqlserver">
<parameter>
<name>factory</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://MICROSOF-BJER9M:1433;DatabaseName=basesql</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>12345</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>20</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>10</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>-1</value>
</parameter>
</ResourceParams>
</Context>


然后conpool.jsp的代码如下:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="javax.naming.*"%>
<%@ page import="java.sql.*"%>
<%
Context initCtx = new InitialContext();
Context ctx=(Context) initCtx.lookup("java:comp/env");
Object obj=(Object) ctx.lookup("jdbc/sqlserver");
javax.sql.DataSource ds=(javax.sql.DataSource)obj;
Connection conn=ds.getConnection();
Statement stmt=conn.createStatement();
String strSql="select * from login";
ResultSet rs=stmt.executeQuery(strSql);

out.print("<table Border>");
out.print("<tr><td colspan=2 align=center>用户数据</td></tr>");
out.print("<tr>");
out.print("<td width=100>"+"用户姓名");
out.print("<td width=100>"+"用户密码");
out.print("</tr>");
while(rs.next())
{
out.print("<tr>");
out.print("<td>"+rs.getString(1)+"</td>");
out.print("<td>"+rs.getString(2)+"</td>");
out.print("</tr>");
}
out.print("</table>");

%>


出错如下:

javax.servlet.ServletException: Could not create resource factory instance, null
org.apache.jasper.runtime.PageContextImpl.doHandlePageException

(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException

(PageContextImpl.java:758)
org.apache.jsp.conpool_jsp._jspService(conpool_jsp.java:89)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

问一下到底那里错了,
需要注明一下的是,我通过JDBC的方式成功连接,即用 con=DriverManager.getConnection("jdbc:microsoft:sqlserver://MICROSOF-BJER9M:1433;DatabaseName=basesql","sa","12345");
已经连接成功

但是在配置地址池的时候就出现了上面的错误,请问到底那里错了


[此贴子已经被作者于2006-8-4 15:35:07编辑过]

搜索更多相关主题的帖子: 数据库连接池  name  parameter  STRONG  

----------------解决方案--------------------------------------------------------
不是很懂这个,不过我觉得你驱动有问题,你用的是sql数据库,你的驱动却是ORACLE的
----------------解决方案--------------------------------------------------------
我搞定了,哈哈,高兴呀,看了N多资料,在tomcat的官方网站上找到了解决方案:
把上面我写的这几句
<parameter>
<name>factory</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>oracle.jdbc.driver.OracleDriver</value>
</parameter>
改为下面这样:

<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
</parameter>
----------------解决方案--------------------------------------------------------

我下面是用mysql数据库做的数据源及测试代码!你可以参考一下:

----------------------------------------------------------------------------

<Context docBase="bookstore" path="/bookstore" reloadable="true">
<Resource auth="Container" name="jdbc/BookDB" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/BookDB">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/bookdb?autoReconnect=true</value>
</parameter>
<parameter>
<name>password</name>
<value>1234</value>
</parameter>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>
<parameter>
<name>username</name>
<value>dbuser</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
</ResourceParams>
</Context>

--------------------------------------------------------------------
test.jsp


<!--首先导入一些必要的packages-->
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="com.mysql.jdbc.Connection"%>
<!--设置中文输出-->
<%@ page contentType="text/html; charset=GB2312" %>
<html>
<head>
<title>DbJsp1.jsp</title>
</head>
<body>
<%
//以try开始
try
{
java.sql.Connection con;
Statement stmt;
ResultSet rs;

//建立数据库连接
Context ctx = new InitialContext();
DataSource ds =(DataSource)ctx.lookup("java:comp/env/jdbc/BookDB");
con = ds.getConnection();
//创建一个JDBC声明
stmt = con.createStatement();
//增加新记录
stmt.executeUpdate("INSERT INTO books (id,name,title,price) VALUES ('999','Tom','Tomcat Bible',44.5)");
//查询记录
rs = stmt.executeQuery("SELECT id,name,title,price from books");
//输出查询结果
out.println("<table border=1 width=400>");
while (rs.next())
{
String col1 = rs.getString(1);
String col2 = rs.getString(2);
String col3 = rs.getString(3);
float col4 = rs.getFloat(4);

//convert character encoding
col1=new String(col1.getBytes("ISO-8859-1"),"GB2312");
col2=new String(col2.getBytes("ISO-8859-1"),"GB2312");
col3=new String(col3.getBytes("ISO-8859-1"),"GB2312");

//打印所显示的数据
out.println("<tr><td>"+col1+"</td><td>"+col2+"</td><td>"+col3+"</td><td>"+col4+"</td></tr>");
}
out.println("</table>");

//删除新增加的记录
stmt.executeUpdate("DELETE FROM books WHERE id='999'");

//关闭数据库连结
rs.close();
stmt.close();
con.close();
}

//捕获错误信息
catch (Exception e) {out.println(e.getMessage());}

%>
</body>
</html>

--------------------------------------------------------------



----------------解决方案--------------------------------------------------------
  相关解决方案