【问题标题】:I can't connect to my google cloud sql database from my deployed backend servlet on the google app engine我无法从谷歌应用引擎上部署的后端 servlet 连接到我的谷歌云 sql 数据库
【发布时间】:2015-06-09 08:54:49
【问题描述】:

我已经完成了我需要做的所有事情,我在谷歌应用引擎上部署的后端 servlet 没有与我的谷歌云数据库建立任何连接,最糟糕的是,我的应用引擎日志没有甚至显示任何错误,它显示“POST /hello HTTP/1.1”200 46...我真的不明白过去 5 天这里发生了什么......

这些是我在下面所做的事情:

这是我的 servlet 代码:

 public class MyServlet extends HttpServlet {   
        @Override  
        public void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {    
            resp.setContentType("text/plain");  
            resp.getWriter().println("Please use the form to POST to this url");  
        }

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

        String url = null;
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();

        try {
           if (SystemProperty.environment.value() ==
                   SystemProperty.Environment.Value.Production) {
              // Connecting from App Engine.

                // Load the class that provides the new "jdbc:google:mysql://" prefix.
            Class.forName("com.mysql.jdbc.GoogleDriver");
            url = "jdbc:google:mysql://festive-shark-90408:daily-joy-project/database?user=joys";

           } else {
                // Local MySQL instance to use during development.
             Class.forName("com.mysql.jdbc.Driver");
             url = "jdbc:mysql://<cloud-sql-instance-ip>:3306/database?user=joys";

                // Alternatively, connect to a Google Cloud SQL instance using:

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

        }


        try {
            Connection conn = DriverManager.getConnection(url);
            try {

                String fname = req.getParameter("name");
              //  String content = req.getParameter("message");


                    String statement = "INSERT INTO message (tags, posts, date, datetime) VALUES( ? , 'message', now(), now() )";
                    PreparedStatement stmt = conn.prepareStatement(statement);
                    stmt.setString(1, fname);
                   // stmt.setString(2, content);
                    int success = 2;
                    success = stmt.executeUpdate();
                    if (success == 1) {
                        out.println(
                                "<html><head></head><body>Success! Redirecting in 3 seconds...</body></html>");
                    } else if (success == 0) {
                        out.println(
                                "<html><head></head><body>Failure! Please try again! " +
                                        "Redirecting in 3 seconds...</body></html>");
                    }



            } finally {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }



    }

    }

在我的 appengine-web.xml 中,参数如下:

应用价值:festive-shark-90408

版本值:app-001

线程安全值:true

使用-google-connector-j 值:true

系统属性:

    property name="java.util.logging.config.file" value="WEB-INF/logging.properties" 

我的 android studio 使用的是 JDK 1.7.0_72

我的 appengine sdk 是 appengine-java-sdk-1.9.18

我已经在我的云 sql 中设置了访问控制来授权我的应用引擎应用程序并授权我的网络 ip。

我已经成功部署了我的后端 servlet,每当我向 servlet 发出 post 请求时,我的应用引擎日志只显示以下内容:

203.106.176.214 - - [04/Apr/2015:04:43:17 -0700] "POST /hello HTTP/1.1" 200 45 - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML , 像壁虎) Chrome/41.0.2272.118 Safari/537.36" "festive-shark-90408.appspot.com" ms=39 cpu_ms=76 cpm_usd=0.000005 instance=00c61b117c0b660d29c1e893219b346d423915 app_engine_release=1.9.

表示成功,因为状态码 200..

在此之后我的云 sql 数据库根本没有被查询,但是每当我在笔记本电脑上本地运行服务器时,一切正常,我的云 sql 得到连接。

它只是不适用于我的应用引擎。

为什么?我真的不明白问题出在哪里。

【问题讨论】:

  • 能否包含查询部分?
  • @jirungaray 查询在 servlet 代码中。或者您指的是哪个查询?

标签: java mysql google-app-engine servlets google-cloud-sql


【解决方案1】:

我知道出了什么问题..

首先,当从应用引擎连接到云 sql 数据库时,我打算使用 root 作为我的用户名。

其次,我打算将 App Engine 项目标题从它的默认名称更改为我想给它的名称..(我仍然不知道为什么这会影响它)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    相关资源
    最近更新 更多