java - 将 jdbc 连接到 openshift 上的 MySql 服务器时出现问题

标签 java mysql sql tomcat jdbc

因此,我在我的 tomcat 服务器中使用微软的 jdbc jar 连接到托管在 openshift 上的 Mysql 服务器。 首先是 tomcat 代码...

try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            } catch (ClassNotFoundException e1) {
                out.println(e1);
            }
            try {
                Connection conn = DriverManager.getConnection("jdbc:sqlserver://xxx.x.xx.x:3306;DatabaseName=encryptionserver", "phpadminuser", "phpadminpass");
                Statement s = conn.createStatement();
                ResultSet rs = s.executeQuery("SELECT Username FROM LoggedInUsers WHERE Username='" + user + "'");
                String u = "none";
                while(rs.next()){
                    u = rs.getString("Username");
                    out.println(u);
                }
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                out.println("SQL Exception: " + e.toString() + " in getFriendRequest");
            } 

我正在使用我用来访问 openshift 中的 sql server 的 phpMyAdmin 凭据,所以也许这就是问题所在?它在第二次尝试时失败并捕获 SQLException e 输出

SQL Exception: com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host 1xx.x.xx.x, port 3306 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.". in getFriendRequest 

我已经尝试通过在本地运行 tomcat 来测试它,在 openshift 上使用 DriverManager.getConnection 的服务器 ip 并仅使用 localhost 作为 ip。两者返回相同的结果。

最佳答案

您使用的 jdbc 驱动程序不正确。对于 MySql,您必须使用 MySQL Connector/J 提供的 com.mysql.jdbc.Driver

另见:

关于java - 将 jdbc 连接到 openshift 上的 MySql 服务器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27161886/

相关文章:

java.lang.Object;无法转换为模型类 : error in Spring Boot

mysql - 打开一条记录,更新它并在 MySQL 中作为新记录插入

php - 将内爆多重选择更新到数据库中

mysql - 更改mysql中外键引用的数据类型

java - 为什么 HashMap 不能是静态的..?

java - 如何在 Java 中高效地创建 Web 数据结构?

java - 检查耳机是否在不同的 API 级别上连接

mysql - 查找丢失的日期并将它们添加到结果中 - MySql

java - 获取结果集的连续 2 行

mysql - sql加入where和1-n连接