- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Connection getClientInfo() method with example
The getClientInfo() method of the Connection interface returns the name and values of the client info properties of the current connection. This method returns a properties object.
To retrieve the values of the client info properties file.
Register the driver using the registerDriver() method of the DriverManager class as −
//Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Get the connection using the getConnection() method of the DriverManager class as −
//Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");
Create a properties object as −
Properties properties = new Properties();
Add the required key-value pairs to the above created Properties object as −
properties.put("user_name", "new_user"); properties.put("password", "password");
Set the above created properties to the client-info using the setClientInfo() method as −
//Setting the Client Info con.setClientInfo(properties);
Retrieve the Properties file (object) using the getClientInfo() method and get properties from it by invoking the getProperty() method as −
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");
Following JDBC program establishes connection with the MYSQL database and sets the credentials of a new user to the client info properties file.
Example
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 { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password"); System.out.println("Connection established......"); //Adding the credentials of another user to the properties file Properties properties = new Properties(); properties.put("user_name", "new_user"); properties.put("password", "my_password"); //Setting the ClientInfo con.setClientInfo(properties); //Retrieving the values in the ClientInfo properties file Properties prop = con.getClientInfo(); System.out.println("user name: "+prop.getProperty("user_name")); System.out.println("password: "+prop.getProperty("password")); } }
Output
Connection established...... user name: new_user password: my_password