Get database connection URL in Java
Description
The following code shows how to get database connection URL.
Example
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
// w w w .j a v a 2 s.c o m
public class Main {
public static void main(String[] args) throws Exception {
Connection conn = getConnection();
DatabaseMetaData mtdt = conn.getMetaData();
System.out.println("URL in use: " + mtdt.getURL());
conn.close();
}
private static Connection getConnection() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
String url = "jdbc:hsqldb:mem:data/tutorial";
return DriverManager.getConnection(url, "sa", "");
}
}
The code above generates the following result.