Java应用程序无法连接到mysql数据库openshift
大家好!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中看到了太多的解决方案(这里也是在*中),没有任何工作。
即时通讯使用一些设计模式,但我认为这不是问题。
这是我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 Dev Studio在openshift中创建我的jboss应用程序,以防您也需要jboss。
我不知道它是否可扩展,我认为它不是。我在eclipse和JBOSS插件中创建了项目,我认为它与JB Dev Studio相同。但现在,你可以连接到你的MySQL数据库吗?无论如何,我会试试这个。 –
您需要更改行:
Connection conn = DriverManager.getConnection(host+port+"/autenticacao",user,password);
它应该是:
Connection conn = DriverManager.getConnection("jdbc:mysql:"+host+":"+port+"/autenticacao",user,password);
什么错误?此外,你有空的catch块可以追踪这个问题非常困难。 – JamesB
此外,当更新您的代码时,将executeQuery替换为preparedStatement:您的代码可以通过SQL注入非常简单。 –
你得到的错误是什么? –