Reputation: 485
I'm trying to make a basic JDBC connection to MySQL. I deployed application on openshift server (tomcat7, mysql) but I can't connect with my db (I use phpmyadmin to create db and tables). I'am using Spring 3.1 MVC, JSF and Primefaces.
I deployed some time ago a simple java web application and I used a class conection:
String driver = "com.mysql.jdbc.Driver";
String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
String url = "jdbc:mysql://"+host+":"+port+"/demo01";
String user = "adminujFVYBF";
String password = "EIyNRbHNBxN_";
This time I wanted to use a jdbc.properties file with Spring MVC in order to manage the values
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT)}/libreriaapp
jdbc.username=adminCnH8p6r
jdbc.password=EhBHSvIqHFAz
So I tried unsuccessfully to figure out how can I use environmental variables in the jdbc url in order to get working my db .
This is my application Context
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<context:component-scan base-package="pe.egcc.eureka.app.layer.controller"/>
</beans>
[Project Repository] https://github.com/cachuan07/libreriaapp 1
If anyone can point me in the right direction that'd be great. Sorry for the huge post, it needed a bit of explaining to make it coherent. Hopefully it makes sense. Thanks.
Upvotes: 2
Views: 2096
Reputation: 1
Activate phpmyadmin, and open it you will find the ip in the header, take it and replace it in the OPENSHIFT_MYSQL_DB_HOST variable, the port is the default mysql : 3306.
Upvotes: 0
Reputation: 899
HI you can simply write a code like this..
try{Class.forName("com.mysql.jdbc.Driver").newInstance();
username=asfksjfjs;
password=sflkaskfjhsjk;
url="jdbc:mysql://178.360.01:3306/demo01";
con = (Connection) DriverManager.getConnection(url,username,password);}
just copy those port no and host name from your phpmyadmin...
It will work surly.
Upvotes: 3
Reputation:
I don't think you can use environment variables in a jdbc.properties file, I don't think they will get parsed (as answered here Regarding application.properties file and environment variable). You MIGHT be able to use the environment variables right in your xml file if that xml file gets parsed correctly by something that will substitute them (like it does for a standalone.xml or context.xml file)
Upvotes: 0