Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. package db;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.util.Properties;
  7.  
  8. public class DBConnection {
  9.  
  10. private static DBConnection dbconnection;
  11. private Connection connection;
  12. private static final String dbName = "School";
  13. private static final String serverAdress = "localhost";
  14. private static final int serverPort = 1433;
  15.  
  16. private DBConnection() {
  17. String connectionString = String.format("jdbc:sqlserver://%s:%d;databaseName=%s;user=%s;password=%s",serverAdress,serverPort,dbName);
  18. Properties connectionProps = getConnectionProperties();
  19.  
  20. try {
  21. connection = DriverManager.getConnection(connectionString,connectionProps);
  22. } catch (SQLException e) {
  23. System.err.println("Could not connect to databse " + dbName);
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. public static DBConnection getInstance() {
  29. if (dbconnection == null) {
  30. dbconnection = new DBConnection();
  31. }
  32. return dbconnection;
  33. }
  34.  
  35. public Connection getConnection(){
  36. return connection;
  37. }
  38.  
  39. private Properties getConnectionProperties(){
  40. Properties connectionProps = new Properties();
  41. connectionProps.put("user","sa");
  42. connectionProps.put("password", "secret");
  43. return connectionProps;
  44. }
  45.  
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement