Accessing a local MySQL instance in your GAE development environment for java using eclipse

不想你离开。 提交于 2020-01-23 04:06:57

问题


I am new to Google App Engine environment. We are starting a project where we are using Google Cloud SQL. For testing purpose we need to setup a local MySQL instance. I have tried searching for the answer, but I didn't find any that helped me.

If I was to summarize my question, I am trying access a local MySQL instance in my GAE development environment using JAVA in Eclipse.


回答1:


You have to add the MySQL connector in you App Engine SDK folder.

You can find the connector there: http://dev.mysql.com/downloads/connector/j/. Then, you have to place it in this folder: appengine-java-sdk\lib\impl

Then you have to run a local version of MySQL (for example using EasyPHP).

Here is a sample of the code that you could use to connect to your database (singleton) :

public static Connection getInstance() throws Exception {
  if (connection != null && !connection.isClosed()) {
    return connection;
  }
  if (isLocalTesting) {
    //MySQL
    String url = "jdbc:mysql://127.0.0.1:3306/YOUR_DB_NAME";
    connection = DriverManager.getConnection(url, "root", "");
  } else {
    // Google Cloud SQL
    DriverManager.registerDriver(new AppEngineDriver());
    connection = DriverManager.getConnection("jdbc:google:rdbms://" + instanceName + "/NAME_DB");
  }
  return connection;
}

And lastly: You have to include the MySQL library in your build path as well: http://prntscr.com/124jwm




回答2:


I use Eclipse Juno and install Google App Engine SDK and plugin. There is configuration for development mysql and google cloud SQL instance. its automaticaly go to your local when development and go to cloud SQL when you deploy it.



来源:https://stackoverflow.com/questions/16220409/accessing-a-local-mysql-instance-in-your-gae-development-environment-for-java-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!