Why use Drivermanager for connection to bank?

Asked

Viewed 699 times

4

I am connecting to the Sqlite database with the following code:

public SQLiteConnection conn() throws SQLException{
        String path = System.getProperty("user.dir");
      try {
       return new SQLiteConnection(path, "DadosPastas.s3db");
    } catch (Exception e) {
        return null;
    }
}

But I have observed several examples as follows:

 import java.sql.*;

 public class SQLiteJDBC {
 public static void main( String args[] ) {
  Connection c = null;

  try {
     Class.forName("org.sqlite.JDBC");
     c = DriverManager.getConnection("jdbc:sqlite:test.db");
  } catch ( Exception e ) {
     System.err.println( e.getClass().getName() + ": " + e.getMessage() );
     System.exit(0);
  }
  System.out.println("Opened database successfully");
 }
}

I’ve already researched and I haven’t found the answer to, why use the DriverManager as in the example above.

So why use the DriverManager in that way specifically?

EDIT

Link to the question: Difference between using Sqliteconnection and Java Drivermanager

  • Your question is : "Why use the DriverManager"? This is your doubt?!

  • Yes. I’ll ask the question.

1 answer

2


Between the various interfaces of the package java.sql, referring to JDBC, there is the interface Connection defining methods for executing a query, commit transaction and close connection, among other resources. To work such as Mysql, it is necessary to use concrete classes that implement these package interfaces java.sql.

This set of concrete classes will bridge the gap between the client code that uses the JDBC API and the database. These are the classes that know how to communicate through the proprietary protocol of the database. This set of classes is named after driver.

When in fact you want to open a connection to a database, it is necessary to always use a driver. The class DriverManager is responsible for communicating with all drivers which is left available. For this, the static method is invoked getConnection with a string which indicates which bank you want to connect to. How you asked the question:

c = DriverManager.getConnection("jdbc:sqlite:test.db");

The method has three overloads:

public static Connection getConnection(String url)
public static Connection getConnection(String url, Properties info)
public static Connection getConnection(String url, String user, String password)

References

  • Thanks acklay. If I use the SQLiteConnection without the DriverManager has any technical or performance problem?

  • @Robss70 I’m sorry, to tell you the truth, I don’t even know if it’s possible.

  • In the question code is without, and in the debug appears the connection opened normally. I can’t find the answer instead of one, why does everyone use this drivermanager.

  • @Robss70 what you could do then, would be an experiment, measuring the running time using the two classes. Through several tests, the to make a decision. I will take one more look if I find something in relation to this and I tell you.

  • If you find something I am very grateful, I don’t know where else to look. I will do the tests.

  • @Robss70 good, the in the answer I focused on "why to use Drivermanager", so I gave this answer by describing its functioning. Perhaps then, it would be better a question focusing more on the "Difference between using Sqliteconnection and Drivermanager in performance".

  • You’re right. I’ll mark it as an answer and ask another question

Show 2 more comments

Browser other questions tagged

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