Guest User

Untitled

a guest
Mar 18th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. package com.cts.mage.connection;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.SQLException;
  12. import java.util.Properties;
  13.  
  14. public class DBConnection {
  15.  
  16. public static Connection getConnection(){
  17. Connection conn=null;
  18. InputStream is = null;
  19. Properties prop = null;
  20.  
  21. prop = new Properties();
  22. try {
  23. is = new FileInputStream(new File("f:\\mylan\\dbconfig.properties"));
  24. prop.load(is);
  25. String db = prop.getProperty("db");
  26. String url = prop.getProperty("url");
  27. String username = prop.getProperty("username");
  28. String password = prop.getProperty("password");
  29.  
  30. Class.forName("com.mysql.jdbc.Driver");
  31. conn = DriverManager.getConnection((url+db),username,password);
  32. } catch (Exception e)
  33. {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37.  
  38.  
  39. return conn ;
  40. }
  41. }
  42.  
  43. --------------dbconfig.properties
  44.  
  45. url=jdbc:mysql://localhost:3306/
  46. db=mylan
  47. username=root
  48. password=root
Add Comment
Please, Sign In to add comment