java.sql.SQLFeatureNotSupportedException: 这个 org.postgresql.jdbc4.Jdbc4Statement.setQueryTimeout(int) 方法尚未被实作。

发布时间 2023-08-04 16:48:21作者: 剑道第一仙

java jdbc连接pg库报错:

八月 04, 2023 4:32:08 下午 com.alibaba.druid.pool.DruidDataSource error

严重: init datasource error, url: jdbc:postgresql://xxxx/xxxx
java.sql.SQLFeatureNotSupportedException: 这个 org.postgresql.jdbc4.Jdbc4Statement.setQueryTimeout(int) 方法尚未被实作。
at org.postgresql.Driver.notImplemented(Driver.java:753)
at org.postgresql.jdbc2.AbstractJdbc2Statement.setQueryTimeout(AbstractJdbc2Statement.java:668)
at com.alibaba.druid.pool.vendor.PGValidConnectionChecker.isValidConnection(PGValidConnectionChecker.java:66)
at com.alibaba.druid.pool.DruidAbstractDataSource.validateConnection(DruidAbstractDataSource.java:1393)
at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1727)
at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:914)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1382)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:1378)
at com.alibaba.druid.pool.DruidDataSource.getConnection(DruidDataSource.java:99)
at com.qsds.liujun.util.JDBCManagerNew.main(JDBCManagerNew.java:47)

异常出现场景:在使用java操作postgresql数据库时。

 

出现原因:Jdbc4Statement类里面的需要实现setQueryTimeout 方法,但此处没有实现。该方法是用来设置查询超时时间的。postgresql驱动版本与druid版本不兼容,可以试试降低durid版本或提升postgresql版本。

解决办法:

修改前:
<!-- 数据库连接池 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.2.0</version>
    </dependency>
    <!-- postgresql JDBC -->
      <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>
修改后:
<!-- 数据库连接池 -->
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.0.0</version>
    </dependency>
    <!-- postgresql JDBC -->
      <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.1-901-1.jdbc4</version>
    </dependency>