在线部署后,Java JDBC Communications链接失败

时间:2017-08-06 14:25:51

标签: java mysql google-app-engine jdbc sqlexception

我在netbeans上使用java httpServlet创建了我的第一个API。经过几天的工作,我终于可以将它连接到这个mySQL googleCloud数据库并在本地获得结果。但是,一旦使用appengine在谷歌云上发布此API,就会抛出以下错误:

    Error sqlException:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
 Communications link failure The last packet sent successfully to the server was 0
 milliseconds ago. The driver has not received any packets from the server.

但mySQL数据库允许访问所有IP,那么可能是什么问题呢?以下是我的代码:

public class HelloServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        PrintWriter out = resp.getWriter();
        //out.println("Dear vistor, you are accessing my first project deployed on cloud. Thank you");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            String url = String.format("jdbc:mysql://IP:3306/DatabaseName?useSSL=false&useUnicode=true&characterEncoding=UTF-8");
            Connection connection = DriverManager.getConnection(url, "userName", "Password");            
            try (Statement statement = connection.createStatement()) {
                ResultSet resultSet = statement.executeQuery("select test from tableTest;");
                while (resultSet.next()) {
                    out.println(resultSet.getString(1));
                }
            }
        } catch (ClassNotFoundException ex) {
            out.println("Error ClassNotFoundException:" + ex.toString());
        } catch (SQLException ex) {
            out.println("Error sqlException:" + ex.toString());
        }
        catch(Exception ex){
            out.println("Exception:" + ex.toString());
        }
    }   
}

更新

基于this link,我发现在线发布时我不应该使用密码。所以我已经更新了我的代码,以这种方式使用jdbc google驱动器:

Class.forName("com.mysql.jdbc.GoogleDriver");

String url = String.format("jdbc:google:mysql://appId:instanceId/dbName?user=root");

Connection connection = DriverManager.getConnection(url);

但是当我在网上发布时,却抛出了同样的错误......

UPDATE2:

可能因为我的appId包含" - "喜欢" test-123"?但实际上我是以这种形式发送我的网址:

url =" jdbc:google:mysql:// test-123:myInstance / myDatabase?user = root& useUnicode = true& characterEncoding = UTF-8&#34 ;;

0 个答案:

没有答案