简体   繁体   English

JNDI InitialContext在简单的netbeans项目中不起作用

[英]JNDI InitialContext not working in simple netbeans project

Warning: New to Java 警告:Java新手

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. 我有一个简单的Netbeans项目-我只想学习与来自php的数据库交互的知识,我想我可以尝试在计算机上运行本地程序。

Lots of the examples out there say to use the InitialContext() object to refer to the database resource. 那里的许多示例都说要使用InitialContext()对象来引用数据库资源。

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

        }


    }    

}

could you try to add this before the InitialContext test_db_context = new InitialContext(); 您可以尝试在InitialContext test_db_context = new InitialContext();之前添加它InitialContext test_db_context = new InitialContext(); :

System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

You should include the jar naming-common-4.1.34 and mysql-connector-java-5.1.6 in your classpath 您应该在类路径中包含jar naming-common-4.1.34和mysql-connector-java-5.1.6

This example works for me (not optimized but works!) 此示例对我有用(未优化,但是有效!)

public static void main(String[] args) throws NamingException {
        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

        InitialContext ic = new InitialContext();
        ic.createSubcontext("java:");
        ic.createSubcontext("java:comp");
        ic.createSubcontext("java:comp/env");
        ic.createSubcontext("java:comp/env/jdbc");

         MysqlConnectionPoolDataSource mysqlConnectionPoolDataSource = new MysqlConnectionPoolDataSource();
         mysqlConnectionPoolDataSource.setUser("root");
         mysqlConnectionPoolDataSource.setPassword("root");
         mysqlConnectionPoolDataSource.setURL("jdbc:mysql://localhost:3306/test_my_database");

         ic.bind("java:comp/env/jdbc/test", mysqlConnectionPoolDataSource);

    }

In general you should understand that JNDI should have a server. 通常,您应该了解JNDI应该具有服务器。 In a code snippet you've provided you're using a _CLIENT_SIDE_ part of JNDI technology when you're doing your lookup. 在您提供的代码段中,当您进行查找时,您正在使用JNDI技术的_CLIENT_SIDE_部分。 There should be a JNDI server that should be accessible from your local client connection. 应该有一个可以从本地客户端连接访问的JNDI服务器。 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? 如何正确配置JNDI?

Usually you should supply a properties file that will contain a host name of this server, a port + some implementation specific information. 通常,您应该提供一个属性文件,其中将包含此服务器的主机名,端口和一些特定于实现的信息。

JNDI server is usually already provided when you're using application server (like JBoss or Web Sphere). 使用应用程序服务器(例如JBoss或Web Sphere)时,通常已经提供了JNDI服务器。 I think this is the root of misunderstanding here. 我认为这是误会的根源。

Hope, this helps 希望这可以帮助

Usually JNDI is used inside an application server which is not your case. 通常情况下,JNDI在应用服务器内部使用,而不是这种情况。

For your needs you may use the following code: 为了您的需要,您可以使用以下代码:

Class.forName("com.mysql.jdbc.Driver")
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dbname", "user", "password");

Also you need to download MySQL driver here and add corresponding JAR file (mysql-connector-java-5.1.21-bin.jar) to your application's class path here is described how to do it . 另外,您还需要在此处下载MySQL驱动程序,并将相应的JAR文件(mysql-connector-java-5.1.21-bin.jar)添加到应用程序的类路径中, 此处介绍了如何执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM