简体   繁体   English

使用Google App Engine端点连接到Cloud SQL

[英]Connect to Cloud SQL with Google App Engine Endpoint

I am trying to connect my backend endpoint code to a google cloud sql database. 我正在尝试将我的后端端点代码连接到Google Cloud sql数据库。 I have succesfully made and created the database and the tables using MySQL workbench. 我已经使用MySQL工作台成功创建并创建了数据库和表。 When i try to insert a new user in the user table i get a java.lang.NullPointerException when executing the query. 当我尝试在用户表中插入新用户时,执行查询时出现java.lang.NullPointerException。 See the code below: 请参见下面的代码:

@ApiMethod(name="registerUser")
public User addUser(@Named("name") String name, @Named("email") String email, @Named("rotation") double rotation,
        @Named("p1email") String p1email, @Named("p2email") String p2email, @Named("password") String password) throws NotFoundException {
    //Check if already exists
    Connection conn = dbConnection.getConnection();
    User u = null;
    try {
        String query = "SELECT * FROM users WHERE email = '"+email+"'";
        java.sql.ResultSet rs = conn.createStatement().executeQuery(query);
        if(rs != null){
            throw new NotFoundException("User email is already used");
        }else{
            java.sql.PreparedStatement statement = conn.prepareStatement("INSERT INTO users (name,email,p1email,p2email,password,rotation) VALUES ( ?, ?, ?, ?, ?, ?)");
            statement.setString(1, name);
            statement.setString(2, email);
            statement.setString(3, p1email);
            statement.setString(4, p2email);
            statement.setString(5, password);
            statement.setDouble(6, rotation);
            statement.execute();
        }
        String query2 = "SELECT * FROM users WHERE email = '"+email+"'";
        java.sql.ResultSet rs2 = conn.createStatement().executeQuery(query2);


        if(rs2 != null){
            while(rs2.next()){
                u = new User(rs2.getInt("id"), name, email, p1email, p2email, password, rotation);
            }
        } 
    }catch (SQLException e){
        throw new NotFoundException(e);
    }
    return u;
}

The code to connect to the database is: 连接到数据库的代码是:

public java.sql.Connection getConnection(){
    try{
        Connection conn = DriverManager.getConnection(
          "jdbc:google:mysql://your-project-id:your-instance-name/database",
          "user", "password");
        }catch(Exception e){
            conn = null;
        }
    return conn;
}

I get the java.lang.NullPointerException in this sentence in the first code snippet: 我在第一个代码段的这句话中得到了java.lang.NullPointerException:

 java.sql.ResultSet rs = conn.createStatement().executeQuery(query);

The issue is most certainly that 'conn' is null by the time you get to this line. 最肯定的问题是,当您到达这行时,'conn'为空。 The reason for this being that an exception was thrown by DriverManager.getConnection and 'null' was returned explicitly by getConnection(). 原因是DriverManager.getConnection引发了异常,而getConnection()显式返回了“ null”。

The question is now why did DriverManager.getConnection throw an exception? 现在的问题是,为什么DriverManager.getConnection引发异常? Since you are swallowing all exceptions and not logging anything, you can only try and guess. 由于您吞没了所有异常并且没有记录任何内容,因此只能尝试猜测。 In general: 一般来说:

  1. Swallowing all exceptions is bad. 吞没所有异常是不好的。 You should only catch those exceptions which you intend to handle. 您应该只捕获要处理的那些异常。
  2. If you catch an exception you should usually log it. 如果捕获到异常,通常应将其记录下来。 In this case you need to know why DriverManager.getConnection threw an exception in the first place. 在这种情况下,您需要知道为什么DriverManager.getConnection首先引发异常。

So the next step is to refactor your code to make it more debugging-friendly, get the exception details from the logs and work from there. 因此,下一步是重构代码,使其更易于调试,从日志中获取异常详细信息,然后从那里开始工作。 If the reason is not apparent feel free to update the question with new info. 如果原因不明显,请随时使用新信息更新问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google APP Engine 和云 sql :: 无法在 Google 云 sql(我的 sql)中连接 Spring boot 应用程序 - Google APP Engine and cloud sql :: Unable to connect Spring boot app in Google cloud sql (my sql) 将Google App Engine Eclipse插件连接到Cloud SQL - Connect Google App Engine Eclipse Plugin to Cloud SQL 谷歌应用引擎云终端安全性 - google app engine cloud endpoint security Spring 启动应用程序在部署到 Google App Engine 时无法连接到 Google Cloud SQL (PostgreSQL) - Spring Boot app can't connect to Google Cloud SQL (PostgreSQL) when deploying to Google App Engine 无法从Google App Engine(春​​季启动应用)连接到Google Cloud SQL(第二代) - Can not connect to google cloud sql(second generation) from google app engine(spring boot app) Google Cloud SQL和Google App Engine - Google Cloud sql and Google app engine 在Eclipse中进行开发时无法从Google App Engine连接到Google Cloud SQL实例 - Unable to connect to Google Cloud SQL instance from Google App Engine when developing in Eclipse 由于连接重置,Google App Engine生成Cloud Endpoint失败 - Google App Engine Generating Cloud Endpoint failed due to connection reset 部署后的Google App Engine和Cloud SQL - Google App Engine and Cloud SQL when deployed 需要帮助以使用Eclipse和GWT App Engine连接到Google Cloud SQL - Need help to connect to google Cloud SQL using Eclipse and GWT App Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM