Guest User

Untitled

a guest
Jul 8th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. package persistence;
  2.  
  3. import java.sql.*;
  4.  
  5. public class SQLConnection
  6. {
  7. private Connection connection = null;
  8. private final String url = "jdbc:sqlserver://localhost\\SQLEXPRESS";
  9. private final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  10. /* private final String serverName = "localhost";
  11. private final String portNumber = "1433";
  12. private final String databaseName = "ProjectManagement2";
  13. private final String userName = "Nelis";
  14. private final String password = "nelis";
  15. private final String selectMethod = "cursor"; */
  16. private static SQLConnection SQLconnection;
  17. private Statement statement = null;
  18. private PreparedStatement preparedStatement= null;
  19.  
  20. public SQLConnection() throws SQLException, ClassNotFoundException
  21. {
  22. Class.forName(DRIVER);
  23. connection = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=ProjectManagement;userName=Lennart;passWord=lennart");
  24. }
  25.  
  26.  
  27. public Connection getConnection()
  28. {
  29. return connection;
  30. }
  31. public void displayDbProperties()
  32. {
  33. java.sql.DatabaseMetaData dm = null;
  34. java.sql.ResultSet rs = null;
  35.  
  36. try
  37. {
  38. connection = this.getConnection();
  39.  
  40. if (connection != null) {
  41. dm = connection.getMetaData();
  42. System.out.println("Information about the drivers:");
  43. System.out.println("\tName driver: "+ dm.getDriverName());
  44. System.out.println("\tVersion drivers: "+ dm.getDriverVersion ());
  45. System.out.println("\nInformation about database:");
  46. System.out.println("\tName database: "+ dm.getDatabaseProductName());
  47. System.out.println("\tVersion database: "+ dm.getDatabaseProductVersion());
  48. System.out.println("Available catalogs ");
  49.  
  50. rs = dm.getCatalogs();
  51.  
  52. while(rs.next())
  53. {
  54. System.out.println("\tCatalogs: "+ rs.getString(1));
  55. }
  56.  
  57. rs.close();
  58. rs = null;
  59.  
  60. closeConnection();
  61. }
  62. else {
  63. System.out.println("Error: No connection is activated");
  64. }
  65. }
  66. catch(Exception e) {
  67. e.printStackTrace();
  68. }
  69. dm = null;
  70. }
  71.  
  72. private void closeConnection() throws SQLException
  73. {
  74. connection.close();
  75. }
  76.  
  77. public static SQLConnection getInstance() throws SQLException, ClassNotFoundException
  78. {
  79. if (SQLconnection == null){
  80. SQLconnection = new SQLConnection();
  81. }
  82. return SQLconnection;
  83.  
  84. }
  85.  
  86. /* public ResultSet executeQuery(String query) throws SQLException, ClassNotFoundException, IllegalStateException
  87. {
  88. if(connection == null)
  89. {
  90. throw new IllegalStateException("No connection to the database!");
  91. }
  92.  
  93. statement = getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.TYPE_FORWARD_ONLY);
  94. return statement.executeQuery(query);
  95. }
  96. */
  97. public PreparedStatement getPreparedStatement(String query) throws SQLException, ClassNotFoundException
  98. {
  99. preparedStatement = getConnection().prepareStatement(query);
  100. return preparedStatement;
  101. }
  102.  
  103. public int updateQuery(String query) throws SQLException, IllegalStateException, ClassNotFoundException
  104. {
  105. if(connection == null)
  106. {
  107. throw new IllegalStateException("No connection to the database.");
  108. }
  109. int recordsUpdated=0;
  110. recordsUpdated = statement.executeUpdate(query);
  111. return recordsUpdated;
  112.  
  113. }
  114. }
Add Comment
Please, Sign In to add comment