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 04:32 Go to next message
Eclipse User
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 04:27 Go to previous messageGo to next message
Eclipse User
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 12:06 Go to previous message
Eclipse User
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 04:27 Go to previous message
Mario Cervera is currently offline Mario Cervera
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 12:06 Go to previous message
Mario Cervera is currently offline Mario Cervera
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: Thu Jul 24 09:12:12 EDT 2014

Powered by FUDForum. Page generated in 0.02491 seconds