Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » DTP » Connecting to a database programmatically
Connecting to a database programmatically [message #38823] Thu, 15 November 2007 09:32 Go to next message
Eclipse UserFriend
Originally posted by: mcervera.integranova.com

Hi.

I am trying to connect to a postgreSQL database by code but I can't and
I don't find any example. Can anyone tell me where I can find one? Or
give me some code to connect to a database using datatools.

Thanks :).
Re: Connecting to a database programmatically [message #38855 is a reply to message #38823] Fri, 16 November 2007 09:27 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mcervera.integranova.com

I forgot to mention that I just want to get an instance of the class
Database of the metamodel with its respective references to the schemas,
tables, etc... of my postgres database. I am sure it must be easy but
I´ve been trying to do it for 3 days without success.

Thanks in advance


Mario Cervera escribió:
> Hi.
>
> I am trying to connect to a postgreSQL database by code but I can't and
> I don't find any example. Can anyone tell me where I can find one? Or
> give me some code to connect to a database using datatools.
>
> Thanks :).
Re: Connecting to a database programmatically [message #38885 is a reply to message #38855] Fri, 16 November 2007 17:06 Go to previous message
Eclipse UserFriend
Originally posted by: mcervera.integranova.com

I didn't get any answer but I´ve finally solved it. For everyone who
wants to know how to get an instance of the Database metaclass with all
the schemas, tables, etc... of a database (in my case postgreSQL), here
is the code that can do it:


String name = "PostgreSQL";
String desc = "Description";
String providerID =
" org.eclipse.datatools.enablement.postgresql.connectionProfil e ";

ConnectionProfile profile = new ConnectionProfile(name, desc, providerID);

Properties props = new Properties();

props.setProperty(IDBConnectionProfileConstants.DATABASE_VEN DOR_PROP_ID,
"postgreSQL");

props.setProperty(IDBConnectionProfileConstants.DATABASE_VER SION_PROP_ID,
"8.2");
props.setProperty(IDBConnectionProfileConstants.DATABASE_NAM E_PROP_ID,
"postgres");
props.setProperty(IDBConnectionProfileConstants.DRIVER_CLASS _PROP_ID,
"org.postgresql.Driver");
props.setProperty(IDBConnectionProfileConstants.URL_PROP_ID,
"jdbc:postgresql:postgres");
props.setProperty(IDBConnectionProfileConstants.USERNAME_PRO P_ID,
"postgres");
props.setProperty(IDBConnectionProfileConstants.PASSWORD_PRO P_ID,
"postgres");

props.setProperty(ConnectionProfileConstants.PROP_DRIVER_DEF INITION_ID,
"DriverDefn.PostgreSQL JDBC Driver");

profile.setBaseProperties(props);

profile.connect();

IManagedConnection managedConnection =
profile.getManagedConnection(" org.eclipse.datatools.connectivity.sqm.core.connection.Conne ctionInfo ");
IConnection connection = managedConnection.getConnection();
ConnectionInfo connectionInfo =
(ConnectionInfo)connection.getRawConnection();
Database db = connectionInfo.getSharedDatabase();



Mario Cervera escribió:
> I forgot to mention that I just want to get an instance of the class
> Database of the metamodel with its respective references to the schemas,
> tables, etc... of my postgres database. I am sure it must be easy but
> I´ve been trying to do it for 3 days without success.
>
> Thanks in advance
>
>
> Mario Cervera escribió:
>> Hi.
>>
>> I am trying to connect to a postgreSQL database by code but I can't
>> and I don't find any example. Can anyone tell me where I can find one?
>> Or give me some code to connect to a database using datatools.
>>
>> Thanks :).
Re: Connecting to a database programmatically [message #588649 is a reply to message #38823] Fri, 16 November 2007 09:27 Go to previous message
Mario Cervera is currently offline Mario CerveraFriend
Messages: 66
Registered: July 2009
Member
I forgot to mention that I just want to get an instance of the class
Database of the metamodel with its respective references to the schemas,
tables, etc... of my postgres database. I am sure it must be easy but
I´ve been trying to do it for 3 days without success.

Thanks in advance


Mario Cervera escribió:
> Hi.
>
> I am trying to connect to a postgreSQL database by code but I can't and
> I don't find any example. Can anyone tell me where I can find one? Or
> give me some code to connect to a database using datatools.
>
> Thanks :).
Re: Connecting to a database programmatically [message #588660 is a reply to message #38855] Fri, 16 November 2007 17:06 Go to previous message
Mario Cervera is currently offline Mario CerveraFriend
Messages: 66
Registered: July 2009
Member
I didn't get any answer but I´ve finally solved it. For everyone who
wants to know how to get an instance of the Database metaclass with all
the schemas, tables, etc... of a database (in my case postgreSQL), here
is the code that can do it:


String name = "PostgreSQL";
String desc = "Description";
String providerID =
" org.eclipse.datatools.enablement.postgresql.connectionProfil e ";

ConnectionProfile profile = new ConnectionProfile(name, desc, providerID);

Properties props = new Properties();

props.setProperty(IDBConnectionProfileConstants.DATABASE_VEN DOR_PROP_ID,
"postgreSQL");

props.setProperty(IDBConnectionProfileConstants.DATABASE_VER SION_PROP_ID,
"8.2");
props.setProperty(IDBConnectionProfileConstants.DATABASE_NAM E_PROP_ID,
"postgres");
props.setProperty(IDBConnectionProfileConstants.DRIVER_CLASS _PROP_ID,
"org.postgresql.Driver");
props.setProperty(IDBConnectionProfileConstants.URL_PROP_ID,
"jdbc:postgresql:postgres");
props.setProperty(IDBConnectionProfileConstants.USERNAME_PRO P_ID,
"postgres");
props.setProperty(IDBConnectionProfileConstants.PASSWORD_PRO P_ID,
"postgres");

props.setProperty(ConnectionProfileConstants.PROP_DRIVER_DEF INITION_ID,
"DriverDefn.PostgreSQL JDBC Driver");

profile.setBaseProperties(props);

profile.connect();

IManagedConnection managedConnection =
profile.getManagedConnection(" org.eclipse.datatools.connectivity.sqm.core.connection.Conne ctionInfo ");
IConnection connection = managedConnection.getConnection();
ConnectionInfo connectionInfo =
(ConnectionInfo)connection.getRawConnection();
Database db = connectionInfo.getSharedDatabase();



Mario Cervera escribió:
> I forgot to mention that I just want to get an instance of the class
> Database of the metamodel with its respective references to the schemas,
> tables, etc... of my postgres database. I am sure it must be easy but
> I´ve been trying to do it for 3 days without success.
>
> Thanks in advance
>
>
> Mario Cervera escribió:
>> Hi.
>>
>> I am trying to connect to a postgreSQL database by code but I can't
>> and I don't find any example. Can anyone tell me where I can find one?
>> Or give me some code to connect to a database using datatools.
>>
>> Thanks :).
Previous Topic:Connecting to a database programmatically
Next Topic:Edit or add column to mysql database?
Goto Forum:
  


Current Time: Tue Apr 23 13:32:29 GMT 2024

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

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

Back to the top