Why use DriverManager for bank connection?

4

I'm 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 noticed 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 did not find the answer to, why use DriverManager as in the example above.

So why use DriverManager in this way specifically?

EDIT

Link for the question: Difference between using SQLiteConnection and Java DriverManager

    
asked by anonymous 26.06.2017 / 16:31

1 answer

2

Among the various interfaces of the java.sql package, referring to JDBC, there is the Connection interface that defines methods to execute a query , commit transaction and close connection, among other resources. To work like MySQL, you need to use concrete classes that implement these interfaces of the java.sql package.

This set of concrete classes is what will bridge the client code that uses the JDBC API and the database. It is these classes that know how to communicate through the proprietary protocol of the database. This class set is named driver .

When you really want to open a connection to a database, you must always use a driver . The DriverManager class is responsible for communicating with all drivers that is left available. For this, the static method getConnection is invoked with a string that indicates which bank you want to connect to. As you did in 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

26.06.2017 / 16:51