无法获得与 PostgreSQL 的 JDBC 连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34816862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:08:15  来源:igfitidea点击:

Unable to get JDBC connection with PostgreSQL

javapostgresqljdbc

提问by Manoj Suthar

I am trying to get a JDBC connection to PostgreSQL. Driver version in dependency is: 9.4-1204-jdbc42 and Postgres version is 9.5.0. Following is the stack:

我正在尝试与 PostgreSQL 建立 JDBC 连接。依赖驱动程序版本为:9.4-1204-jdbc42,Postgres 版本为 9.5.0。以下是堆栈:

Caused by: java.lang.UnsupportedClassVersionError: org/postgresql/Driver : Unsupported major.minor version 52.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access0(URLClassLoader.java:71)
    at java.net.URLClassLoader.run(URLClassLoader.java:361)
    at java.net.URLClassLoader.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName(DriverManagerDataSource.java:127)
    at com.biginfolabs.ipmonitor.core.config.HibernateConfiguration.dataSource(HibernateConfiguration.java:41)
    at com.biginfolabs.ipmonitor.core.config.HibernateConfiguration$$EnhancerByCGLIB$$a2cde1cc.CGLIB$dataSource(<generated>)
    at com.biginfolabs.ipmonitor.core.config.HibernateConfiguration$$EnhancerByCGLIB$$a2cde1cc$$FastClassByCGLIB$817301.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:326)
    at com.biginfolabs.ipmonitor.core.config.HibernateConfiguration$$EnhancerByCGLIB$$a2cde1cc.dataSource(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 49 more

Please suggest what is the issue here?

请建议这里有什么问题?

回答by Christoph-Tobias Schenke

"unsupported major.minor version 52" says that you must use java8 and are running in java7 or lower.

“不支持的major.minor 版本52”表示您必须使用java8 并且在java7 或更低版本中运行。

according to https://jdbc.postgresql.org/download.html

根据https://jdbc.postgresql.org/download.html

9.4-1204-jdbc42 is the driver compiled in java8.

9.4-1204-jdbc42是java8编译的驱动。

9.4-1204-jdbc41 is the driver compiled in java7.

9.4-1204-jdbc41是java7编译的驱动。

9.4-1204-jdbc4 is the driver compiled in java6.

9.4-1204-jdbc4是java6编译的驱动。

回答by nos

The PostgreSQL driver you're using is built for Java 1.8, but you're not running Java 1.8 - probably you're running Java 1.7

您使用的 PostgreSQL 驱动程序是为 Java 1.8 构建的,但您运行的不是 Java 1.8 - 可能您运行的是 Java 1.7

Either upgrade to Java 1.8 or use the postgresql-9.4.1207.jre7.jar file that works with Java 1.7 (as you can see here)

无论是升级到Java 1.8或经常使用PostgreSQL,9.4.1207.jre7.jar文件与Java 1.7工程(你可以看到这里