Can I access and use the IDEA Data Sources?

Creating a plugin that will connect to a DB and do stuff (read data only).

Is there a way to use data sources defined in IntelliJ? Easier than writing my own data source and connection management stuff.

Thanks,

Happy

14 comments
Comment actions Permalink

Hello Martin,

Creating a plugin that will connect to a DB and do stuff (read data
only).

Is there a way to use data sources defined in IntelliJ? Easier than
writing my own data source and connection management stuff.


Yes, you can use that. That functionality is part of the Java EE plugin,
so you'll need to depend on that plugin and reference javaee-openapi.jar
in your plugin. The starting point for this functionality is the DataSourceManager
class.

If you have more specific questions, feel free to ask them here.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

I actually have a small plugin that I wrote for 6, and I used the DataSourceManager.getInstance(project) method, but it now returns null. I went through the latest API but there is no mention of any changes. There was a JIRA post but the user commented the following:

"Sorry, my mistake! I should make plugin extend com.intellij.javaee, and use version 2."

But that isn't very helpful. Is there something special that needs to be done to access the DataSourceManager instance?

0
Comment actions Permalink

Hello James,

I actually have a small plugin that I wrote for 6, and I used the
DataSourceManager.getInstance(project) method, but it now returns
null. I went through the latest API but there is no mention of any
changes. There was a JIRA post but the user commented the following:

"Sorry, my mistake! I should make plugin extend com.intellij.javaee,
and use version 2."

But that isn't very helpful. Is there something special that needs to
be done to access the DataSourceManager instance?


Add com.intellij.javaee]]> to your plugin.xml.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Thanks Dmitry. It didn't seem to make a difference. I still get a null on the DataSourceManager.getInstance(project) method.

0
Comment actions Permalink

Hello James,

I still get a null on the DataSourceManager.getInstance(project) method.


You probably added javaee-openapi.jar to your plugin dependencies. You should add it to the IDEA-JDK
instead. Also make sure the jar is not present any more in the plugin's lib directory in the sandbox.

Sascha

0
Comment actions Permalink

Thanks Sascha. That was it. When I was getting the null error, I thought that it was a dependency issue so I had added it in but I forgot to take it out.

Works like a charm now.

Thanks to both of you for your time.

0
Comment actions Permalink

Hi, sorry for my english, I speak spanish.
I have a similar problem, with idea 8.1.3:
I have the DatasourceManager.class in the IDEA_HOME\plugins\DatabaseSupport\lib\database-openapi.jar file. If I add this jar to the plugin dependencies, I get the NPE. But If I remove this file from the dependencies, and adds it to the IDEA-JDK, I get:

java.lang.NoClassDefFoundError: com/intellij/javaee/dataSource/DataSourceManager
at com.intellij.openapi.samples.HelloWorldAction.actionPerformed(HelloWorldAction.java:33)
  at com.intellij.openapi.actionSystem.impl.ActionMenuItem$ActionTransmitter.actionPerformed(ActionMenuItem.java:17)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
....


Thanks.

0
Comment actions Permalink

Hi, thank you very much for the information, I could instanciate the DataSourceManager and get the DataSource I needed, but know I have a little Problem:
-The DataSource only has the method 'performJdbcOperation' to actualy perfom an datasource-action, which needs a ServerInstance as one paramter.

How can I aquire a ServerInstance to use for a query? -Or is there a simpler way to do this?

Thanks in advance,
Chris

0
Comment actions Permalink

Hi i did it this way but it's awfull:

 
DataSourceManager dataSourceManager = DataSourceManager.getInstance(e.getProject());
DataSource dataSource = dataSourceManager.getDataSourceByName("BB21_TST"); 
Method getConnectionMethod = dataSource.getClass().getDeclaredMethod("getConnection", Project.class, ServerInstance.class);
getConnectionMethod.setAccessible(true);
Connection conn = (Connection) getConnectionMethod.invoke(dataSource, p, null);



Works on Idea 12

0
Comment actions Permalink

Hi Daniel,

Why do you want to get a Connection object instead of using DataSource.performJdbcOperation()?

Denis

0
Comment actions Permalink

Because I'm using third party library which need's connection, i would make my own connection but I can't find LocalDataSource class any more, for obtaining driver and url.

0
Comment actions Permalink

Just checked that LocalDataSource class is available at the database-impl.jar under $IDEA_HOME/plugins/DatabaseSupport/lib

Denis

0
Comment actions Permalink

That's it, i was missing database-impl.jar in my sdk, didn't noticed its not in database-openapi.jar.
Thank you

0
Comment actions Permalink

In order to simplify this a

public DataSource.getConnection(Project)

method is added to IDEA 13 and IDEA 12.1.X codebases.

0

Please sign in to leave a comment.