简体   繁体   中英

Java Application cannot connect to mysql database in openshift

everyone!

I'm using the free account of OpenShift by RedHat PaaS to host my java aplication. For tests, I created an aplication that just get two user info (login and password) in the index.jsp, then it search into database and redirect to the success page(message "Good morning/afternoot/night, ${user.name}") or a failure page (message "You're not registred"). I also created a database, called autenticacao, with one table called usuario (user) in phpMyAdmin and this works well in localhost. But in the openshift, my servlet receives a null 'usuario' (user) object from the method obter(String login, String senha), that should get a result of one select query. I think it doesnt create a database connection. I've really tried so hard to make it works, and seen too many solutions in foruns (also here in stackoverflow) and nothing works.

Im using some design patterns but I think it's not a problem.

This is my DatabaseLocator.java:`

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package dao;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    /**
     *
     * @author Luiz
     */
    public class DatabaseLocator {

        private static DatabaseLocator instance = new DatabaseLocator();

        public static DatabaseLocator getInstance() {
        return instance;
    }

    private DatabaseLocator() {

    }

    public Connection getConnection() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        String user = System.getenv("OPENSHIFT_MYSQL_DB_USERNAME");
        String password = System.getenv("OPENSHIFT_MYSQL_DB_PASSWORD");
        String url = System.getenv("OPENSHIFT_MYSQL_DB_URL");
        String host = System.getenv("OPENSHIFT_MYSQL_DB_HOST");
        String port = System.getenv("OPENSHIFT_MYSQL_DB_PORT");
        Connection conn
                = DriverManager.getConnection(host+port+"/autenticacao",user,password);
        return conn;  

    }
}

The error happens when I try create a connection in st = conn.createStatement(); part.

public Usuario obterUsuario(String login, String password) {
    Usuario usuario = null;
    Connection conn = null;
    Statement st = null;
    try {
        conn = DatabaseLocator.getInstance().getConnection();
        st = conn.createStatement();
        ResultSet rs = st.executeQuery("select * from usuario where login='" + login + "' and senha='" + password + "'");
        rs.first();
        usuario = instanciar(rs);
    } catch (ClassNotFoundException cnf) {

    } catch (SQLException ex) {
        System.out.println(ex.getCause());
    }

    return usuario;
}

public static Usuario instanciar(ResultSet rs) throws SQLException, ClassNotFoundException {
    Usuario usuario = new Usuario();
    usuario.setLogin(rs.getString("login"));
    usuario.setSenha(rs.getString("senha"));
    //other rs fields that would be setted to user object
    return user;
}

}

This code is in portuguese, so if any word doesnt make sense, you can ask me. I have some other classes, I can show it if you want.

So, how can I connect to my database? Can you help me? Thanks.

Are you still facing this problem? If yes then try to make your openshift application non-scalable if it's set to scalable and vise versa if not. I've encountered this before I just don't remember exactly. In your case, port-forwarding in openshift will help if you don't want to change the scalability of your application. I use Jboss Dev Studio for creating my jboss app in openshift, in case you also want jboss.

You need to change the line:

Connection conn = DriverManager.getConnection(host+port+"/autenticacao",user,password);

It should be:

Connection conn = DriverManager.getConnection("jdbc:mysql:"+host+":"+port+"/autenticacao",user,password);

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