Skip to content

Instantly share code, notes, and snippets.

@brnrc
Created May 10, 2014 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brnrc/f46bd04e73d604d97127 to your computer and use it in GitHub Desktop.
Save brnrc/f46bd04e73d604d97127 to your computer and use it in GitHub Desktop.
Relative path to .properties file.
package com.timezone.jdbc;
import org.apache.commons.dbcp.BasicDataSource;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* Created by Bruno on 5/6/2014.
*/
public class DBCPDataSourceFactory {
public static DataSource getDataSource() {
Properties props = new Properties();
FileInputStream fis = null;
BasicDataSource ds = new BasicDataSource();
try {
// getClass().getResource("resources/db.properties").getPath();
fis = new FileInputStream("src/main/resources/db.properties");
props.load(fis);
} catch (IOException e) {
e.printStackTrace();
return null;
}
ds.setDriverClassName(props.getProperty("MYSQL_DB_DRIVER_CLASS"));
ds.setUrl(props.getProperty("MYSQL_DB_URL"));
ds.setUsername(props.getProperty("MYSQL_DB_USERNAME"));
ds.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));
return ds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment