我无法从Google应用引擎上的已部署后端servlet连接到我的Google云端数据库 [英] I can't connect to my google cloud sql database from my deployed backend servlet on the google app engine

查看:118
本文介绍了我无法从Google应用引擎上的已部署后端servlet连接到我的Google云端数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



以下是我在下面所做的事情:

这是我的servlet代码:

  public class MyServlet extends HttpServlet {
@Override $ b $ public void doGet(HttpServletRequest req,HttpServletResponse resp)
throws IOException {
resp.setContentType(文本/无格式);
resp.getWriter()。println(请使用表单发布到这个URL);

$ b @Override
public void doPost(HttpServletRequest req,HttpServletResponse resp)
抛出IOException {

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

尝试{
if(SystemProperty.environment.value()==
SystemProperty.Environment.Value.Production){
//从App Engine进行连接。

//载入提供新的jdbc:google:mysql://前缀的类。
Class.forName(com.mysql.jdbc.GoogleDriver);
url =jdbc:google:mysql:// festive-shark-90408:daily-joy-project / database?user = joys;

} else {
//开发过程中使用的本地MySQL实例。
Class.forName(com.mysql.jdbc.Driver);
url =jdbc:mysql://< cloud-sql-instance-ip>:3306 / database?user = joys;

//或者,使用以下命令连接到Google Cloud SQL实例:

$ b catch(Exception e){
e.printStackTrace() ;

}


尝试{
Connection conn = DriverManager.getConnection(url);
尝试{

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


$ String $ =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>成功!重定向3秒.. < /体>< / HTML>中);
} else if(success == 0){
out.println(
< html>< head>< / head>< body>失败!请重试! +
重定向3秒...< / body>< / html>);
}



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



}

}

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

应用程序值:festive-shark-90408



版本值:app-001

线程安全值:true

use-google-connector-j value:true

系统属性:

  property name =java.util.logging.config.filevalue =WEB-INF / logging.properties

我的android studio使用JDK 1.7.0_72



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



我在我的云端sql中设置了Access控制权限,以授权我的应用程序引擎应用程序并授权我的网络ip。



我已经成功地部署了我的后端servlet,无论何时向servlet发出发布请求,我的应用程序引擎日志都只显示这一点:



203.106.176.214 - - [04 / Apr / 2015:04:43:17 -0700]POST / hel lo HTTP / 1.1200 45 - Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 41.0.2272.118 Safari / 537.36festive-shark-90408.appspot.comms = 39 cpu_ms = 76 cpm_usd = 0.000005 instance = 00c61b117c0b660d29c1e893219b346d423915 app_engine_release = p>

这意味着它已成功,因为状态码为200 ..



我的云端sql数据库完全没有被查询这一点,但每当我在我的笔记本电脑本地运行服务器,一切正常,我的云计算SQL获取连接。



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



为什么?我真的不明白问题来自哪里。

解决方案

我找出了什么是错误的。首先,当我连接到应用程序引擎的云数据库时,我的意思是使用root作为我的用户名。

其次,我的意思是将App Engine项目标题从其默认名称更改为我想给它的名称..(我仍然不知道w这是影响它)


I have done everything that is required of me and my deployed backend servlet on the google app engine isn't making any connection to my google cloud database and the worst part of it, is that, my app engine log isn't even showing any error at all, its showing "POST /hello HTTP/1.1" 200 46... I seriously don't understand what is going on here for the past 5days..

these are the things i have done below:

This is my servlet code:

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



    }

    }

In my appengine-web.xml, below are the parameters:

application value: festive-shark-90408

version value: app-001

threadsafe value: true

use-google-connector-j value: true

system-properties:

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

My android studio is using the JDK 1.7.0_72

My appengine sdk is appengine-java-sdk-1.9.18

I have set the Access control in my cloud sql to authorize my app engine application and also to authorize my network ip.

I already deployed my backend servlet successfully, whenever I make a post request to the servlet, my app engine log shows ONLY this:

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, like Gecko) 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.18

Meaning it was successfully because status code 200..

My cloud sql database isn't being queried at all after this, but whenever I run the server locally on my laptop, everything works fine and my cloud sql gets the connection.

It just doesn't work with my app engine.

WHY? I really don't understand where the problem is coming from.

解决方案

I figured out what was wrong..

First, I was meant to use root as my username when connecting to the cloud sql database from the app engine.

Secondly, I was meant to change the App Engine Project Title from its default name to a name i want to give it.. (I still don't know why this is affecting it)

这篇关于我无法从Google应用引擎上的已部署后端servlet连接到我的Google云端数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆