简体   繁体   English

java如何在OpenShift中连接到MySQL数据库

[英]java how to connect to a MySQL database in OpenShift

package library;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class conexion {

    private String server = "jdbc:mysql://" + System.getenv("OPENSHIFT_MYSQL_DB_HOST") + ":" + System.getenv("OPENSHIFT_MYSQL_DB_PORT") + "/" + System.getenv("OPENSHIFT_APP_NAME") + "";
    private String user = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
    private String pass = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
    private Connection conn;

    public Connection conectar() throws ClassNotFoundException, SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            this.conn = DriverManager.getConnection(server, user, pass);
        } catch (Exception ex) {
            Logger.getLogger(conexion.class.getName()).log(Level.SEVERE, null, ex + ex.getMessage());
        }

        return this.conn;
    }

}

I have a java application lodged in OpenShift but when I try to connect it to the cartridge mysql nothing happens when you connect. 我有一个Java应用程序存放在OpenShift中,但是当我尝试将其连接到MySQL墨盒时,连接时什么也没有发生。 only it stays in the servlet and takes no other action 只有它保留在servlet中,并且不执行其他任何操作

Depending on what type of java server you are running on OpenShift, try reading through one of these articles, which should help you get your database connection up and running: 根据您在OpenShift上运行的Java服务器类型,尝试阅读以下文章之一,这将有助于您建立和运行数据库连接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM