Skip to content

Instantly share code, notes, and snippets.

@faloi
Last active August 29, 2015 14:17
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 faloi/a5aab5d06cd3f66cb63d to your computer and use it in GitHub Desktop.
Save faloi/a5aab5d06cd3f66cb63d to your computer and use it in GitHub Desktop.
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)
}
}
jdbc.user=root
jdbc.password=tupassword
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