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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/254213/
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-09-18 17:26:07  来源:igfitidea点击:

How to determine database type for a given JDBC connection?

javaoraclejdbc

提问by Justin Cave

I need to handle resultsets returning stored procedures/functions for three databases (Oracle, sybase, MS-Server). The procedures/functions are generally the same but the call is a little different in Oracle.

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

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

JDBC doesn't provide a generic way to handle this, so I'll need to distinguish the different types of DBs in my code. I'm given the connection but don't know the best way to determine if the DB is oracle. I can use the driver name but would rather find a cleaner way.

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

回答by Justin Cave

I suspect you would want to use the DatabaseMetaData class. Most likely DatabaseMetaData.getDatabaseProductNamewould be sufficient, though you may also want to use the getDatabaseProductVersion method if you have code that depends on the particular version of the particular database you're working with.

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

回答by Rinat Tainov

You can use org.apache.ddlutils, class Platformutils:

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

databaseName = new PlatformUtils().determineDatabaseType(dataSource)