我已经设法从Java连接到我的Google Cloud SQL数据库。 目前我一直在通过以下方式验证:
Connection conn = DriverManager.getConnection(getUrl());然后从中检索准备好的语句,依此类推。 getUrl()是一种方法,它以Google指定的格式返回网址:“jdbc:google:mysql:// xxx / xxx?user = xxx&password = xxx”。
我想在我的数据库中使用JPA。 查看Google的文档,他们似乎推荐使用WTP aka Eclipse Web Tools Platform( https://developers.google.com/eclipse/docs/cloudsql-jpatools )。 我不使用Eclipse,我真的想避免使用这个工具,通常应该很容易实现,没有任何gui / wizard / plugin-thingy。
我的Java servlet是用Spring编写的,我的app配置看起来像这样:
@EnableWebMvc @EnableScheduling @EnableTransactionManagement @EnableJpaRepositories("stuff") @ComponentScan("stuff") @Configuration public class AppConfig { @Bean public DataSource dataSource() { DriverManagerDataSource driver = new DriverManagerDataSource(); driver.setDriverClassName("org.postgresql.Driver"); //Obviously not working for Google Cloud SQL driver.setUrl(Config.getUrl()); // Same url as described above. driver.setUsername(/*omitted*/); driver.setPassword(/*omitted*/); return driver; } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } @Bean public EntityManagerFactory entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("stuff"); factory.setPersistenceUnitName("PU"); factory.setDataSource(dataSource()); factory.afterPropertiesSet(); return factory.getObject(); } }我的persistence.xml看起来像这样:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="PU"> <class>stuff.Message</class> <properties> <property name="javax.persistence.jdbc.user" value="root" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> </properties> </persistence-unit> </persistence>有什么方法可以修改我的AppConfig.java + persistence.xml以使其与Google Cloud SQL一起使用? 如上所述,我想跳过Eclipse插件。
I've managed to connect to my Google Cloud SQL database from Java. Currently I've been verifying this by going like this:
Connection conn = DriverManager.getConnection(getUrl());Then retrieving a prepared statement from that and so forth. getUrl() is a method that returns the url in the format that Google are specifying: "jdbc:google:mysql://xxx/xxx?user=xxx&password=xxx".
I would like to use JPA with my database. Looking at Google's documentation for this, they seem to recommend WTP a.k.a. Eclipse Web Tools Platform (https://developers.google.com/eclipse/docs/cloudsql-jpatools). I don't use Eclipse and I would really like to avoid using this tool for something that normally should be very easy to achieve without any gui/wizard/plugin-thingy.
My Java servlet is written in Spring and my app config looks something like this:
@EnableWebMvc @EnableScheduling @EnableTransactionManagement @EnableJpaRepositories("stuff") @ComponentScan("stuff") @Configuration public class AppConfig { @Bean public DataSource dataSource() { DriverManagerDataSource driver = new DriverManagerDataSource(); driver.setDriverClassName("org.postgresql.Driver"); //Obviously not working for Google Cloud SQL driver.setUrl(Config.getUrl()); // Same url as described above. driver.setUsername(/*omitted*/); driver.setPassword(/*omitted*/); return driver; } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory(entityManagerFactory()); return txManager; } @Bean public EntityManagerFactory entityManagerFactory() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl(true); LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setJpaVendorAdapter(vendorAdapter); factory.setPackagesToScan("stuff"); factory.setPersistenceUnitName("PU"); factory.setDataSource(dataSource()); factory.afterPropertiesSet(); return factory.getObject(); } }My persistence.xml is looking something like this:
<?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"> <persistence-unit name="PU"> <class>stuff.Message</class> <properties> <property name="javax.persistence.jdbc.user" value="root" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/> </properties> </persistence-unit> </persistence>Is there any way that I could modify my AppConfig.java+persistence.xml to make this work with Google Cloud SQL? As mentioned I would love skipping the Eclipse Plugin.
最满意答案
如果您使用的是AppEngine,可能需要阅读https://cloud.google.com/sql/docs/app-engine-connect和https://cloud.google.com/appengine/docs/java/cloud-sql / 。
在这里,您似乎尝试在不使用Google Cloud SQL驱动程序的情况下连接到Google Cloud SQL实例,但使用需要此驱动程序的Google连接字符串,如示例中所述:
// Load the class that provides the new "jdbc:google:mysql://" prefix. Class.forName("com.mysql.jdbc.GoogleDriver");因此无法识别您的连接字符串(并且PostgreSQL驱动程序没有帮助)。
如果您没有使用AppEngine,请切换到MySQL驱动程序并删除连接字符串中的“google”,就像使用标准MySQL连接(使用主机,端口等而不是实例名称)一样。
If you are using AppEngine, you might want to read https://cloud.google.com/sql/docs/app-engine-connect and https://cloud.google.com/appengine/docs/java/cloud-sql/.
Here, it seems you're trying to connect to Google Cloud SQL instance without using the Google Cloud SQL Driver, but with a Google connection string which requires this driver, as explained in their sample:
// Load the class that provides the new "jdbc:google:mysql://" prefix. Class.forName("com.mysql.jdbc.GoogleDriver");So your connection string is not recognised (and the PostgreSQL driver doesn't help).
If you're not using AppEngine, switch to a MySQL driver and remove "google" in your connection string, as you would do with a standard MySQL connection (with host, port, etc. instead of instance name).
更多推荐
发布评论