Last active
August 29, 2015 14:17
-
-
Save faloi/a5aab5d06cd3f66cb63d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.unq.epers.rentauto.dbutils | |
import java.io.FileInputStream | |
import java.io.FileNotFoundException | |
import java.util.Properties | |
import org.eclipse.xtend.lib.annotations.Accessors | |
@Accessors | |
class Credentials { | |
String user | |
String password | |
new(String user, String password) { | |
this.user = user | |
this.password = password | |
} | |
def static loadFromFile(String fileName) { | |
try { | |
val in = new FileInputStream(fileName) | |
val props = new Properties() | |
props.load(in) | |
in.close() | |
new Credentials(props.getProperty("jdbc.username"), props.getProperty("jdbc.password")) | |
} catch (FileNotFoundException e) { | |
throw new CredentialsNotFoundException(e, fileName) | |
} | |
} | |
} | |
class CredentialsNotFoundException extends RuntimeException { | |
new(Exception e, String fileName) { | |
super(''' | |
No se encontro el archivo «fileName» con las credenciales! | |
Por favor crealo con el siguiente formato: | |
jdbc.username=root | |
jdbc.password=tupassword | |
''', e) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jdbc.user=root | |
jdbc.password=tupassword |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.unq.epers.rentauto | |
import java.sql.DriverManager | |
import org.unq.epers.grupo5.rentauto.dbutils.Credentials | |
class Main { | |
def static void main(String[] args) { | |
Class.forName("com.mysql.jdbc.Driver") | |
val credentials = Credentials.loadFromFile("src/main/resources/db.properties") | |
val connection = DriverManager.getConnection("jdbc:mysql://localhost/mibasededatos", credentials.user, credentials.password) | |
// hacer algo interesante con la connection | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment