Advertisement
Guest User

Untitled

a guest
May 20th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.IOException;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. /**
  9. * A simple data source for getting database connections.
  10. */
  11. public class SimpleDataSource
  12. {
  13. /**
  14. * Initializes the data source.
  15. *
  16. * @param fileName
  17. * the name of the property file that contains the database
  18. * driver, URL, username, and password
  19. */
  20. public static void init(String fileName) throws IOException,
  21. ClassNotFoundException
  22. {
  23. Properties props = new Properties();
  24. FileInputStream in = new FileInputStream(fileName);
  25. props.load(in);
  26.  
  27. String driver = props.getProperty("jdbc.driver");
  28. url = props.getProperty("jdbc.url");
  29. username = props.getProperty("jdbc.username");
  30. if (username == null)
  31. username = "";
  32. password = props.getProperty("jdbc.password");
  33. if (password == null)
  34. password = "";
  35. if (driver != null)
  36. Class.forName(driver);
  37. }
  38.  
  39. /**
  40. * Gets a connection to the database.
  41. *
  42. * @return the database connection
  43. */
  44. public static Connection getConnection() throws SQLException
  45. {
  46. System.out.println("u: " + username + " p: " + password);
  47. if (username.equals(""))
  48. {
  49. return DriverManager.getConnection(url);
  50. }
  51. else
  52. {
  53. return DriverManager.getConnection(url, username, password);
  54. }
  55. }
  56.  
  57. private static String url;
  58. private static String username;
  59. private static String password;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement