Posted by:returner
Posted on:2004-11-09 22:42
tomcat5配置jndi的问题;斑竹高手们来看看啊,情况紧急!!!!!!!!10万分感谢
这个问题我是搜索出来的,我也遇到了同样的问题,希望高手给于关注:
http://www.cjsdn.net/post/view?bid=7&id=95089&tpg=1&ppg=1&sty=1&age=0#95089
本人的环境是Eclipse2.1.2+tomcat5.0+lomboz+mysql
$tomcat_home$/comm/lib下加入jndi.jar,mysql+jdbc驱动。
在tomcat的Sever.xml中的〈/Engine>前加入以下内容:
factory
org.apache.commons.dbcp.BasicDataSourceFactory
url
jdbc:mysql://localhost:3306/shop
driverclassname
com.mysql.jdbc.Driver
username
root
password
root
maxWait
3000
maxIdle
10
maxActive
100
在Webapps的Web.xml中加入以下内容:
connectDB test
jdbc/hibernate
javax.sql.DataSource
Container
测试代码如下:
package com.yzs.test.hibernate;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.*;
import java.sql.*;
/**
* @author yzs
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class testservlet extends HttpServlet {
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//TODO Method stub generated by Lomboz
Context initctx=null;
Context ctx=null;
DataSource ds=null;
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
response.setContentType("text/html");
PrintWriter out=response.getWriter();
try {
initctx=new InitialContext();
ctx= (Context) initctx.lookup("jdbc:comp/env");
ds=(DataSource)ctx.lookup("jdbc/hibernate");
if(ds==null) throw new NamingException("no database");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn=ds.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from brand");
rs.next();
out.println("");
out.println("
out.println("
test jndi");out.println("");
out.println("
");out.println(rs.getString("name"));
} catch (SQLException e4) {
// TODO Auto-generated catch block
e4.printStackTrace();
}
}
}
出现了Name jdbc:comp is not bound in this Context的问题,请各位大侠
帮忙看一下问题出在哪里。