信息技术

当前位置: 主页 > 编程语言 > JAVA >

jndi配置问题 - Java / Web 开发

时间:2010-07-09 14:27来源:未知 作者:admin 点击:
简介:这是jndi配置问题 - Java / Web 开发的详细页面,介绍了和java,jndi配置问题 - Java / Web 开发有关的知识,加入收藏请按键盘ctrl+D,谢谢大家的观看!要查看更多有关信息, 请点击此处 配置jnd

简介:这是jndi配置问题 - Java / Web 开发的详细页面,介绍了和java,jndi配置问题 - Java / Web 开发有关的知识,加入收藏请按键盘ctrl+D,谢谢大家的观看!要查看更多有关信息,请点击此处

配置jndi都需要什么啊,哪位朋友可以帮我一下啊
我使用的是tomcat6
我是mysql数据库 库名为:jx0000 用户名:user 密码:password
meta-inf下创建context.xml文件,内容如下:
<Context debug="0"> 
  <Resource name="jdbc/jx0000" auth="Container" 
  type="javax.sql.DataSource" 
  driverClassName="com.mysql.jdbc.Driver" 
  url="jdbc:mysql://localhost:3306/jx0000?autoReconnect=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;mysqlEncoding=utf8" 
  username="user" 
  password="password" 
  maxActive="49" 
  maxIdle="3" 
  removeAbandoned="true" 
  maxWait="3000" /> 
</Context> 
----------------------------------------
连接程序:
package dhtmlxconnector;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.*;
import javax.sql.DataSource;

public class DataBaseConnection {
Connection getConnection(){
InitialContext ctx=null;
DataSource ds=null;
try {
ctx = new InitialContext();
ds = (DataSource)ctx.lookup("java:comp/env bc/jx0000");
} catch (NamingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("初始化连接失败");
}
 
Connection conn=null;
try {
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
//conn = DriverManager.getConnection("jdbc:mysql://localhost/jx0000?characterEncoding=UTF-8", "user", "password");
conn=ds.getConnection();
} catch (Throwable e) {
e.printStackTrace();
System.out.println("获取连接失败");
}
return conn;
}
}

测试后显示错误:获取连接失败

请问什么原因啊,如何解决?谢谢
 
回答 1 



------其他回答(20分)---------

在项目的web.xml的根节点下添加以下内容吧:

XML code
<resource-ref> <res-ref-name>jdbc/jx0000</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> <res-sharing-scope>Shareable</res-sharing-scope> </resource-ref>



代码中这样写应该就可以了

Java code
javax.naming.InitialContext ctx = new javax.naming.InitialContext(); DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/jx0000"); Connection conn = ds.getConnection();

------其他回答(20分)---------

在\WebRoot\META-INF下添加一个context.xml
内容为:
<Context>
<Resource name="jdbc/wdpc" auth="Container"
type="javax.sql.DataSource" maxActive="100" maxIdle="30"
maxWait="10000" username="sa" password="wdpc"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=wdpc" />
</Context>
在Tomcat的lib目录下添加数据库驱动包,自己改成MySQL的
取Connection代码 :
public class DbManager {
public static Connection getConnection() {
Connection conn = null;
try {
Context context = new InitialContext();
DataSource ds = (DataSource) context
.lookup("java:comp/env/jdbc/wdpc");
conn = ds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;// 返回连接对象
}

public static void closeConnection(Connection conn, Statement pst,
ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
------其他回答(40分)---------

在项目的web.xml的根节点下添加以下内容吧:

XML code
  <resource-ref>
  <res-ref-name>jdbc/jx0000</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
  <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>




代码中这样写应该就可以了

Java code
  javax.naming.InitialContext ctx = new javax.naming.InitialContext();

------其他回答(10分)---------

引用 1 楼 jzshmyt 的回复:
在项目的web.xml的根节点下添加以下内容吧:


XML code

<resource-ref>
<res-ref-name>jdbc/jx0000</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth……


LZ试试,这种方法应该可以!
------其他回答(10分)---------

将ds = (DataSource)ctx.lookup("java:comp/env bc/jx0000");这句改成ds = (DataSource)ctx.lookup("java:comp/env/bc/jx0000");就可以了
(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片