Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.accenture.adf.dao;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DataConnection {
- // New instance of Connection
- private static Connection connection = null;
- // Opening connection with MYSQL database
- public static Connection createConnection() throws ClassNotFoundException,
- SQLException {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost/sampledb", "root", "root");
- System.out.println("----Connection established with MYSQL database----"+connection);
- return connection;
- }
- // Closing connection
- public static void closeConnection() throws SQLException {
- System.out.println("----Connection closed with MYSQL database----");
- connection.close();
- }
- public static void main(String[] args) {
- Connection con;
- try {
- con = DataConnection.createConnection();
- if(con!=null) {
- System.out.println("Connection Successful");
- }else {
- System.out.println("Connection not Successful");
- }
- } catch (ClassNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment