使用IntelliJ中的DataSource,JNDI API [英] Working with DataSource, JNDI API in IntelliJ

查看:168
本文介绍了使用IntelliJ中的DataSource,JNDI API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解使用 DataSource对象 JNDI API 建立数据库连接。 / p>

我正在使用Intellij UE,并且具有本地 Tomcat-8 -和 Postgres -服务器正在运行。



我按照Oracle Java文档中的说明进行操作:


  1. 创建 DataSource类的实例并设置其属性

      org.postgresql.ds.PGSimpleDataSource dataSource =新的org.postgresql.ds.PGSimpleDataSource(); 
    dataSource.setServerName( localhost);
    dataSource.setDatabaseName( db01);
    dataSource.setUser( jwi);
    dataSource.setPassword( password);


  2. 使用命名方式注册 DataSource对象使用 JNDI API

    的服务

      Context ctx = null; 

    试试{
    ctx = new InitialContext();
    ctx.bind( jdbc / localDB,dataSource);
    } catch(NamingException e){
    e.printStackTrace();
    }


Oracle文档说:


设置属性后,系统管理员可以使用JNDI(Java命名和目录接口)命名服务注册BasicDataSource对象。 / p>

所以我的第一个问题是:注册 DataSource 是什么意思?我的代码是否已经将 DataSource对象注册到 JNDI


  1. 使用已部署的 DataSource对象

      try {

    Context ctx = new InitialContext();
    DataSource ds =(DataSource)ctx.lookup( jdbc / localDB);
    dbCon = ds.getConnection();
    ...


在此代码切割IntelliJ始终声称无法解析方法 getConnection()



Oracle文档说:


在系统管理员部署了基本的DataSource实现之后,程序员就可以使用它了。


所以我的第二个问题是:在这种情况下,确切地意味着部署了什么?创建 DataSource实例并使用 JDNI 执行注册?还是部署意味着 Tomcat context.xml web.xml 配置( Tomcat 8 JNDI操作指南)?



如果有人对此问题有很好的逐步指导,我将非常感激,实际上,Oracle文档并不清楚某些方面

解决方案

对于第二个问题,部署意味着您的数据源在tomcat的context.xml中声明。
这是一个oracle数据库的示例(您必须更改postgres的驱动程序):

 <资源name = jdbc / myoracle auth =容器 
type = javax.sql.DataSource driverClassName = oracle.jdbc.OracleDriver
url = jdbc:oracle:thin:@ 127.0。 0.1:1521:mysid
username = scott password = tiger maxTotal = 20 maxIdle = 10
maxWaitMillis =-1 />

之后,您可以编写Java部分,为此您可以观看此链接 http://www.javapractices.com/topic/TopicAction.do?Id=127



有关完整示例,这里有一个很好的教程 http://alvinalexander.com/blog/post/java/how-configure-tomcat-dbcp-connection-pool-pooling-postgres



希望获得帮助


I'm trying to understand establishing a database-connection with a DataSource Object and the JNDI API.

I'm working with Intellij UE and have a local Tomcat-8- and Postgres-Server running.

I proceed as mentioned in the Oracle Java Documentation:

  1. Creating Instance of DataSource Class and Setting its Properties

    org.postgresql.ds.PGSimpleDataSource dataSource = new org.postgresql.ds.PGSimpleDataSource();
    dataSource.setServerName("localhost");
    dataSource.setDatabaseName("db01");
    dataSource.setUser("jwi");
    dataSource.setPassword("password");
    

  2. Registering DataSource Object with Naming Service That Uses JNDI API

    Context ctx = null;
    
    try {
        ctx = new InitialContext();
        ctx.bind("jdbc/localDB", dataSource);
    } catch (NamingException e) {
        e.printStackTrace();
    }
    

The Oracle Documentation says:

With the properties set, the system administrator can register the BasicDataSource object with a JNDI (Java Naming and Directory Interface) naming service.

So my first Question is: What means to register a DataSource? Is my code obove already the registration of an DataSource Object to JNDI?

  1. Using Deployed DataSource Object

    try {
    
        Context ctx = new InitialContext();
        DataSource ds = (DataSource) ctx.lookup("jdbc/localDB");
        dbCon = ds.getConnection();
        ...
    

In this code cutting IntelliJ always claims, that it can't resolve the method getConnection().

The Oracle Documentation says:

After a basic DataSource implementation is deployed by a system administrator, it is ready for a programmer to use.

So my second Question is: What exactly means deployed in this case? Creating a DataSource Instance and execute the registration with JDNI? Or does deployed mean the Tomcat context.xml and web.xml configuration (Tomcat 8 JNDI How-To)?

I'd really appreciate if anybody has a good step by step instruction for this issue, in fact that the Oracle Documentation isn't really clear about some points imho.

解决方案

for the second question, deployed means that your datasource is declared in the context.xml in tomcat. Here is an example of an oracle database (you have to change the driver for postgres) :

<Resource name="jdbc/myoracle" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@127.0.0.1:1521:mysid"
              username="scott" password="tiger" maxTotal="20" maxIdle="10"
              maxWaitMillis="-1"/>

After that, you can code the java part, for that you can watch this link http://www.javapractices.com/topic/TopicAction.do?Id=127

For a complete example, there's a good tutorial here http://alvinalexander.com/blog/post/java/how-configure-tomcat-dbcp-connection-pool-pooling-postgres.

Hope this help

这篇关于使用IntelliJ中的DataSource,JNDI API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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