Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. package DBLayer;
  2.  
  3. import java.sql.DatabaseMetaData;
  4. import java.sql.DriverManager;
  5.  
  6. public class DBConnection {
  7. //Constants used to get access to the database
  8. public static void main(String[] args) {
  9. DBConnection.getInstance();
  10. }
  11. private static final String driver = "jdbc:sqlserver://kraka.ucn.dk";
  12. private static final String databaseName = ";databaseName=dmaj0916_196343";
  13.  
  14. private static String userName = "; user=dmaj0916_196343";
  15. private static String password = ";password=Password1!";
  16.  
  17. private DatabaseMetaData dma;
  18. private static java.sql.Connection con;
  19.  
  20. // an instance of the class is generated
  21. private static DBConnection instance = null;
  22.  
  23. // the constructor is private to ensure that only one object of this class is created
  24. private DBConnection()
  25. {
  26. String url = driver + databaseName + userName + password;
  27.  
  28. try{
  29. //load of driver
  30. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  31. System.out.println("Driver class loaded ok");
  32.  
  33. }
  34. catch(Exception e){
  35. System.out.println("Cannot find the driver");
  36. System.out.println(e.getMessage());
  37. }
  38. try{
  39. //connection to the database
  40. con = DriverManager.getConnection(url);
  41. con.setAutoCommit(true);
  42. dma = con.getMetaData(); // get meta data
  43. System.out.println("Connection to " + dma.getURL());
  44. System.out.println("Driver " + dma.getDriverName());
  45. System.out.println("Database product name " + dma.getDatabaseProductName());
  46. }//end try
  47. catch(Exception e){
  48. System.out.println("Problems with the connection to the database:");
  49. System.out.println(e.getMessage());
  50. System.out.println(url);
  51. }//end catch
  52. }//end constructor
  53.  
  54. //closeDb: closes the connection to the database
  55. public static void closeConnection()
  56. {
  57. try{
  58. con.close();
  59. instance= null;
  60. System.out.println("The connection is closed");
  61. }
  62. catch (Exception e){
  63. System.out.println("Error trying to close the database " + e.getMessage());
  64. }
  65. }//end closeDB
  66.  
  67. //getDBcon: returns the singleton instance of the DB connection
  68. public java.sql.Connection getDBcon()
  69. {
  70. return con;
  71. }
  72. //getDBcon: returns the singleton instance of the DB connection
  73. public static boolean instanceIsNull()
  74. {
  75. return (instance == null);
  76. }
  77. //this method is used to get the instance of the connection
  78. public static DBConnection getInstance()
  79. {
  80. if (instance == null)
  81. {
  82. instance = new DBConnection();
  83. }
  84. return instance;
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement