Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No suitable driver found for jdbc:db2: in java

Tags:

java

jdbc

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:/

like image 804
Chimbu Durai Avatar asked Mar 09 '26 18:03

Chimbu Durai


1 Answers

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!

like image 171
anacron Avatar answered Mar 12 '26 07:03

anacron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!