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.");
System.exit(0);
}
// 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
e.printStackTrace();
}


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

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

JDBC Topics