3

I'm trying to connect to a remote database using the following java code, but I get an error saying no suitable driver found

I have included the required db2 libraries in my project:

enter image description here

I have declared the jdbc settings in the main class

Settings.loadSettings();

Class.forName("Settings.DB2_JDBC_DRIVER");

Controller con = new Controller();
con.business_logic();
}

Then trying to connect the database in another class

public Connection getDBConnection()
{
Connection DBConnection = null;
try {
  System.out.println("Connecting to  database " + Settings.DBName + ".");
  String DBURL = "jdbc:db2://" + Settings.DBServer + ":" + Settings.DBPort + 
 "/" + Settings.DBName + ";";
  String decryptedPass = decryptPassString(Settings.DBPass);
  DBConnection = DriverManager.getConnection(DBURL, Settings.DBUser, 
  decryptedPass);
  System.out.println("Database connection successfully established to  
  database " + Settings.DBName + " using user " + Settings.DBUser + ".");
  return DBConnection;
 }
 catch (Exception e) {
  System.out.println("An unexpected error occurred when attempting to 
 establish connection to  database " + Settings.DBName + ". The error was: " 
 + e.getMessage() + "\r\n" + e.getMessage()); }
return DBConnection;
}

can anyone please explain what i am missing here ?

The error message i am receiving is

An unexpected error occurred when attempting to establish connection to database DWHER00. The error was: No suitable driver found for jdbc:db2:/

anacron
  • 6,443
  • 2
  • 26
  • 31
Chimbu Durai
  • 73
  • 1
  • 2
  • 9

2 Answers2

7

The line:

Class.forName("Settings.DB2_JDBC_DRIVER");

should ideally be something like this:

Class.forName("com.ibm.db2.jcc.DB2Driver");

unless you are creating your own driver for DB2.

You will need the JAR files for the DB2 installation that you are using.

Put them in the class path and make the above code change. And it should work.


IBM DB2 Universal Driver Type 4

Driver Class Name:

com.ibm.db2.jcc.DB2Driver 

Driver Jar Files: db2jcc.jar and db2jcc_license_cu.jar (Both of these jars must be included)

JDBC URL Format:

jdbc:db2://<host>[:<port>]/<database_name>

JDBC URL Examples:

jdbc:db2://127.0.0.1:50000/SAMPLE

IBM DB2 Universal Driver Type 2

Driver Class Name:

com.ibm.db2.jcc.DB2Driver

Driver Jar Files: db2jcc.jar and db2jcc_license_cu.jar (Both of these jars must be included)

JDBC URL Format:

jdbc:db2:<database_name> 

JDBC URL Examples:

jdbc:db2:sample 

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
0

Make sure the /lib folder is added to the build path of your project as well. See here other answer

Community
  • 1
  • 1
Stimpson Cat
  • 1,444
  • 19
  • 44