2

I have created my first API using java httpServlet on netbeans. After days working I could finally connect it to this mySQL googleCloud database and get results locally. But once this API is published on google cloud using appengine, below error was thrown:

    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.

But the mySQL database is allowing access for all IPs, so what could be the problem? Below is my code:

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());
        }
    }   
}

Update:

Based on this link, I have discovered that when publishing online I should not use a password. So I have updated my code to use jdbc google drive in this way:

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

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

Connection connection = DriverManager.getConnection(url);

But when I published online , same error was thrown...

Update2:

Could it be bcause my appId contains "-" like "test-123"? But actually I am sending my url in this form:

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

Mlle 116
  • 1,149
  • 4
  • 20
  • 53

0 Answers0