This example demonstrates the work with different database types.
The functionality of Database library doesn't depend on specific database type and the code of using it look the same in all cases. But to be able to connect to specific database type it's necessary to add corresponding database driver as Maven dependency. For convenience the Database library has already have some drivers as Maven dependencies. These are drivers for:
- MySQL
- PostgreSQL
- MS SQL Server
- Oracle
- DB2
Corresponding database driver is loaded based on the value of jdbcUrl
parameter when the method withConnection()
or
withTransaction()
is called.
The information regarding different JDBC URL formats for different type of databases can be found here.
Example of connection parameters to connect to MySQL database:
{
"jdbcUrl":"jdbc:mysql://localhost:33060/dbname",
"user": "root",
"password": "root"
}
Below an example of using these parameters that are specified as value of secret vault entry with alias "mysql.db"
:
@Inject
private DatabaseService dbService;
public void execute() {
dbService.withConnection("mysql.db", (c) -> {
ResultSet results = c.executeQuery("SELECT * FROM invoices");
while (results.next()) {
log.info("Invoice Number: {}", results.getString("invoice_number"));
}
});
}
Example of connection parameters to connect to MySQL database:
{
"jdbcUrl":"jdbc:postgresql://localhost:5432/postgres",
"user": "postgres",
"password": "root"
}
Below an example of using these parameters that are specified as value of secret vault entry with alias "postgres.db"
:
@Inject
private DatabaseService dbService;
public void execute() {
dbService.withConnection("postgres.db", (c) -> {
ResultSet results = c.executeQuery("SELECT * FROM invoices");
while (results.next()) {
log.info("Invoice Number: {}", results.getString("invoice_number"));
}
});
}
See the full source of this example for more details or check following instructions to run it.
⚠️ To be able to build and run this example it's necessary to have an access to some instance of EasyRPA Control Server.
Its a fully workable process. To play around with it and run do the following:
-
Download this example using link.
-
Unpack it somewhere on local file system.
-
Specify URL to the available instance of EasyRPA Control Server in the
pom.xml
of this example:<repositories> <repository> <id>easy-rpa-repository</id> <url>[Replace with EasyRPA Control Server URL]/nexus/repository/easyrpa/</url> </repository> </repositories>
-
If necessary, change version of
easy-rpa-engine-parent
in the samepom.xml
to corresponding version of EasyRPA Control Server:<parent> <groupId>eu.ibagroup</groupId> <artifactId>easy-rpa-engine-parent</artifactId> <version>[Replace with version of EasyRPA Control Server]</version> </parent>
-
Build it using
mvn clean install
command. This command should be run within directory of this example. -
Run
main()
method ofWorkingWithDifferentDbModule
class.