What Is the Username of the Database Connection? JDBC

You can use DatabaseMetaData to get the name of the database user used in creating a connection object. The following snippet shows how:

import java.sql.Connection;
import java.sql.DatabaseMetaData;
Connection conn = null;
try {
conn = getConnection(); // returns a Connection
DatabaseMetaData dbMetaData = conn.getMetaData();
if (dbMetaData == null) {
System.out.prinln("database does not support metadata.");
// retrieve the user name as known to this database.
String user = dbMetaData.getUserName();
System.out.prinln("database user="+user);
catch(Exception e) {
// handle the exception

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics