簡體   English   中英

Java應用程序無法在openshift中連接到mysql數據庫

[英]Java Application cannot connect to mysql database in openshift

大家!

我正在使用RedHat PaaS的OpenShift免費帳戶托管Java應用程序。 為了進行測試,我創建了一個應用程序,該應用程序僅在index.jsp中獲得兩個用戶信息(登錄名和密碼),然后搜索數據庫並重定向到成功頁面(消息“早上好/下午/晚上,$ {user.name }“)或失敗頁面(消息“您尚未注冊”)。 我還創建了一個數據庫autenticacao,在phpMyAdmin中創建了一個名為usuario(用戶)的表,該表在localhost中運行良好。 但是在openshift中,我的servlet從方法obter(String login,String senha)中接收到一個空的“ usuario”(用戶)對象,該對象應該得到一個選擇查詢的結果。 我認為它不會創建數據庫連接。 我真的非常努力地使其工作,並且在forun中看到了太多的解決方案(也在stackoverflow中),但是沒有任何效果。

我使用了一些設計模式,但我認為這不是問題。

這是我的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;  

    }
}

當我嘗試在st = conn.createStatement();中創建連接時發生錯誤。 部分。

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;
}

}

這段代碼是葡萄牙語,所以如果有任何疑問,您可以問我。 我還有其他班級,如果您願意的話,我可以告訴您。

那么,如何連接到我的數據庫? 你能幫助我嗎? 謝謝。

您還在面對這個問題嗎? 如果是,則嘗試將您的openshift應用程序設置為可擴展,使其不可擴展,否則請設置為不可擴展。 在我不完全記得之前,我已經遇到過這個問題。 對於您而言,如果您不想更改應用程序的可伸縮性,則openshift中的端口轉發將很有幫助。 如果您也需要jboss,我會使用Jboss Dev Studio在openshift中創建我的jboss應用。

您需要更改以下行:

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

它應該是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM