java - Connect web application with mysql database while web hosting -


i using jelastic.com host spring mvc application mysql database.for specified username , password database of mysql in application.then uploaded war file . application uploaded can't connect database. please me should connect?

mysql highly popular open source database, used developers on world. in instruction we’ll show how connect java application, hosted within jelastic cloud, db server.

please, follow step-by-step instruction below:

  1. log jelastic account.

enter image description here

  1. create environment tomcat7 , mysql database server (aviable within sql section).

enter image description here

  1. check email inbox - should contain message jelastic's platform administration credentials created mysql server (access url, login , password), example:

enter image description here

  1. switch dashboard , click open in browser button mysql node. log in opened admin panel using above mentioned credentials , create new database (for example, mysqlconnection).

enter image description here

  1. then, click config button next application server (tomcat 7 in our case) in expandable environment’s nodes list.

enter image description here

  1. in opened tab, create new mydb.cfg file inside home folder , add following mysql connection strings there:

    host=jdbc:mysql://mysql{node_id}-{your_env_name}.{hoster_domain}/{db_name} username={get in email jelastic} password={get in email jelastic} driver=com.mysql.jdbc.driver

where {node_id} - id of container mysql server want receive access to. can seen @ dashboard (277134 in our example).

enter image description here

in such way, required settings mysql environment, used in instruction, @ image below.

enter image description here note: connect mysql database project, can mention required connecting settings in code (application) apparently. in given example put settings file, read our application.

  1. as example, here can see code of our application, connects our mysql node.

dbmanager.java:

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();         }     } } 
  1. next, upload .war file of project jelastic deployment manager. example, we’ll use dbconnexample.war file (click download it) contains appropriate jdbc-connector.

to connect own project mysql node, need upload jdbc-connector jar file webapps/{app_context}/web-inf/lib folder of jelastic environment application deployed.

click dbconnexample link download package sources of our project.

enter image description here

  1. deploy uploaded war file environment.

enter image description here

  1. now can click open in browser next application server (tomcat 7 in our case). you’ll see new window create table "example" in database button opened. click button.

enter image description here

  1. in order ensure works fine, click open in browser next mysql node , navigate created mysqlconnection database in opened admin panel. you’ll see new example table appeared in it, means db has been accessed deployed java application.

enter image description here


Comments