我有一个方法,可以将记录插入到Postgres数据库中,并返回为该记录生成的标识字段。问题是,如果我在POM文件中包含Redshift驱动程序,那么该驱动程序将被使用,而不是Postgres驱动程序 - 而Redshift驱动程序不允许返回标识值。
代码如下:
什么原因使Java选择Redshift驱动而不是Postgres驱动?(Redshift驱动的类路径为com.amazon.jdbc41.Driver,因此我认为这不是类路径冲突)谢谢。
代码如下:
try {
Class.forName( "org.postgresql.Driver" ).newInstance();
Connection connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5433/postgres", "postgres", "password" );
Statement stmt = connection.createStatement();
stmt.execute( "insert into public.job ( job_name ) values ( 'test' )" , Statement.RETURN_GENERATED_KEYS );
ResultSet keyset = stmt.getGeneratedKeys();
if ( keyset.next() ) System.out.println( keyset.getLong( 1 ) );
}
catch ( Exception e ) {
e.printStackTrace();
}
当使用此POM时,它可以正常工作:
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
</dependencies>
当使用这个POM时,它无法正常工作:
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>redshift.jdbc</artifactId>
<version>1.1.2.0002</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
</dependency>
</dependencies>
什么原因使Java选择Redshift驱动而不是Postgres驱动?(Redshift驱动的类路径为com.amazon.jdbc41.Driver,因此我认为这不是类路径冲突)谢谢。