高分求resin+mssql数据库连接池代码(可以用resin自带的和配置
我已经作了以下尝试:
在resin的resin.conf中<database>处作了修改
<database>
<jndi-name>jdbc/mysql</jndi-name>
<driver type="com.microsoft.jdbc.sqlserver.SQLServerDriver">
<url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=wuye</url>
<user>sa</user>
<password>china</password>
</driver>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
然后写了一个servlet TestDatabase.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.sql.*;
public class TestDatabase extends HttpServlet {
private final static String DATASOURCE_NAME = "jdbc/mysql";
DataSource _pool;
public void init()
throws ServletException
{
try {
Context env = (Context) new InitialContext().lookup("java:comp/env");
_pool = (DataSource) env.lookup("DATASOURCE_NAME");
if (_pool == null)
throw new ServletException("`" + DATASOURCE_NAME + "' is an unknown DataSource");
} catch (NamingException e) {
throw new ServletException(e);
}
}
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection conn = null;
try {
conn = _pool.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from room");
out.println("Brooms:<br>");
while (rs.next()) {
out.print(rs.getString(1));
out.print(" ");
out.print(rs.getInt(2));
out.println("<br>");
}
rs.close();
stmt.close();
} catch (SQLException e) {
throw new ServletException(e);
} finally {
try {
if (conn != null)
conn.close();
} catch (SQLException e) {
}
}
}
}
运行时报错提示
javax.servlet.ServletException: `jdbc/mysql' is an unknown DataSource
at TestDatabase.init(TestDatabase.java:22)
at javax.servlet.GenericServlet.init(GenericServlet.java:82)
at com.caucho.server.dispatch.ServletConfigImpl.createServlet(ServletConfigImpl.java:475)
at com.caucho.server.dispatch.ServletFilterChain.doFilter(ServletFilterChain.java:96)
at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:207)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:168)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:221)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:253)
at com.caucho.server.port.WorkerThread.run(WorkerThread.java:127)
at java.lang.Thread.run(Thread.java:534)
不知道什末原因啊
用<dbpool.sql>好像不行啊 应该加在那个位置啊
第2种方法
用论坛上写的连接池代码(http://www.chinabs.net/jsp/default.asp?infoid=305)
属性 文件改为
#news.txt
driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
mysql.url=jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=wuye
mysql.user=sa
mysql.password=china
mysql.maxconn=1000
logfile=C:\\ilog.txt
也不行啊,这个还需要在resin.conf中配置吗
请大家不吝赐教,能帮我搞成功的话给高分,先谢谢了
问题点数:100、回复次数:10Top
1 楼xiehoo(解狐)回复于 2003-12-02 08:20:37 得分 0
大家帮帮忙啊,给点建议啊Top
2 楼favourl(往事如烟)回复于 2003-12-02 08:31:13 得分 0
http://www.chinajavaworld.com/bbsoffline/jinghuaforum2/5137.html
也许会有帮助,连接池在csdn也很多,自己收一下Top
3 楼netwebs(Only Java)回复于 2003-12-02 10:32:01 得分 100
一、resin.conf
<database>
<jndi-name>jdbc/mysql</jndi-name>
<driver type="com.microsoft.jdbc.sqlserver.SQLServerDriver">
<url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=wuye</url>
<user>sa</user>
<password>china</password>
</driver>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
二、你的程序中WEB-INF\web.xml文件加以下内容
<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>
三、public Connection getConnection() {
try {
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/mysql");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();
System.out.println("数据库连接正常(连接池)!");
}
catch (Exception ex) {
ex.printStackTrace();
System.out.println("数据库无法连接(连接池)!");
conn = null;
}
return conn ;
}
四、接分Top
4 楼xiehoo(解狐)回复于 2003-12-02 13:11:32 得分 0
还不行啊 resin中web-inf下面没有web.xml,我拷了一个tamcat下的
但还是不行提示:
javax.naming.NoInitialContextException: Need to specify class name in environmen
t or system property, or as an applet parameter, or in an application resource f
ile: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
40)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243
)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.jav
a:280)
at javax.naming.InitialContext.lookup(InitialContext.java:347)
at test.getConnection(test.java:12)
at test.main(test.java:30)
数据库无法连接(连接池)!
Top
5 楼flylyke(爱就像英雄莫问出处)回复于 2003-12-02 17:18:11 得分 0
怎么会没有web.xml呢?没有的话自己写一个把楼上的那段贴上,注意格式Top
6 楼netwebs(Only Java)回复于 2003-12-03 11:12:05 得分 0
我说了是在你的程序中的WEB-INF目录先有个web.xml文件,这是我的,你看一下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>debugjsp</servlet-name>
<description>Added to compile JSPs with debug info</description>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<init-param>
<param-name>classdebuginfo</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/MysqlDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Top
7 楼xiehoo(解狐)回复于 2003-12-04 21:54:24 得分 0
首先向 netwebs(Only Java)等表示感谢!!!
又试了一遍,还不行啊,出错提示
javax.naming.NameNotFoundException: java:comp/env/jdbc/mssql
at com.caucho.naming.ContextImpl.lookupImpl(ContextImpl.java:214)
at com.caucho.naming.ContextImpl.lookup(ContextImpl.java:172)
at _wuye._test1__jsp._jspService(/wuye/test1.jsp:15)
at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
at com.caucho.jsp.Page.pageservice(Page.java:551)
at com.caucho.server.dispatch.PageFilterChain.doFilter(PageFilterChain.java:184)
at com.caucho.server.cache.CacheFilterChain.doFilter(CacheFilterChain.java:207)
at com.caucho.server.webapp.WebAppFilterChain.doFilter(WebAppFilterChain.java:168)
at com.caucho.server.dispatch.ServletInvocation.service(ServletInvocation.java:221)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:253)
at com.caucho.server.port.WorkerThread.run(WorkerThread.java:127)
at java.lang.Thread.run(Thread.java:534)
我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mssql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
resin.conf
...
<database>
<jndi-name>jdbc/mssql</jndi-name>
<driver type="com.microsoft.jdbc.sqlserver.SQLServerDriver">
<url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=wuye</url>
<user>sa</user>
<password>china</password>
</driver>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
...
测试jsp文件
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.naming.*"%>
<%@ page import="javax.sql.DataSource"%>
<%Connection conn=null;
try {
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
Object obj = (Object) ctx.lookup("jdbc/mssql");
//类型转换
javax.sql.DataSource ds = (javax.sql.DataSource) obj;
conn = ds.getConnection();
out.println("数据库连接正常(连接池)!");
}
catch (Exception ex) {
ex.printStackTrace();
out.println("数据库无法连接(连接池)!"+ex.getMessage());
conn = null;
}
%>
请再指点一下,多谢了阿,不甚感激Top
8 楼zdhsoft(冬瓜猫)回复于 2003-12-05 23:55:02 得分 0
Context ctx = (Context) initCtx.lookup("java:comp/jdbc/mssql");我记得好是这样用的Top
9 楼netwebs(Only Java)回复于 2003-12-10 13:40:46 得分 0
不好意思,这两天没看~~估计是你的resin.conf的配置问题。找到这段话(resin.conf自带的):
<!--
- Sample database pool configuration
-
- The JDBC name is java:comp/env/jdbc/test
-
<database>
<jndi-name>jdbc/mysql</jndi-name>
<driver type="org.gjt.mm.mysql.Driver">
<url>jdbc:mysql://localhost:3306/test</url>
<user></user>
<password></password>
</driver>
<prepared-statement-cache-size>8</prepared-statement-cache-size>
<max-connections>20</max-connections>
<max-idle-time>30s</max-idle-time>
</database>
-->
把你的配置就写在这段话后面,不要写在 <!-- 和 --> 的里面。
Top
10 楼xiehoo(解狐)回复于 2003-12-10 16:16:40 得分 0
谢谢netwebs(Only Java大侠的帮助,我现在已经能连上了
再次表示感谢Top




