0

I'm trying to create a method that allows me to connect to a database. Since it can be use more than one different database I was trying to create a generic method that receives the database connection as String and connect to that database.

public static Connection ConnectingDB(String urlConnection) throws SQLException {
        Connection c = null;
        try {
            Class.forName("??");
            c = DriverManager.getConnection("??:"+ urlConnection);
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Opened database successfully");
        return c;
    }

What should I put in Class.forname and in DriverManager.getConnection()?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90

1 Answers1

0

I've implement the same kind of things using custom qualifier.

If you have define a jdbc Datasource on your server. In J2EE with annotation, you can define a custom qualifier and a resource producer that will instantiate your different Datasource.

I have asked this question 3 month ago and i have managed to do it :

Custom qualifiers on interface doesn't work on injection

Hope it helps you

Community
  • 1
  • 1
Koraxos
  • 418
  • 5
  • 13