ClassNotFoundException when using JDBC driver for DB2 - java

I'm trying to connect to a DB2 database using JDBC. Therefore I downloaded the DB2 driver db2jcc.jar and added the path to the classpath while compiling and running my application (I'm not using an IDE).
The following is the source of my Test-Application:
import java.sql.*;
public class TestApp {
public static void main(String[] args){
try {
Class.forName("com.ibm.db2.jcc.DB2Driver");
}
catch (Exception e) {
System.out.println(e);
}
}
}
Does anybody know, where my problem is?

Try compiling:
'javac -cp ".;(path)/db2jcc.jar;(path)/db2jcc_license_cu.jar" TestApp.java'
Then running
'java -cp ".;(path)/db2jcc.jar;(path)/db2jcc_license_cu.jar" TestApp'
You only need quotes if spaces in the file/path names also.

Related

Connection with SQL Server: java.sql.SQLException: No suitable driver found for jdbc:sqlserver

I am trying to connect a little Java App to a SQL Server database using Visual Studio Code. This is my code:
import java.sql.*;
public class App {
public static void main(String[] args) {
String connectionUrl =
"jdbc:sqlserver://DESKTOP-03898L2DB;"
+ "Database=DB_PA;"
+ "User=checkou-GN;"
+ "Password=sapassword;"
+ "IntegratedSecurity=true;"
+ "encrypt=true;trustServerCertificate=true";
;
try {
//String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//Class.forName(driver);
try (Connection conn = DriverManager.getConnection(connectionUrl);) {
System.out.println("Connected");
}
} catch (SQLException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
But when I run it I get this message:
cd "c:\Users\checki\Desktop\JAVA\pruebaN3\prueba\src\" && javac App.java && java App
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://DESKTOP-033L2DB;Database=DB_PA;User=checkou-GN;Password=sapassword;IntegratedSecurity=true;encrypt=true;trustServerCertificate=true
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:252)
at App.main(App.java:16)
I've succesfully imported the connectors and the SDK as shown in the picture:
The thing is that I've tried to do the same on another computer and it seems to work fine:
The main difference for me is the command that the shell is executing: in the first case is using javac and java and in the working case is using:
& 'C:\Program Files\Java\jdk-18\bin\java.exe' '--enable-preview' '-XX:+ShowCodeDetailsInExceptionMessages' '#C:\Users\sempr\AppData\Local\Temp\cp_9hxr1t8qo8sc8hiaiyq1qpyf9.argfile' 'App'
I don't know what the problem could be and I've tried almost everything. In Eclipse and Intelij the program works fine on both machines but with VSCode in the first machine does not work.
You are using Code Runner for the first case? Please choose Run Java button instead of Run Code button. The Code Runner will not find the referenced libraries.

error: java.lang.ClassNotFoundException: org.postgresql.Driver

I'm just trying to connect to postgres (13.0.1) from java (11.0.8) on Debian 10 and get the error below:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class cli_test {
public static void main(String[] args) {
try {
Connection connection = null;
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/myproj", "postgres", "<pwd>");
System.out.println("Connection established successfully");
} catch (Exception e) {
System.out.print("error: \n" + e.toString() + "\n" + e.getMessage());
}
}
}
I run these commands:
CLASSPATH=/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
javac -verbose cli_test.java
java cli_test
I have verified that the jar file exists in that location and is set to 755 permissions.
I get this error:
java.lang.ClassNotFoundException: org.postgresql.Driver
I have spent hours searching online and found many others with this problem. No other solutions I found have fixed it. I'm trying to do this without a java ide, just a text editor.
That's not a Java problem but you use bash in a wrong way. Setting CLASSPATH=.. will not make this environment variable available to java in the program call afterwards. Try
export CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar
javac -verbose cli_test.java
java cli_test
or
CLASSPATH=.:/usr/lib/jvm/java-11-openjdk-amd64/lib/postgresql-42.2.18.jar java cli_test

ClassNotFound Exception for "com.mysql.jdbc.Driver" even after adding mysql-con.jar file

I have already set the classpath to mysql-connector-java-5.0.8-bin.jar and compiled my class successfully.
But when I run it I get:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
The code is:
import java.sql.*;
public class JdbcExample
{
public static void main(String arg[])
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://loacalhost:3306/sample","root","root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from sample");
while(rs.next())
{
System.out.println(rs.getString(1));
}
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Make sure you are "adding" the mysql-connector-java-5.1.42-bin.jar file to the classpath when starting your program (obviously it can be a different version number).
something like
java -cp .;mysql-connector-java-5.0.8-bin.jar JdbcExample
or
set CLASSPATH=...;mysql-connector-java-5.0.8-bin.jar
java JdbcExample
assuming:
the JAR is in the current folder... if that works, consider putting the JAR in a 'central' place a use the complete path in above commands
using Windows, otherwise the separator would be : instead of ;
the class is in no package
for Ubuntu:
java -cp .:mysql-connector-java-5.0.8-bin.jar JdbcExample
Looks like more of a classpath issue. Try adding the jar manually to your project. I ran the same with mysql-connector-java-5.0.8.jar and I dont get this error.

No suitable driver found for jdbc:derby:<db-name> error on JDBC with Java DB on Linux/Ubuntu

Essential parts of the Code:
(Taken from example from Book: Java How to Program 10th ed, By: Paul Deitel, Harvey Deitel (p. 1063)):
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DisplayAuthors
{
public static void main(String args[])
{
final String DATABASE_URL = "jdbc:derby:books";
try (
Connection connection =
DriverManager.getConnection(DATABASE_URL, "user", "pass");
)
{
// ...
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
}
I'm on Ubuntu 16.04
Although Java website for Java DB says the db is included in JDK, I have
learned after few hours of web searches that openJDK java, the recommended
install on Ubuntu, does not come with the java db.
The website that had that helpful information suggested to run:
sudo apt-get install sun-javadb-client sun-javadb-core
Which produced error and did not install.
So I downloaded jdk-8u111-linux-x64.tar.gz, extracted as instructed, and just to be safe(I believe, I could have just set JAVA_HOME, DERBY_HOME to extracted location, and which did "half-worked" upto ij parts[see below]), removed everything from my /usr/lib/jvm/java-8-openjdk-amd64/ and replaced with the content of what I downloaded.
And added these in my .bashrc:
JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/
DERBY_HOME=/usr/lib/jvm/java-8-openjdk-amd64/db
export DERBY_HOME
Entered in terminal to create database and insert data as instructed in book:
$JAVA_HOME/db/bin/ij
connect 'jdbc:derby:books;create=true;user=usr;password=pass';
run 'book-basic-table-create-and-insertions.sql';
exit;
It produces a file books with db info+/data inside it.
At ./ there's also a file named derby.log that contains these informations:
----------------------------------------------------------------
Wed Dec 14 19:01:58 EST 2016:
Booting Derby version The Apache Software Foundation - Apache Derby - 10.11.1.2 - (1629631): instance a816c00e-0158-ffc9-1471-000006d047c8
on database directory /tmp/deleteme-IuI/books with class loader sun.misc.Launcher$AppClassLoader#6f94fa3e
Loaded from file:/usr/lib/jvm/java-8-openjdk-amd64/db/lib/derby.jar
java.vendor=Oracle Corporation
java.runtime.version=1.8.0_111-b14
user.dir=/tmp/deleteme-IuI
os.name=Linux
os.arch=amd64
os.version=4.4.0-53-generic
derby.system.home=null
Database Class Loader started - derby.database.classpath=''
----------------------------------------------------------------
Wed Dec 14 19:04:25 EST 2016: Shutting down Derby engine
----------------------------------------------------------------
Wed Dec 14 19:04:27 EST 2016:
Shutting down instance a816c00e-0158-ffc9-1471-000006d047c8 on database directory /tmp/deleteme-IuI/books with class loader sun.misc.Launcher$AppClassLoader#6f94fa3e
----------------------------------------------------------------
Compiled and ran:
javac DisplayAuthors.java # compiles without error
java DisplayAuthors
Program outputs:
java.sql.SQLException: No suitable driver found for jdbc:derby:books
at java.sql.DriverManager.getConnection(DriverManagerager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.javaava:247)
at DisplayAuthors.main(DisplayAuthors.java:15)
How do I get the program to work?
You are missing the load driver section of code
e.g.
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
...
try {
Class.forName(driver);
} catch(java.lang.ClassNotFoundException e) {
...
}
see http://docs.oracle.com/javadb/10.8.3.0/getstart/rwwdactivity3.html for an example
Edit
As a proof I have done the following
create simple java project in Eclipse
add derby.jar, derbynet.jar & derbyclient.jat to classpath
My Code
public static void main(String[] args) {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
try {
Class.forName(driver);
} catch(java.lang.ClassNotFoundException e) {
e.printStackTrace();
}
final String DATABASE_URL = "jdbc:derby:myDB;create=true;user=user;password=pass";
try (
Connection connection = DriverManager.getConnection(DATABASE_URL, "user", "pass");
)
{
// ...
}
catch (SQLException sqlException)
{
sqlException.printStackTrace();
}
}
This runs without Exceptions

ClassNotFoundException and NoClassDefFound errors with UCanAccess

The JDBC-ODBC Bridge is no longer included in Java 8 so I tried UCanAccess, but I am having trouble with that. Here is my code:
package jdbc;
import java.sql.*;
public class jdbc
{
Connection con;
Statement st;
jdbc()
{
try
{
con=DriverManager.getConnection("jdbc:ucanaccess://P:/eclipseWorkspace/databases/signup.accdb");
st=con.createStatement();
st.executeUpdate("INSERT INTO signup (firstName,lastName,email,password) VALUES ('rocky','balboa','rocky#gmail.com','pop')");
System.out.println("SUCCESS");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class main
{
public static void main(String args[])
{
new jdbc();
}
}
I have included some external jars as shown in the image:
http://i.imgur.com/ujhPP0l.png?1
When i run it, it gives me a stack trace with ClassNotFoundException and NoClassDefFound errors as shown here:
http://i.imgur.com/UACP77k.png?1
What is wrong with my code?
UCanAccess has several dependencies, one of which is commons-lang-2.x (2.4 or newer). You are using commons-lang3-3.3.2 in your project, so UCanAccess (actually Jackcess) cannot find the commons-lang-2.x classes.
When you unzipped the UCanAccess distribution it created a lib/ folder with the compatible versions of the required dependencies. You need to replace the commons-lang3-3.3.2.jar reference in your project with commons-lang-2.6.jar from the UCanAccess lib/ folder.

Categories