Java DatabaseMetaData .getUserName ()
Syntax
DatabaseMetaData.getUserName() has the following syntax.
String getUserName() throws SQLException
Example
In the following code shows how to use DatabaseMetaData.getUserName() method.
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
/*from w w w . j ava 2 s. co m*/
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getHSQLConnection();
DatabaseMetaData md = conn.getMetaData();
System.out.println("JDBC Driver: " + md.getDriverName() + " "
+ md.getDriverVersion());
System.out.println("Database: " + md.getURL());
System.out.println("User: " + md.getUserName());
conn.close();
}
private static Connection getHSQLConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
The code above generates the following result.