Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- mysql dabase url, username and password in file
- Properties config = new Properties();
- config.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties"));
- String url = config.getProperty("url");
- String username = config.getProperty("username");
- String password = config.getProperty("password");
- // ...
- /**
- * Function that connects to a DB and data fro connection is loaded from a properties file
- * @param fileName
- * @return the connection to DB or null if any problem
- */
- public java.sql.Connection connect_to_database_from_properties(String fileName)
- {
- Properties prop = new Properties();
- InputStream is;
- try {
- is = new FileInputStream(fileName);
- prop.load(is);
- String url=prop.getProperty("url");
- String un=prop.getProperty("username");
- String pass=prop.getProperty("password");
- System.out.println("URL: "+url+" UN: "+un+" PASS: "+pass);
- is.close();
- Class.forName("com.mysql.jdbc.Driver");
- java.sql.Connection conection =DriverManager.getConnection(url, un, pass);
- return conection;
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return null;
- }
Add Comment
Please, Sign In to add comment