Click here to Skip to main content
15,885,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:


Java
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 {
            if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
    Class.forName("com.mysql.jdbc.GoogleDriver");
     
        url = "jdbc:google:mysql://test-123:myInstance/myDatabase?user=root&useUnicode=true&characterEncoding=UTF-8";
    } else {
        Class.forName("com.mysql.jdbc.Driver");
        url = "jdbc:mysql://1.2.3.4:3306/myDatabase?user=root&password=test";
    }
    Connection connection = DriverManager.getConnection(url);
    try (Statement statement = connection.createStatement()) {
                ResultSet resultSet = statement.executeQuery("select test from tableTest;");
                while (resultSet.next()) {
                    out.println(resultSet.getString(1));
                }
            }        
        }catch(Exception ex){
            out.println("Exception:" + ex.toString());
        }
    }   
}


Could it be because of my projectD (because it has special character) ? Please help me

What I have tried:

I have seen this multiple times and I did the exact solution like here and here
Posted
Comments
Herman<T>.Instance 10-Aug-17 4:31am    
Connection.Open?
H.AL 10-Aug-17 11:38am    
getConnection in java is the Connection.Open in c#;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900