Connecting to a database programmatically [message #38823] |
Thu, 15 November 2007 09:32 |
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 09:27 |
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 17:06 |
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 #588660 is a reply to message #38855] |
Fri, 16 November 2007 17:06 |
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 :).
|
|
|
Powered by
FUDForum. Page generated in 0.02878 seconds