0
votes

I am new to google cloud, and I just configure all components needed in cloud computation. But I have a problem on MySQL connection between MySQL and APP engine/VM. I can browser see my main html page, but when my servlet try to make MySQL connection and it failed.

Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://mysql external IP:3306/Database?user=us&password=pw");

Error message: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure and The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. Class.forName("com.mysql.jdbc.GoogleDriver"); con = DriverManager.getConnection("jdbc:google:mysql://mysql external IP:3306/Database?user=us&password=pw"); Error message: java.lang.ClassNotFoundException: com.mysql.jdbc.GoogleDriver at java.net.URLClassLoader.findClass(URLClassLoader.java:382) I add appengine-web.xml in WEB-INF folder with true

Does anyone have the solution on this? I deploy to google cloud from my local Eclipse 2020-06 version.

Thanks

1

1 Answers

0
votes

You an try this sample code:

String url = "jdbc:mysql://ip:3306/dbname?useSSL=false";
String user = "root";
String password = "password";
StringBuffer datStr = new StringBuffer("");

String query = "SELECT * FROM dbname.tablename";

try {
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con = DriverManager.getConnection(url, user, password);
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(query);

    while (rs.next()) {
        //Code
    }

} catch (Exception ex) {
    ex.printStackTrace();
}