Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » DTP » Programmatically access a JDBC connection via DTP
Programmatically access a JDBC connection via DTP [message #488921] Wed, 30 September 2009 17:24 Go to next message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Is there any external documentation/example showing how to programmatically access a JDBC connection via DTP and create/populate tables via DTP. I am trying to develop a plugins which requires JDBC connection to persistent some of the data in database.

Thanks.

Joshua

[Updated on: Wed, 30 September 2009 17:24]

Report message to a moderator

Re: Programmatically access a JDBC connection via DTP [message #488939 is a reply to message #488921] Wed, 30 September 2009 19:14 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Hi Joshua...

What exactly are you trying to do?

Do you have a JDBC connection instance and want to use it in DTP? Or do
you have connection details that you want to leverage to create the
applicable DTP connections so you can get to the underlying database model?

More information about your goal will help me tailor an example for you.

Thanks
--Fitz

Joshua Hui wrote:
> Is there any external documentation/example showing how to
> programmatically access a JDBC connection via DTP and create/populate
> tables via DTP. I am trying to develop a plugins which requires JDBC
> connection to persistent some of the data in database.
>
> Thanks.
>
> Joshua
Re: Programmatically access a JDBC connection via DTP [message #488949 is a reply to message #488939] Wed, 30 September 2009 20:00 Go to previous messageGo to next message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

Instead of directly using java.sql to establish a JDBC connection and manipulate the tables in the database, I would like to leverage DTP to use existing connection profile that is configured in the project and issue SQL statement. It is like the latter answer in your question.

Thanks.

Joshua

Re: Programmatically access a JDBC connection via DTP [message #488967 is a reply to message #488949] Wed, 30 September 2009 22:49 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
I'll work up an example over the next couple of days. Sorry for the delay.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> Instead of directly using java.sql to establish a JDBC connection and
> manipulate the tables in the database, I would like to leverage DTP to
> use existing connection profile that is configured in the project and
> issue SQL statement. It is like the latter answer in your question.
>
> Thanks.
>
> Joshua
>
>
Re: Programmatically access a JDBC connection via DTP [message #489165 is a reply to message #488967] Thu, 01 October 2009 16:31 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Ok...

So to get a named connection profile and connect via APIs, you use:

IConnectionProfile profile =
ProfileManager.getInstance().getProfileByName("myprofile");
IStatus status = profile.connect();
if (status.equals(IStatus.OK)) {
// success
} else {
// failure :(
if (status.getException() != null) {
status.getException().printStackTrace();
}
}

And then once you have your IConnectionProfile reference, you can get
the JDBC connection this way:

public java.sql.Connection getJavaConnectionForProfile
(IConnectionProfile profile) {
IConnection connection =
ProfileConnectionManager.
getProfileConnectionManagerInstance().getConnection(profile,
"java.sql.Connection");
if (connection != null) {
return (java.sql.Connection) connection.getRawConnection();
}
return null;
}

From there you can treat it like any other JDBC connection and create a
JDBC statement and execute it how you'd like.

Does that help?

--Fitz

Brian Fitzpatrick wrote:
> I'll work up an example over the next couple of days. Sorry for the delay.
>
> --Fitz
>
> Joshua Hui wrote:
>> Hi Fitz,
>>
>> Instead of directly using java.sql to establish a JDBC connection and
>> manipulate the tables in the database, I would like to leverage DTP to
>> use existing connection profile that is configured in the project and
>> issue SQL statement. It is like the latter answer in your question.
>>
>> Thanks.
>>
>> Joshua
>>
>>
Re: Programmatically access a JDBC connection via DTP [message #489169 is a reply to message #489165] Thu, 01 October 2009 16:54 Go to previous messageGo to next message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

Thanks. It is really useful. Really appreciate that.

BTW, apart from javadoc, is there any external document/web site which describes how to use the APIs? In that case, if I have any further question, I can refer to that.

Joshua
Re: Programmatically access a JDBC connection via DTP [message #489173 is a reply to message #489169] Thu, 01 October 2009 17:00 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
There really isn't, or wasn't until I created one this morning for FAQs.
You can see it here: http://wiki.eclipse.org/DTP_FAQ

What exactly are you looking for? I have a number of things I'll add to
that page over time.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> Thanks. It is really useful. Really appreciate that.
>
> BTW, apart from javadoc, is there any external document/web site which
> describes how to use the APIs? In that case, if I have any further
> question, I can refer to that.
>
> Joshua
Re: Programmatically access a JDBC connection via DTP [message #489216 is a reply to message #489173] Thu, 01 October 2009 21:54 Go to previous messageGo to next message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

Apart from using the raw JDBC connection, I am also looking for ways to programmatically invoke a ddl script from DTP. Is there a way to do it?

BTW, I tried your suggestion. It works except for
public java.sql.Connection getJavaConnectionForProfile( IConnectionProfile profile)

I have to change the following to get hold of the sql connection.

IManagedConnection connection = profile.getManagedConnection("java.sql.Connection");

if (connection != null) {
return (java.sql.Connection) connection.getConnection().getRawConnection();
}
return null;

Thanks.

Joshua
Re: Programmatically access a JDBC connection via DTP [message #489352 is a reply to message #489216] Fri, 02 October 2009 14:49 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Hi Joshua...

Thanks for the correction - I'll update that code in the FAQ too. :)

Let me ask around and see if we have an example of how to use the APIs
to invoke some DDL.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> Apart from using the raw JDBC connection, I am also looking for ways to
> programmatically invoke a ddl script from DTP. Is there a way to do it?
>
> BTW, I tried your suggestion. It works except for public
> java.sql.Connection getJavaConnectionForProfile(
> IConnectionProfile profile)
>
> I have to change the following to get hold of the sql connection.
>
> IManagedConnection connection =
> profile.getManagedConnection("java.sql.Connection");
>
> if (connection != null) {
> return (java.sql.Connection)
> connection.getConnection().getRawConnection();
> }
> return null;
>
> Thanks.
>
> Joshua
Re: Programmatically access a JDBC connection via DTP [message #489424 is a reply to message #489352] Fri, 02 October 2009 22:33 Go to previous messageGo to next message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Hey Joshua...

Are you trying to execute DDL so that the results appear in the results
view or simply execute some DDL with the JDBC connection you got from a
connection profile?

--Fitz

Brian Fitzpatrick wrote:
> Hi Joshua...
>
> Thanks for the correction - I'll update that code in the FAQ too. :)
>
> Let me ask around and see if we have an example of how to use the APIs
> to invoke some DDL.
>
> --Fitz
>
> Joshua Hui wrote:
>> Hi Fitz,
>>
>> Apart from using the raw JDBC connection, I am also looking for ways
>> to programmatically invoke a ddl script from DTP. Is there a way to
>> do it?
>> ...
>> Thanks.
>>
>> Joshua
Re: Programmatically access a JDBC connection via DTP [message #489429 is a reply to message #488921] Fri, 02 October 2009 23:15 Go to previous messageGo to next message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

It will be the latter one which is to execute the DDL file using the IConnectionProfile.

Also, if not all the properties are specified (i.e. arePropertiesComplete() is false), can I bring up the same properties dialog to allow user to enter the valid connection information? If so, which command should I use?

Thanks for your help!

Joshua
Re: Programmatically access a JDBC connection via DTP [message #491497 is a reply to message #489429] Wed, 14 October 2009 19:35 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Ok... First, the DDL question... The only example I can find is this:

private void executeDDL(String[] sqlStatements,
IConnectionProfile connectionProfile) {
DatabaseIdentifier databaseIdentifier = new DatabaseIdentifier(
connectionProfile.getName(), "");
if (databaseIdentifier != null) {
try {
Job job = new GroupSQLResultRunnable(null, sqlStatements, null,
null, databaseIdentifier, false, new HashMap(),
Messages.BaseExecuteAction_group_exec_title,
"Generate DDL");
job.setName(Messages.BaseExecuteAction_job_title);
job.setUser(true);
job.schedule();

PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage().showView(EditorConstants.RESULTS_VIEW);
} catch (Exception e) {
}
}
}

For the ability to pop up the property page as we do, check out
ConnectAction in o.e.d.connectivity.ui in the
o.e.d.connectivity.ui.actions package. The public void run(IAction
action) method pops up the dialog.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> It will be the latter one which is to execute the DDL file using the
> IConnectionProfile.
>
> Also, if not all the properties are specified (i.e.
> arePropertiesComplete() is false), can I bring up the same properties
> dialog to allow user to enter the valid connection information? If so,
> which command should I use?
>
> Thanks for your help!
>
> Joshua
>
Re: Programmatically access a JDBC connection via DTP [message #596499 is a reply to message #488939] Wed, 30 September 2009 20:00 Go to previous message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

Instead of directly using java.sql to establish a JDBC connection and manipulate the tables in the database, I would like to leverage DTP to use existing connection profile that is configured in the project and issue SQL statement. It is like the latter answer in your question.

Thanks.

Joshua
Re: Programmatically access a JDBC connection via DTP [message #596506 is a reply to message #596499] Wed, 30 September 2009 22:49 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
I'll work up an example over the next couple of days. Sorry for the delay.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> Instead of directly using java.sql to establish a JDBC connection and
> manipulate the tables in the database, I would like to leverage DTP to
> use existing connection profile that is configured in the project and
> issue SQL statement. It is like the latter answer in your question.
>
> Thanks.
>
> Joshua
>
>
Re: Programmatically access a JDBC connection via DTP [message #596513 is a reply to message #488967] Thu, 01 October 2009 16:31 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Ok...

So to get a named connection profile and connect via APIs, you use:

IConnectionProfile profile =
ProfileManager.getInstance().getProfileByName("myprofile");
IStatus status = profile.connect();
if (status.equals(IStatus.OK)) {
// success
} else {
// failure :(
if (status.getException() != null) {
status.getException().printStackTrace();
}
}

And then once you have your IConnectionProfile reference, you can get
the JDBC connection this way:

public java.sql.Connection getJavaConnectionForProfile
(IConnectionProfile profile) {
IConnection connection =
ProfileConnectionManager.
getProfileConnectionManagerInstance().getConnection(profile,
"java.sql.Connection");
if (connection != null) {
return (java.sql.Connection) connection.getRawConnection();
}
return null;
}

From there you can treat it like any other JDBC connection and create a
JDBC statement and execute it how you'd like.

Does that help?

--Fitz

Brian Fitzpatrick wrote:
> I'll work up an example over the next couple of days. Sorry for the delay.
>
> --Fitz
>
> Joshua Hui wrote:
>> Hi Fitz,
>>
>> Instead of directly using java.sql to establish a JDBC connection and
>> manipulate the tables in the database, I would like to leverage DTP to
>> use existing connection profile that is configured in the project and
>> issue SQL statement. It is like the latter answer in your question.
>>
>> Thanks.
>>
>> Joshua
>>
>>
Re: Programmatically access a JDBC connection via DTP [message #596518 is a reply to message #489165] Thu, 01 October 2009 16:54 Go to previous message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

Thanks. It is really useful. Really appreciate that.

BTW, apart from javadoc, is there any external document/web site which describes how to use the APIs? In that case, if I have any further question, I can refer to that.

Joshua
Re: Programmatically access a JDBC connection via DTP [message #596533 is a reply to message #489169] Thu, 01 October 2009 17:00 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
There really isn't, or wasn't until I created one this morning for FAQs.
You can see it here: http://wiki.eclipse.org/DTP_FAQ

What exactly are you looking for? I have a number of things I'll add to
that page over time.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> Thanks. It is really useful. Really appreciate that.
>
> BTW, apart from javadoc, is there any external document/web site which
> describes how to use the APIs? In that case, if I have any further
> question, I can refer to that.
>
> Joshua
Re: Programmatically access a JDBC connection via DTP [message #596540 is a reply to message #489173] Thu, 01 October 2009 21:54 Go to previous message
Joshua Hui is currently offline Joshua Hui
Messages: 23
Registered: July 2009
Junior Member
Hi Fitz,

Apart from using the raw JDBC connection, I am also looking for ways to programmatically invoke a ddl script from DTP. Is there a way to do it?

BTW, I tried your suggestion. It works except for
public java.sql.Connection getJavaConnectionForProfile( IConnectionProfile profile)

I have to change the following to get hold of the sql connection.

IManagedConnection connection = profile.getManagedConnection("java.sql.Connection");

if (connection != null) {
return (java.sql.Connection) connection.getConnection().getRawConnection();
}
return null;

Thanks.

Joshua
Re: Programmatically access a JDBC connection via DTP [message #596549 is a reply to message #489216] Fri, 02 October 2009 14:49 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Hi Joshua...

Thanks for the correction - I'll update that code in the FAQ too. :)

Let me ask around and see if we have an example of how to use the APIs
to invoke some DDL.

--Fitz

Joshua Hui wrote:
> Hi Fitz,
>
> Apart from using the raw JDBC connection, I am also looking for ways to
> programmatically invoke a ddl script from DTP. Is there a way to do it?
>
> BTW, I tried your suggestion. It works except for public
> java.sql.Connection getJavaConnectionForProfile(
> IConnectionProfile profile)
>
> I have to change the following to get hold of the sql connection.
>
> IManagedConnection connection =
> profile.getManagedConnection("java.sql.Connection");
>
> if (connection != null) {
> return (java.sql.Connection)
> connection.getConnection().getRawConnection();
> }
> return null;
>
> Thanks.
>
> Joshua
Re: Programmatically access a JDBC connection via DTP [message #596558 is a reply to message #489352] Fri, 02 October 2009 22:33 Go to previous message
Brian Fitzpatrick is currently offline Brian Fitzpatrick
Messages: 495
Registered: July 2009
Senior Member
Hey Joshua...

Are you trying to execute DDL so that the results appear in the results
view or simply execute some DDL with the JDBC connection you got from a
connection profile?

--Fitz

Brian Fitzpatrick wrote:
> Hi Joshua...
>
> Thanks for the correction - I'll update that code in the FAQ too. :)
>
> Let me ask around and see if we have an example of how to use the APIs
> to invoke some DDL.
>
> --Fitz
>
> Joshua Hui wrote:
>> Hi Fitz,
>>
>> Apart from using the raw JDBC connection, I am also looking for ways
>> to programmatically invoke a ddl script from DTP. Is there a way to
>> do it?
>> ...
>> Thanks.
>>
>> Joshua
Previous Topic:again on Data Tools api / extensions. How to write on DB using DTP objects.
Next Topic:Programmatically access a JDBC connection via DTP
Goto Forum:
  


Current Time: Wed Sep 17 19:50:23 GMT 2014

Powered by FUDForum. Page generated in 0.07603 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software