This is what I have inside the properties file:
hostname=localhost
port=1521
database=xe
username=xxxxxxx
password=xxxxxxx
jndi =dbp_props
And here the code
package ConexionBD;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class Conexion {
static Properties props = new Properties();
String hostname = null;
String port = null;
String database = null;
String username = null;
String password = null;
Connection cn = null;
public Connection CreateConnection(){
InputStream in = null;
try{
in = Files.newInputStream( FileSystems.getDefault().getPath("C:\Users\usuario\Desktop\Aplicativos finales\bd_farmacia.properties") );
props.load(in);
in.close();
} catch (IOException ex){
ex.printStackTrace();
} finally{
try{
in.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
CargarProperties();
return null;
}
public void CargarProperties(){
hostname =props.getProperty("hostname");
port = props.getProperty("port");
database = props.getProperty("database");
username = props.getProperty("username");
password = props.getProperty("password");
}
public Connection getConnection() throws SQLException{
String jdbcUrl = "jdbc:oracle:thin:@" + this.hostname + ":" + this.port + ":" + this.database;
cn = DriverManager.getConnection(jdbcUrl, username, password);
System.out.println("Conexion establecida");
return cn;
}
}