如何确定给定JDBC连接的数据库类型?

我需要处理返回三个数据库(Oracle,sybase,MS-Server)的存储过程/函数的结果集。 程序/function通常是相同的,但在Oracle中调用有点不同。

statement.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR); ... statement.execute(); ResultSet rs = (ResultSet)statement.getObject(1); 

JDBC没有提供处理它的通用方法,因此我需要在代码中区分不同类型的DB。 我给了连接,但不知道确定数据库是否是oracle的最佳方法。 我可以使用驱动程序名称,但宁愿找到更清洁的方式。

我怀疑你会想要使用DatabaseMetaData类。 很可能DatabaseMetaData.getDatabaseProductName就足够了,但如果您的代码依赖于您正在使用的特定数据库的特定版本,则可能还需要使用getDatabaseProductVersion方法。

您可以使用org.apache.ddlutils,类Platformutils :

 databaseName = new PlatformUtils().determineDatabaseType(dataSource)