
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
In this article, we will learn how to retrieve and set client information properties in a MySQL database connection using the getClientInfo() method from the Connection interface in JDBC. The program demonstrates how to establish a connection to a database, set custom user credentials as client information properties, and then retrieve and display those values.
Steps to use the Java Connection getClientInfo() method
Following are the steps to use the Java Connection getClientInfo() method ?
- Register the MySQL driver using the DriverManager.registerDriver() method.
- Establish a connection to the MySQL database using DriverManager.getConnection().
- Create a Properties object and add custom user credentials (username and password) to it.
- Use the setClientInfo() method to set the properties for the connection.
- Retrieve the client info properties using the getClientInfo() method.
- Print the username and password stored in the client info properties.
Demonstrating Java Connection getClientInfo() with code
Below is the demonstration of Java Connection getClientInfo() with code ?
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
Code Explanation
First, the MySQL JDBC driver is registered using DriverManager.registerDriver(). A connection to the MySQL database is established using getConnection(), where the database URL, username, and password are provided. A Properties object is created to store new credentials (username and password), which are added using put(). These properties are then set as the client info for the connection using setClientInfo(). Finally, the program retrieves the client info properties using getClientInfo() and prints the username and password. This allows the program to handle different user credentials dynamically within the connection.