Guest User

Untitled

a guest
Apr 25th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. package com.accenture.adf.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class DataConnection {
  8.  
  9. // New instance of Connection
  10. private static Connection connection = null;
  11.  
  12. // Opening connection with MYSQL database
  13. public static Connection createConnection() throws ClassNotFoundException,
  14. SQLException {
  15. Class.forName("com.mysql.jdbc.Driver");
  16. connection = DriverManager.getConnection(
  17. "jdbc:mysql://localhost/sampledb", "root", "root");
  18. System.out.println("----Connection established with MYSQL database----"+connection);
  19. return connection;
  20. }
  21.  
  22. // Closing connection
  23. public static void closeConnection() throws SQLException {
  24. System.out.println("----Connection closed with MYSQL database----");
  25. connection.close();
  26. }
  27. public static void main(String[] args) {
  28. Connection con;
  29. try {
  30. con = DataConnection.createConnection();
  31. if(con!=null) {
  32. System.out.println("Connection Successful");
  33. }else {
  34. System.out.println("Connection not Successful");
  35. }
  36. } catch (ClassNotFoundException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. } catch (SQLException e) {
  40. // TODO Auto-generated catch block
  41. e.printStackTrace();
  42. }
  43.  
  44. }
  45.  
  46. }
Add Comment
Please, Sign In to add comment