谷歌云sql没有与我的项目(Java)连接

时间:2016-10-16 09:46:55

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

LoginAuthServlet.java

            Class.forName("com.mysql.jdbc.GoogleDriver");
            String url="jdbc:google:mysql://Instance_connection_name/dbname?user=xyz&password=abc";
            Connection conn = DriverManager.getConnection(url);

            String query = "SELECT  "
                        + "  * "
                        + " FROM "
                        + "  users "
                        + " WHERE "
                        + "   user_username = ? AND user_password = ? AND user_status = '1' "
                        /*+ "     user_type = ? "*/
                        + " limit 0,1";
                PreparedStatement stmtLogin = conn.prepareStatement(query);
                stmtLogin.setString(1, username);
                stmtLogin.setString(2, userPassword);

                ResultSet resource = stmtLogin.executeQuery();
                if(resource.next()){
                        HttpSession sess = req.getSession();
                        sess.setAttribute("isLogin", "true");

                        sess.setAttribute("userId", resource.getString("user_id"));
                        sess.setAttribute("userName", resource.getString("user_username"));
                        sess.setAttribute("userType", resource.getString("user_type"));
                        sess.setMaxInactiveInterval(30*60);
                        resp.sendRedirect("BusinessList.jsp"); 
                    }

应用服务引擎-web.xml中

 <use-google-connector-j>true</use-google-connector-j> 

当我部署这个项目并尝试登录然后给出响应是{} ..... 我包括所有jar文件,如果我尝试在本地连接IPV4地址然后它运作良好,但如果我部署项目然后它不起作用,我做了所有的事情,但我的项目无法与谷歌云sql连接。 / p>

1 个答案:

答案 0 :(得分:0)

如果您使用的是第二代实例,为了从appengine连接,您还需要包含区域JDBC url,如下所示:

String url="jdbc:google:mysql://[PROJECT-ID]:[REGION]:[INSTANCE-NAME]/dbname?user=xyz&password=abc";

这里他们提供了一些例子: https://cloud.google.com/appengine/docs/java/cloud-sql/