java info()方法_Java Connection getClientInfo()方法与示例

Connection接口的getClientInfo()方法返回当前连接的客户端信息属性的名称和值。此方法返回一个属性对象。

检索客户端信息属性文件的值。

使用DriverManager类的registerDriver()方法将驱动程序注册为-//注册驱动程序

DriverManager.registerDriver(new com.mysql.jdbc.Driver());

使用DriverManager类的getConnection()方法获取连接,如下所示://获得连接

String url = "jdbc:mysql://localhost/mydatabase";

Connection con = DriverManager.getConnection(url, "root", "password");

创建一个属性对象为-Properties properties = new Properties();

将所需的键值对添加到上面创建的Properties对象中,如下所示:properties.put("user_name", "new_user");

properties.put("password", "password");

使用setClientInfo()方法将上述创建的属性设置为client-info,如下所示://Setting the Client Info

con.setClientInfo(properties);

使用getClientInfo()方法检索属性文件(对象),并通过以以下getProperty()方式调用方法从文件中获取属性:Properties prop = con.getClientInfo();

prop.getProperty("user_name");

prop.getProperty("password");

You can also directly get the properties as:

con.getClientInfo("user_name");

con.getClientInfo("password");

以下JDBC程序建立与MYSQL数据库的连接,并将新用户的凭据设置为客户端信息属性文件。

示例import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

public class Connection_getClientInfo {

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......");

//将另一个用户的凭证添加到属性文件

Properties properties = new Properties();

properties.put("user_name", "new_user");

properties.put("password", "my_password");

//设置ClientInfo-

con.setClientInfo(properties);

//检索ClientInfo属性文件中的值

Properties prop = con.getClientInfo();

System.out.println("user name: "+prop.getProperty("user_name"));

System.out.println("password: "+prop.getProperty("password"));

}

}

输出结果Connection established......

user name: new_user

password: my_password

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值