2015-04-06 203 views
0

大家好!Java應用程序無法連接到mysql數據庫openshift

我使用RedHat PaaS的OpenShift免費帳戶來託管我的java應用程序。對於測試,我創建了一個應用程序,只需在index.jsp中獲取兩個用戶信息(登錄名和密碼),然後搜索到數據庫並重定向到成功頁面(消息「Good morning/afternoot/night,$ {user.name }「)或失敗頁面(消息」您沒有註冊「)。我還創建了一個名爲autenticacao的數據庫,在phpMyAdmin中有一個名爲usuario(user)的表,這在localhost中運行良好。但在openshift中,我的servlet從方法obter(String login,String senha)接收到一個空的'usuario'(user)對象,它應該得到一個select查詢的結果。我認爲它不創建數據庫連接。我真的很努力地努力使它成功,並在foruns中看到了太多的解決方案(這裏也是在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; 
} 

}

此代碼是葡萄牙語,因此,如果任何單詞沒有道理,你可以問我。我還有其他課程,如果你願意,我可以展示。

那麼,我該如何連接到我的數據庫?你可以幫我嗎?謝謝。

+0

什麼錯誤?此外,你有空的catch塊可以追蹤這個問題非常困難。 – JamesB

+0

此外,當更新您的代碼時,將executeQuery替換爲preparedStatement:您的代碼可以通過SQL注入非常簡單。 –

+0

你得到的錯誤是什麼? –

回答

0

你還在面對這個問題嗎?如果是,那麼嘗試使您的openshift應用程序不可擴展,如果它設置爲可擴展的,反之則不可擴展。我之前遇到過這個問題,我只是不記得確切。在你的情況下,如果你不想改變應用程序的可伸縮性,openshift中的端口轉發將有所幫助。我使用Jboss Dev Studio在openshift中創建我的jboss應用程序,以防您也需要jboss。

+0

我不知道它是否可擴展,我認爲它不是。我在eclipse和JBOSS插件中創建了項目,我認爲它與JB Dev Studio相同。但現在,你可以連接到你的MySQL數據庫嗎?無論如何,我會試試這個。 –

0

您需要更改行:

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

它應該是:

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