Questions with the Connectionfactory and DAO class

Asked

Viewed 2,107 times

4

I want to make a crud simple I am basing myself on some ready classes. My doubt follows:

public class ConnectionFactory {

private String url = "jdbc:postgresql://localhost:5432/aluno";
private String user = "postgres";
private String password = "";

public Connection getConnection(){
    try{
        Class.forName("org.postgresql.Driver");
        return DriverManager.getConnection(url, user, password);
    } catch (SQLException | ClassNotFoundException e){
        throw new RuntimeException(e);

That part:

return DriverManager.getConnection(url, user, password);

It means that the DriverManager has the static method called getConnection? And what does it return? A connection?

And I doubted that part too:

public AlunoDAO(){
    if(connection == null){
        connection = new ConnectionFactory().getConnection();
    }
}

I’m accessing the class method ConnectionFactory right in the builder, what do you call doing this kind of thing?

Why wasn’t it made like this:

connection.getConnection()

1 answer

6


It means that Drivermanager has the static method called getConnection? And what does it return? A connection?

Exactly. The method getConnection is static and will return a connection (an instance of java.sql.Connection) according to the configuration parameters that have been specified.

Note that in your case, you are connecting to Postgresql. To do this, you need the connection library to that database (usually a *.jar file that should be in your classpath when you start the application). This jar will be the implementation of how to connect with Postgresql.

Java has an abstraction for database connection called JDBC (Java Database Connectivity). The Postgresql driver you are using follows the specification of this abstraction to implement connection details with a Postgresql database.

And how important that is?

Tomorrow if you change your database to Mysql, for example, just include the jar (Mysql connection driver) and change the connection string to Mysql. Obviously, if you are using specific Postgresql features within your DAO classes, then the change will be more complicated. This is one of the reasons frameworks like Hibernate exist, but this is subject to another question.

Now about the DAO...

I’m accessing the Connectionfactory class method on builder, what is called doing this kind of thing?

In your DAO there is an attribute called java.sql.Connection connection. This attribute is what you will use to interact with your database. Making queries, insertions, changes, etc.

When you instantiate an Alunode, this attribute is initialized with null. However, soon in your constructor you pass to him the reference of an object Connection, created by its factory called ConnectionFactory. In other words, your Alunodao instance now has a database connection.

If that’s all you do connection.getConnection(), as proposed in the question, there will be a build error. Since the class java.sql.Connection does not have a method getConnection(). Who has a getConnection() is the DriverManager.

Overall, your implementation is consistent. The only caveat I make is that every time you call ConnectionFactory.getConnection() a new connection to the database will be made. This is not desirable, as each time you instantiate one of your Daos, a new connection will be made without need. Usually, you work with a connection or use some library to manage a connection pool. But this is subject to another question.

  • Thank you. "Obviously, if you are using specific Postgresql features within your DAO classes, then the change will be more complicated." You refer to database syntax, it is not?

  • You know a lot! wow :D

  • That’s right @Alinegonzaga. The basic SQL syntax is usually the same, because the companies that develop the banks try to follow a pattern, however, there are things that can diverge a lot. Example: in SQL Server you query the current date of the database with the getdate() function, in Postgresql the function is different. Thus,.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.