因此,我在tomcat服务器中使用Microsoft的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");
}
我正在使用phpMyAdmin凭据(用于在openshift中访问sql服务器),所以也许是问题所在?第二次尝试失败,并捕获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
类
也可以看看:
Connect to MySQL with JDBC driver
Using Connector/J with Tomcat