Java Connection to MySQL
MySQL is a highly popular open source database, used by developers all over the world. In this instruction we’ll show you how to connect your Java application, hosted within Apiqcloud , to this DB server.
1. Log into your Apiqcloud account.
2. Create new environment with the MySQL database server (available within the SQL wizard section):
3. Check your email inbox - it should contain a message from Robot@jelastic with administration details for the created MySQL server (access URL, login and password):
4. Switch back to the dashboard and click the Open in browser button for your MySQL node. Log in to the opened admin panel using the abovementioned credentials and create a new database (for example, mysqlconnection).
5. Then, click the Config button next to the application server (Tomcat 7 in our case) in the expandable environment’s nodes list.
6. In the opened tab, create a new mydb.cfg file inside the /opt/tomcat/temp folder and add the following MySQL connection strings there:
host=jdbc:mysql://{mysql_node_id}-{your_env_name}.{hoster_domain}/{db_name}username={get it in the email from Apiqcloud}
password={get it in the email from Apiqcloud}
driver=com.mysql.jdbc.Driver
where {node_id} - ID of the container with MySQL server you want to receive the access to. It can be seen at the dashboard (4227 in our case).
In such a way, the required settings for the mysql environment, used in this instruction, will look like at the image below.
Note: To connect the MySQL database to your project, you can mention all the required connecting 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 MySQL node.
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. Next, upload the .war file of your project to the Apiqcloud Deployment Manager. As an example, we’ll use the dbconnexample.war file which contains the appropriate jdbc-connector.
To connect your own project to a MySQL node, you need to upload the jdbc-connector jar file to the webapps/{app_context}/WEB-INF/lib folder of your Apiqcloud environment with your application deployed. |
9. Deploy the uploaded WAR file to the environment.
10. Now you can click Open in browser next to your application server (Tomcat 7 in our case).
works fine, click Open in browser next to your MySQL node and navigate to the previously created mysqlconnection database in the opened admin panel. You’ll see the new example table appeared in it, which means the DB has been successfully accessed from the deployed Java application.
It’s that simple!