Here you can find the source of getConnection()
Parameter | Description |
---|---|
Exception | if any error occurs |
public static Connection getConnection() throws Exception
//package com.java2s; import java.io.FileInputStream; import java.sql.Connection; import java.sql.DriverManager; import java.util.Properties; public class Main { /**//from ww w . j av a2s . co m * Represents the database configure file path. */ private static final String DATABASE_PROPERTY_FILE_PATH = "./config/application.properties"; /** * Creates a sql connection. * * @return the created sql connection * @throws Exception if any error occurs */ public static Connection getConnection() throws Exception { //gets connection properties from file database.properties Properties prop = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream(DATABASE_PROPERTY_FILE_PATH); prop.load(fis); } finally { if (fis != null) { fis.close(); } } //creates the connection Class.forName(prop.getProperty("jdbc.driverClassName")); return DriverManager.getConnection(prop.getProperty("jdbc.url"), prop.getProperty("jdbc.username"), prop.getProperty("jdbc.password")); } }