简体   繁体   中英

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. only it stays in the servlet and takes no other action

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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