如何获取SQL连接的默认模式? [英] How to get the default schema of a SQL connection?

查看:113
本文介绍了如何获取SQL连接的默认模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从java代码中我已经连接到数据库 - 我需要找到连接的默认模式。

From within a java code - where I already have a connection to a database - I need to find the default schema of the connection.

我有以下代码,给出了该连接的所有模式的列表。

I have the following code that gives me a list of all schemas of that connection.

rs  = transactionManager.getDataSource().getConnection().getMetaData().getSchemas();
while (rs.next()) {
    log.debug("The schema is {} and the catalogue is {} ", rs.getString(1), rs.getString(2));
}

但是,我不想要所有模式的列表。我需要这个连接的默认模式。

However, I don't want the list of all the schemas. I need the default schema of this connection.

请帮忙。

注1:我在Windows7(dev盒)和Linux Redhat(生产盒)上使用H2和DB2

Note1: I am using H2 and DB2 on Windows7 (dev box) and Linux Redhat (production box)

Note2:我终于得出结论,使用Java中的Connections对象不可能使用相同的代码来查找H2和DB2的默认模式。我修复了一个配置文件的问题。然而,如果有人可以共享一个解决方案,我可以返回并重构代码。

Note2: I finally concluded that it was not possible to use the Connections object in Java to find the default schema of both H2 and DB2 using the same code. I fixed the problem with a configuration file. However, if someone can share a solution, I could go back and refactor the code.

推荐答案

请使用 connection.getMetaData()。getURL()它返回字符串像

jdbc:mysql://localhost:3306/?autoReconnect=true&useUnicode=true&characterEncoding=utf8

我们可以轻松地解析它,并获取模式名称。它适用于所有JDBC驱动程序。

We can parse it easily and get the schema name. It works for all JDBC drivers.

这篇关于如何获取SQL连接的默认模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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