JNDI InitialContext在简单的netbeans项目中不起作用 [英] JNDI InitialContext not working in simple netbeans project

查看:196
本文介绍了JNDI InitialContext在简单的netbeans项目中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:Java的新手

我有一个简单的Netbeans项目-我只想学习与来自php的数据库交互的知识,我想我可以与运行在计算机上的本地数据库一起使用.

I have a simple Netbeans project - I wanted to just learn about interacting with DB's coming from php I thought I would have a go with a local one running on my computer.

许多示例都说明要使用InitialContext()对象来引用数据库资源.

Lots of the examples out there say to use the InitialContext() object to refer to the database resource.

遵循示例之后,我得到以下异常-许多Google内容指向某个.xml文件-我不知道该文件,甚至不知道它在Netbeans项目中的位置吗?我目前不使用Web服务器,因此不使用Tomcat或类似的工具,而仅使用本地Java程序来执行此操作,我怀疑这可能是问题所在.有人能对此有所启发吗?

After following the examples I get the following exception - Lots of Google stuff points to some .xml file - which I have no idea about or even where it exists in the Netbeans project? I'm not using a Webserver at this time so not Tomcat or anything like that, just local Java program to do this, I suspect this might be the problem. Could anyone shed some light on this?

Exception thrown javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.


package learningjava;
import com.mysql.jdbc.jdbc2.optional.*;
import com.mysql.jdbc.Driver;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.*;



public class LearningJava {

    public static void main(String[] args) {

       MysqlDataSource test_db = new MysqlDataSource(); 

       test_db.setServerName("localhost");
       test_db.setDatabaseName("dev");

       try {

            InitialContext test_db_context = new InitialContext();
            test_db_context.bind("jcdb/testdb", test_db);
            MysqlDataSource test_db_datasource = (MysqlDataSource)test_db_context.lookup("testdb");

       } catch (NamingException e) {

           System.out.println("Exception thrown " + e);

       }
        try {

            test_db.getConnection("root","password");

        } catch (SQLException e) {

             System.out.println("Exception thrown " + e);

        }


    }    

}

推荐答案

通常,您应该了解JNDI应该具有服务器.在您提供的代码段中,当您进行查找时,您正在使用JNDI技术的_CLIENT_SIDE_部分.应该有一个可以从本地客户端连接访问的JNDI服务器. 一旦正确配置,对查询的调用应发出与JNDI服务器的连接,并提供一种获取/绑定该服务器资源的方法.

In general you should understand that JNDI should have a server. In a code snippet you've provided you're using a _CLIENT_SIDE_ part of JNDI technology when you're doing your lookup. There should be a JNDI server that should be accessible from your local client connection. Once configured properly, the call to lookup should issue a connection with JNDI server and provide a way to obtain/bind resources to that server.

如何正确配置JNDI?

How to configure JNDI properly?

通常,您应该提供一个属性文件,其中将包含该服务器的主机名,端口和一些实现特定的信息.

Usually you should supply a properties file that will contain a host name of this server, a port + some implementation specific information.

在使用应用程序服务器(例如JBoss或Web Sphere)时,通常已经提供了JNDI服务器. 我认为这是误会的根源.

JNDI server is usually already provided when you're using application server (like JBoss or Web Sphere). I think this is the root of misunderstanding here.

希望,这会有所帮助

这篇关于JNDI InitialContext在简单的netbeans项目中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆