Configure database connectivity in Jelastic

| August 22, 2011 | DevOps PaaS | , , , ,

Most applications have underlying databases. In case of Jelastic, you get a wide choice of the databases to choose from. Here's a quick tip on how to connect to the database you chose from your application.
The gist is that:

  • The database exists on a separate server, and to get the URL of that server you need to click the Open in Browser button next to the database in the environment in Jelastic dashboard,
  • Your administrative credentials for the database get emailed to your when you create the environment. You can then click Open in Browser and use the credentials to further create the databases and accounts you need.

If you need further details - here's the step by step version (scroll down for the video version!):
We will use MariaDB as an example, but for MySQL and other databases you will be basically following the same steps.
1. Create the environment in Jelastic,

2. Check your e-mail - your inbox should have a message from Robot@jelastic with database login and password:

3.Expand the environment and click Open in Browser next to the MariaDB database - this will open the database administrative page, use the credentials from email to log in:

4. If you don't do so in your code, create the database you need on that server:

5. Within the database (for example, jelasticDb), create a user account to access it. Make sure to remember the Login and Password:

6. Make sure that you use the URL which Jelastic uses when you open the database in browser (see above) in the database connection string. For example, in our case it is mariadb.mariadbexample.jelastic.com. The code (which you edit in your IDE of choice) will probably look similar to this:

package сonnection;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MariadDb {
    private final static String username = "jelastic", password = "jelastic";
    private final static String createTableQuery = "CREATE TABLE `books` ("
            + "  `id` int(11) NOT NULL auto_increment,"
            + "  `title` varchar(50) default NULL,"
            + "  `comment` varchar(100) default NULL,"
            + "  `price` double default NULL,"
            + "  `author` varchar(50) default NULL,"
            + "  PRIMARY KEY  (`id`)"
            + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;";
    public Connection createConnection() {
        Connection connection = null;
        try {
            // Load the JDBC driver
            Class.forName("com.mysql.jdbc.Driver");
            // Create a connection to the database
            connection = DriverManager.getConnection("jdbc:mysql://mariadb.mariadbexample.jelastic.com/jelasticDb", username, password);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }    public void runSqlStatement() {
        try {
            Statement statement = createConnection().createStatement();
            //Executing query
            boolean rs = statement.execute(createTableQuery);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

7. For connecting to MariaDb from Java code you also need JDBC MySQL .jar library mysql-connector-java-5.1.17-bin.jar
8. After adding all files to the web-application project, you need to BUILD your project to get the .WAR archive.
9. Upload the .war archive to Jelastic's Deployment Manager.
10. Now deploy this WAR to your environment and open it in browser,
11. The code sample which we provided above, creates a new table called book in thejelasticDb database. Which you can then verify by checking the database administrative interface.
That is it. Here's also a video showing the procedure: