JDBC cannot connect to mysql database in shutter mode

I was able to configure the MySQL database on OpenShift using phpMyAdmin and all. I was told the host name and port my for my database: $ OPENSHIFT_MYSQL_DB_HOST and $ OPENSHIFT_MYSQL_DB_PORT respectively, which I put in my context.xml file as follows:

<context-param> <param-name>driver</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/burgerjoint</param-value> </context-param> <context-param> <param-name>user</param-name> <param-value>admin******</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>*********</param-value> </context-param> 

Code for setting up the connection:

 public void contextInitialized(ServletContextEvent event) { // Connect String driver = event.getServletContext().getInitParameter(PARAM_DRIVER); String url = event.getServletContext().getInitParameter(PARAM_URL); String username = event.getServletContext().getInitParameter(PARAM_USERNAME); String password = event.getServletContext() .getInitParameter(PARAM_PASSWORD); try { Class.forName(driver); Connection connection = DriverManager.getConnection(url, username, password); event.getServletContext().setAttribute(ATTR_CONNECTION, connection); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } 

but the problem is that the connection is null on the server, and I don't understand why. Did I do something wrong? The code works when I try it on localhost. And as far as I can tell, I have all the necessary libraries:

enter image description here

Thanks for the help:)

Update

I changed the connection code as follows:

 { if (connection != null) return connection; try { Properties dbProperties = new Properties(); InputStream input = DatabaseUtil.class.getClassLoader().getResourceAsStream(DB_PROPERTIES_FILE); dbProperties.load(input); String url = ""; if (appIsDeployed) { String host = System.getenv("$OPENSHIFT_MYSQL_DB_HOST"); String port = System.getenv("$OPENSHIFT_MYSQL_DB_PORT"); String name = "burgerjoint"; url = "jdbc:mysql://" + host + ":" + port + "/" + name; } else { url = dbProperties.getProperty(PARAM_URL); } String driver = dbProperties.getProperty(PARAM_DRIVER); String username = dbProperties.getProperty(PARAM_USERNAME); String password = dbProperties.getProperty(PARAM_PASSWORD); Class.forName(driver); connection = DriverManager.getConnection(url, username, password); 

but it still gives a null connection. The values ​​of System.getenv("$OPENSHIFT_MYSQL_DB_HOST") and System.getenv("$OPENSHIFT_MYSQL_DB_PORT") are zero.

+4
source share
4 answers

This is not true:

 jdbc:mysql://$OPENSHIFT_MYSQL_DB_HOST:$OPENSHIFT_MYSQL_DB_PORT/burgerjoint 

This dollar sign suggests that you think that the correct host and database name will be replaced, but that is not the case.

Enter the host name and database name to see that it is working, and then find out about the .properties files to make them screen.

+4
source

Clear dollar signs:

  String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST"); String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT"); 

And it will work perfectly (I tested it on my machine and on my OpenShift).

+2
source

You can start port forwarding first.

rhc port-forward -a your-app-name

After starting, do not kill this cmd and just use the generated local addresses / ports.

0
source

If you use a MySQL cartridge, the information for connecting to the database is in the application environment variables . If you are using JDBC pure, you need to get the values ​​first. eg:.

 String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST"); String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT"); String username = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME"); String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD"); String url = String.format(":mysql://%s:%s/jbossas", host, port); Connection conn = DriverManager.getConnection(url, username, password); 

jbossas in url string is a schema. Change your layout ( burgerjoint ).

0
source

Source: https://habr.com/ru/post/1485043/


All Articles