MariaDB - Java Connection

Java Connection to MariaDB 

1. Log into Cloudjiffy.

2. Create an environment with MariaDB database:

   

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

                                             

4. Click Open in browser button for MariaDB. Log in to the admin panel using abovementioned credentials and create a new database, for example, MariaDB connection.

      

5. Click the Config button next to the application server (Tomcat 7) in the expandable list of the environment.

  

6. In the opened tab, create a new mydb.cfg file in the home folder and add the necessary configurations there:

host=jdbc:mysql://mariadb{nodeID}-{env_name}.{hoster_domain}/{db_name}
username={get in the email from Robot@cloudjiffy} 
password={get in the email from Robot@cloudjiffy}
driver=com.mysql.jdbc.Driver

 

Note: You can mention all connection settings in your code (application) apparently. In the given example we put all the settings to the file, which is read by our application.

7. As an example here you can see the code of our application which connects to our database.

DbManager.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package connection;
  
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
  
public class DbManager {
  
    private final static String createTable = "CREATE TABLE `example` (id INT, data VARCHAR(100))";
  
    public Connection createConnection() throws IOException, ClassNotFoundException, SQLException {
  
        Connection connection;
          
        Properties prop = new Properties();
        System.out.println("test");
        prop.load(new FileInputStream(System.getProperty("user.home") + "/mydb.cfg"));
        System.out.println("user.home: "+System.getProperty("user.home"));
        String host = prop.getProperty("host").toString();
        String username = prop.getProperty("username").toString();
        String password = prop.getProperty("password").toString();
        String driver = prop.getProperty("driver").toString();
  
        System.out.println("host: " + host + "\username: " + username + "\password: " + password + "\ndriver: " + driver);
  
        Class.forName(driver);
        System.out.println("--------------------------");
        System.out.println("DRIVER: " + driver);
        connection = DriverManager.getConnection(host, username, password);
        System.out.println("CONNECTION: " + connection);
  
        return connection;
    }
  
    public void runSqlStatement() {
        try {
            Statement statement = createConnection().createStatement();
            boolean rs = statement.execute(createTable);
  
        } catch (IOException ex) {
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DbManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}

8. The next step is to upload a .war file to Cloudjiffy Deployment Manager. As an example, we used the dbconn example.war file (click to download it) which contains appropriate JDBC-connector.

Click the link dbconn example to download the package with the sources of our project.

 

9. Deploy the uploaded WAR file to the environment.

 

10. Click Open in Browser next to the application server (Tomcat 7).

11. Click Open in Browser next to the MariaDB database to see the created table in the database Admin panel.

 
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

CouchDB - Java Connection

CouchDB connection 1. Log into  Cloudjiffy   2. Create an environment with CouchDB database:...

MongoDB - Java Connection

Java Connection to MongoDB MongoDB is one of the most popular NoSQL databases, which can be...

MySQL - Java Connection

Java Connection to MySQL MySQL is a highly popular open source database, used by developers...

PostgreSQL - Java Connection

Connection to PostgreSQL for Java Applications To establish connection between PostgreSQL...

Powered by WHMCompleteSolution