No suitable driver found for jdbc:db2: in java - java

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

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!

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

Related

Java, sqlite3 since command line troubleshooting

my name is Omar
i am following the instructions in this webpage https://www.tutorialspoint.com/sqlite/sqlite_java.htm in order to compile a sample program of java conected with sqlite3.
sample program
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 downloaded the sqlite connector sqlite-jdbc-3.7.2.jar but according to the webpage above, i Added the address of downloaded jar file sqlite-jdbc-(VERSION).jar in my class path(the same class path of mysql address connector) and place the sqlite-jdbc-3.7.2.jar in C:\Program Files\Java\jdk1.8.0_152\jre\lib\ext just like mysql connector driver.
but i got bad results when i try to run it in command line
"No suitable driver found for jdbc: sqlite: test.db"
i run java -classpath ".;sqlite-jdbc-3.7.2.jar" SQLiteJDBC with the sqlite-jdbc-3.7.2.jar in the same folder in command line but i got bad results too.
"No suitable driver found for jdbc: sqlite: test.db"
Where sqlite-jdbc-3.7.2.jar connector should be placed according to connect java and sqlite just as myql do?
anyhelp is appreciated
thank you in advance
Omar
You should download and use the newest SQLite driver 3.27.2.1 from this site: https://bitbucket.org/xerial/sqlite-jdbc/downloads/
I tried it with 3.7.2 and got the same error. But with 3.27.2.1 it works well.

JDBC no suitable driver found just on Linux? [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed last month.
I am trying to write a program to connect to a MySQL database in eclipse, but I get the error "java.sql.SQLException: No suitable driver found".
The java code is:
import java.sql.*;
public class FirstExample {
//static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String S_DB_URL = "jdbc:mysql://localhost:3306/emp";
static final String S_USER = "root";
static final String S_PASS = "root";
public static void main(String[] args) {
try {
System.out.println("Connecting to database...");
//Class.forName(S_JDBC_DRIVER);
Connection connection = DriverManager.getConnection(S_DB_URL,
S_USER, S_PASS);
System.out.println("Creating statement...");
Statement statement = connection.createStatement();
String sql = "SELECT * FROM Employee";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int iId = resultSet.getInt("id");
int iAge = resultSet.getInt("age");
String sFirst = resultSet.getString("fname");
String sLast = resultSet.getString("lname");
System.out.print("ID: " + iId);
System.out.print("\tAge: " + iAge);
System.out.print("\tFirst: " + sFirst);
System.out.println("\tLast: " + sLast);
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException se) {
for (Throwable t : se) {
t.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}
The output in the console tab is:
Connecting to database...
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/emp
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at FirstExample.main(FirstExample.java:21)
Goodbye!
I have used the MySQL Connector/J. It is unzipped in the MySQL installation directory and the jar file is added to the CLASSPATH.
Also refer to this image. There is an ! mark at the project root.image01
I get the error as in the next image: image02 when I include the 2 commented statements:
static final String S_JDBC_DRIVER = "com.mysql.jdbc.Driver";
Class.forName(S_JDBC_DRIVER);
For all but the most trivial applications the CLASSPATH environment variable is NOT used. Normally the libraries are include in the Class-Path entry in the manifest of the jar, or in the -cp option of the java commandline.
In this case you need to add the MySQL JDBC driver to the buildpath of your Eclipse project.
I had the same problem. I solved it by adding:
Class.forName("com.mysql.jdbc.Driver");
you can place the path like java -cppwd/mysql-connector-java-5.1.22-bin.jar:. <classname>.
make sure that your in same directory where the mysql driver is .
Hope that helps .
Load Driver class just before getting the connection.
Use this code:
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_db", "user", "passw");
Alternatively you can also add the installed jar file to your eclipse project by selecting the project in eclipse, right click it and go down to properties, select the Java Build Path>>select the Libraries Tab>>Add external jar file and browse for the installed mysql-connector-java.jar file or any mysql java connector file int the /usr/share/java/ directory for most ubuntu users. Click okay and rebuild your project. Good luck
I encountered the same problem as you, but I handled it as follows:
I copied the jar, which is called mysql-connector-java-5.1.23-bin.jar, into the \Apache Software Foundation\Tomcat 6.0\lib, and restarted tomcat.
Hope that helps

MySql Driver Not Loading In Java

When I use the following line,
Class.forName("com.mysql.jdbc.Driver");
//Sets up database connection
connect = DriverManager.getConnection("jdbc:mysql://www.papademas.net/tickets?"
+ "user=root&password=jamesp");
statement = connect.createStatement();
String sql = "INSERT INTO JReimTicketer (dateIssued, ticketName, issuerName,"
+ " issuerDepartment, ticketDescription, activity) "
+ "VALUES (SYSDATE(),'"+ticketName+"', '"+issuerName+"', "
+ "'"+issuerDepartment+"', '"+ticketDescription+"', "
+ " '"+activity+"')";
my program stops, and it doesn't seem like it loads the driver. I've downloaded it, so I'm not sure why it's not working. Any help would be appreciated.
Firstly if your are using jdbc 4.0, you don't require
Class.forName("com.mysql.jdbc.Driver");
to load driver as it it auto-loaded when you call
DriverManager.getConnection();
If you have specified mysql jar in your class path, the problem must be in your url. So kindly check your URL/Username/Password
Also, if you are getting exception,please post stacktrace
Your code seems to be correct. And I think you're right. Things aren't missing from Front-End but Back-End. So, before you compile your code, you need to put MySQL Connecter jar (mysql-connector-java-x.x.xxx-bin.jar) file in your Java Buildpath Path. Do it before you compile and run the code.

Connecting to Netbeans SQL Database for the first time - java.sql.SQLException: No suitable driver found 0 08001

I am fairly new to Java, so I bought a book to learn from. Everything went swimmingly until I got to the chapter on SQL. I am working in NetBeans with the sample database, but I can't get the database to connect with the program.
Here's the code I faithfully copied from the book:
import java.sql.*;
public class SysTableReporter {
public static void main(String[] arguments) {
String data = "jdbc:derby://localhost:1527/sample";
try (
Connection conn = DriverManager.getConnection(
data, "app", "APP");
Statement st = conn.createStatement()) {
System.out.println("TABLEID:\t" );
Class.forName("org.apache.derby.jdbc.ClientDriver");
ResultSet rec = st.executeQuery(
"select * " +
"from SYS.SYSTABLES " +
"order by TABLENAME");
while(rec.next()) {
System.out.println("TABLEID:\t" + rec.getString(1));
System.out.println("TABLENAME:\t" + rec.getString(2));
System.out.println("TABLETYPE:\t" + rec.getString(3));
System.out.println("SCHEMAID:\t" + rec.getString(4));
System.out.println();
}
st.close();
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
}
}
}
Here's what my Services Panel looks like:
Click to view Image
Here's my output:
SQL Error: java.sql.SQLException: No suitable driver found for jdbc:derby://localhost:1527/sample 0 08001
At first I figured I just had some typo, but looking over it carefully I can't find it. I tried installing a new JDBC driver, but that didn't seem to help either. I'm running a Windows 8 laptop, and I have the telnet client turned on. My best guess at this point is that I somehow missed a step in the initial setup, but I can't find it to go back and fix it. Any help would be great.
You are probably just missing the Derby JDBC driver jar in your project's library section (I'm assuming that you created a standard Netbeans project, not a Maven type project). The jar is called derbyclient.jar.
The easiest way to solve this:
locate derbyclient.jar eg with Explorer
in Netbeans, rightclick on the project node in the Projects pane.
Select Properties, and there select libraries
Click "Add JAR/Folder", navigate to derbyclient.jar
That effectively adds the jar to your project. Just recompile, run and everything should work as intended.
EDIT: aside from #BobKuhar's find, another problem with the given code is that it doesn't use one of Java's more powerful debugging mechanisms, the stacktrace. At its most basic form, showing them on screen is simple, not more than
} catch (SQLException s) {
System.out.println("SQL Error: " + s.toString() + " "
+ s.getErrorCode() + " " + s.getSQLState());
// and show us the stacktrace
s.printStackTrace();
} catch (Exception e) {
System.out.println("Error: " + e.toString() + e.getMessage());
// and show us the stacktrace
e.printStackTrace();
}
the stack trace will not only show you the exact line at which the error occurred, but also the trajectory to the exception (how the program got there), invaluable in more complicated programs. Definitely something you want to learn to use!
Lots of info on stack traces here: What is a stack trace, and how can I use it to debug my application errors?
I think what you really have is just a sequencing problem. The Class.forName call registers the driver with the DriverManager (I think). This needs to occur before you attempt to establish a Connection through the DriverManager.
Class.forName( "org.apache.derby.jdbc.ClientDriver" ).newInstance();
Connection conn = DriverManager.getConnection( data, "app", "APP");
If this gives you some "ClassNotFound" exception, then fvu's assertion that you don't have the Derby JDBC Jar on the class path is your next issue.
The Derby docs have an example: http://db.apache.org/derby/integrate/plugin_help/derby_app.html

Can't find Oracle Database Driver in Eclipse JAVA Program

I am trying to connect to an oracle database in eclipse. I have ojdbc14.jar in a lib folder in the same project and i added it to the build path of my project, so it also resides in the Referenced Libraries directory, but yet i still get an output to the console of "Could not find the database driver"
Myself and another student employee have been trying to get this figured out for the past day and a half, and no one else in our department is experienced in Java and JSP so i thought StackOverflow would be our best bet =)
try {
// Load the JDBC driver
String driverName = "oracle.jdbc.driver.OracleDriver";
System.out.println("Attempting to load the driver...");
Class.forName(driverName);
System.out.print("Loaded the driver");
// Create a connection to the database
String serverName = " ;) ";
String portNumber = " ;) ";
String sid = " ;) ";
String url = "jdbc:oracle:thin:#" + serverName + ":"
+ portNumber + ":" + sid;
String username = "kenne13";
String password = "**********";
connection = DriverManager.getConnection(url, username, password);
if (connection != null) {
return true;
}
} catch (ClassNotFoundException e) {
// Could not find the database driver
System.out.println("Could not find the database driver");
connected = false;
} catch (SQLException e) {
// Could not connect to the database
System.out.println("Could not connect to the database");
connected = false;
}
Here is the output in the console:
Aug 18, 2011 10:07:50 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 382 ms
Attempting to load the driver...
Could not find the database driver
Here is a screen capture of the code, the error, and my project directories.
I'm quite sure now that the class name of the driver is oracle.jdbc.OracleDriver and not oracle.jdbc.driver.OracleDriver.
I believe the Oracle Driver class is: oracle.jdbc.OracleDriver
Go to the deploy folder(C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps) of your web app and make sure that ojdbc14.jar is there.
In a related post:
Cannot find Oracle jdbc driver
the problem was solved adding the oracle library in the run path. Have you tried to configure too? (Run/Run Configurations Select your configuration/classpath tab)

Categories