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

时间:2016-02-05 20:24:03

标签: java mysql 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;
    }

}

我在OpenShift中有一个java应用程序但是当我尝试将它连接到盒式磁带mysql时,连接时没有任何反应。只有它停留在servlet中并且不采取其他行动

1 个答案:

答案 0 :(得分:-1)

根据您在OpenShift上运行的Java服务器类型,请尝试阅读其中一篇文章,这些文章可以帮助您启动并运行数据库连接: