Java Connection getCatalog()方法与示例
通常,目录是一个目录,其中包含有关数据集,文件或数据库的信息。而数据库目录中包含所有数据库,基本表,视图(虚拟表),同义词,值范围,索引,用户和用户组的列表。
Connection接口的getCatalog()方法返回当前连接对象的当前目录/数据库的名称。
此方法返回代表目录名称的Sting值。如果没有目录,则返回null。
获取目录名称-
使用registerDriver()DriverManager类的方法将驱动程序注册为-
//注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver());
使用getConnection()DriverManager类的方法获取连接,如下所示:
//获得连接 String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");
使用以下getCatalog()方法检索连接对象目录名称:
//检索当前目录名称 String catalogName = con.getCatalog();
让我们使用CREATE语句在MySQL中创建一个名为mydatabase的数据库,如下所示。
create database mydatabase;
以下JDBC程序建立与MySQL数据库的连接,检索并显示基础目录的名称。
示例
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connection_getCatalog {
public static void main(String args[]) throws SQLException {
//注册驱动程序
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//获得连接
String url = "jdbc:mysql://localhost/mydatabase";
Connection con = DriverManager.getConnection(url, "root", "password");
System.out.println("Connection established......");
//将自动提交设置为false-
con.setAutoCommit(false);
//检索当前目录名称
String catalogName = con.getCatalog();
System.out.println("Current catalog name is: "+catalogName);
}
}输出结果
Connection established...... Current catalog name is: mydatabase
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下