[英]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:
一般来说:
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.