如何确定给定JDBC连接的数据库类型? [英] How to determine database type for a given JDBC connection?

查看:112
本文介绍了如何确定给定JDBC连接的数据库类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

JDBC没有提供处理这种情况的通用方法,因此我需要在代码中区分不同类型的数据库.我已获得连接,但不知道确定数据库是否为oracle的最佳方法.我可以使用驱动程序名称,但宁愿找到一种更简洁的方法.

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.

推荐答案

我怀疑您想使用DatabaseMetaData类. DatabaseMetaData.getDatabaseProductName 这样就足够了,尽管如果您的代码依赖于所使用的特定数据库的特定版本,那么您可能还想使用getDatabaseProductVersion方法.

I suspect you would want to use the DatabaseMetaData class. Most likely DatabaseMetaData.getDatabaseProductName would 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.

这篇关于如何确定给定JDBC连接的数据库类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆