0

I don't understand the purpose of DatabaseAccess class. I'm curious about how did the object name "conDA" contains the database connection even without using the getConnection() method I created. Example "con2 = new Connection2(conDA);" how is that "conDA" object name can be understood as a connection of database? Here are the codes:

import java.sql.*;
public class Connection1 
{
    static Connection connection;
    static ConnectionDA conDA;
    static Connection2 con2;

    public static void main(String args[]) throws ClassNotFoundException, SQLException
    {
        conDA = new ConnectionDA(getConnection());
        System.out.println("Con1 Connected!!");
        con2 = new Connection2(conDA);  
    }

    public static Connection getConnection() throws ClassNotFoundException, SQLException
    {
        Class.forName("com.ibm.db2.jcc.DB2Driver");
        connection =  DriverManager.getConnection("jdbc:db2://localhost:50000/Sample", "ZeroCool","siliconvalley"); 

        return connection;
    }
}

2nd Class

import java.sql.*;
public class Connection2
{
    ConnectionDA conDA;
    Connection3 con3;

    public Connection2(ConnectionDA conDA) throws ClassNotFoundException
    {
        this.conDA = new ConnectionDA(conDA.getConnection());
        this.conDA = conDA;
        System.out.println("Con2 Connected!!");
        con3 = new Connection3(conDA);
    }
}

3rd Class

import java.sql.*;
public class Connection3 
{
    ConnectionDA conDA;

    public Connection3(ConnectionDA conDA) throws ClassNotFoundException
    {
        this.conDA = new ConnectionDA(conDA.getConnection());
        this.conDA = conDA;
        System.out.println("Con3 Connected!!");
    }
}

Database Access Class

import java.sql.*;
public class ConnectionDA 
{
    Connection connection;

    public ConnectionDA(Connection connection) throws ClassNotFoundException
    {
        this.connection = connection;
        System.out.println("ConDA Established!!!");
    }

    public Connection getConnection() 
    {
        return connection;
    }
}

1 Answer 1

1

how is that "conDA" object name can be understood as a connection of database?

because the class ConnectionDA have the method getConnection implement that returns an object of the type connection.

public Connection3(ConnectionDA conDA) throws ClassNotFoundException
{
    this.conDA = new ConnectionDA(conDA.getConnection());
}

And here the implementation of the getConnection() method.

public class ConnectionDA 
{
    public Connection getConnection() 
    {
        return connection;
    }
}

I'm curious about how did the object name "conDA" contains the database connection even without using the getConnection() method I created

this is because, the class receive an connection on the constructor

public class ConnectionDA 
{
    Connection connection;

    public ConnectionDA(Connection connection) throws ClassNotFoundException
    {
        this.connection = connection;
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

So, I just need to call the ConnectionDA to establish connection to other classes? If that's the case, what is the use of getConnection() method everytime I initialize a conDA object name?
you have the method getConection() to get the connection in the case you need it for some reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.