Java add PostgreSQL Driver to Class Path - java

I have a script that I would like to use to interface with a PostgreSQL database but I'm struggling a bit to include the driver when executing.
The Java code is very basic at the moment
import java.sql.*;
import java.util.*;
public class l_connect {
public static void main(String[] args) {
try {
Class.forName("org.postgresql.Driver");
} catch(Exception log) {
System.out.println(log);
}
}
}
If I execute this
# java l_connect
it does what I expect it to; outputs the exception log that it can't find the driver
I downloaded the postgresql driver and placed it in a directory in my project and then tried to execute it
# java -cp ".;../assets/postgresql-9.4-1202.jdbc4.jar" l_connect
and I get the error
# Error: Could not find or load main class l_connect
Why would this be happening? Is my usage of java -cp not correct?
UPDATE
I forgot to mention that my system is Fedora 22 Linux and I am not using an IDE I am using the terminal

May be this would help some one.
export CLASSPATH=/myapp1.jar:/myapp2.jar

Related

ClassNotFoundException in JDBC program despite of adding driver's JAR file [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 2 years ago.
I am writing a simple program in Java using to demonstrate insertion of data in MySQL table using JDBC. But the program is generating ClassNotFoundException while registering driver.
import java.sql.*;
public class test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch(ClassNotFoundException e) {
System.out.println("ClassNotFoundException: "+e.getMessage());
}
}
}
I have added the driver JAR file in the same directory.
To compile the program:
javac -cp ".:~/Programs/D/friends/assignment/driver.jar;" test.java
To execute the program:
java -cp ".:~/Programs/D/friends/assignment/driver.jar;" test
O/p:
ClassNotFoundException: com.mysql.jdbc.Driver
Note: The issue is caused by ; at the end of the driver.jar and also not using fully qualified path.
Windows based OS uses ; separator whereas Unix-based OS uses : separator.
Solution :
First compile the code : javac test.java (Run this command)
Run the code without semi-colon : java -cp .:<fully-qualified-path>/driver.jar test
Sample output :
anish#Anishs-MacBook-Pro ~ % javac Test.java
anish#Anishs-MacBook-Pro ~ % java -cp .:/Users/anish/driver.jar Test
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Note : I'm using mysql-connector-8.0.15.jar. If you are using the same or greater, then change from com.mysql.jdbc.Driver to com.mysql.cj.jdbc.Driver as that class is deprecated.
1.at the end of the classpath there seems to be an extra semi-colon:
/assignment/driver.jar;"
try without it
2. Are you sure driver.jar is the correct file?
normally they are called like mysql-connector-java-8.0.23.jar

ucanaccess jdbc connection without IDE [duplicate]

This question already has an answer here:
UCanAccess Initializer Error (compile/run without IDE)
(1 answer)
Closed 6 years ago.
I am trying to make a basic JDBC program using MS Access. I downloaded the Ucanacess.zip file and I got in total 6 .jar files namely:
ucanaccess-3.0.7,
ucanload,
commons-lang-2.6,
commons-logging-1.1.1,
hsqldb, and
jackcess-2.1.3
I added them to the classpath as Environment Variables(Computer->Properties->Advanced System Settings->Environment Variable).
But when I run my Code it gives an Exception
java.lang.ClassNotFoundException: net.ucanaccess.jdbc.UcanaccessDriver
Here is the code
class DB {
public static void main(String[] args) {
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
}
catch(ClassNotFoundException ex) {
System.out.println(ex);
}
}
}
You have to run :
javac DB.java
java -cp . -cp ucanaccess-3.0.7.jar -cp ucanload.jar ... DB
The former to compile DB.java.
The latter to run java by setting classpath, "." is the directory where resides the compiled "DB.class"

Running java class with library from cmd

I have a Baseloader.java class which makes use of Postgresql database, and I have a postgresql42.jar file which handles the jdbc stuff.
Baseloader.java
import java.sql.Connection;
import java.sql.DriverManager;
public class Baseloader
{
public static void main(String[] args)
{
System.out.println("Hello, sir!");
Connection c = null;
try {
Class.forName("org.postgresql.Driver");
c = DriverManager
.getConnection("jdbc:postgresql://localhost:5432/my_base",
"postgres", "123");
System.out.println("Done, sir!");
} catch (Exception e) {
e.printStackTrace();
System.err.println(e.getClass().getName()+": "+e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
}
So, when I compile and run this code through Netbeans (the postgresql.jar was included using GUI into Libraries folder) everything goes OK, the output is
Hello, sir!
Done, sir!
Opened database successfully
But when I try to run the compiled class file from cmd I get an error.
I placed Baseloader.class and postgresql42.jar into the same folder and cd'ed into that folder in cmd. Then I use the java -classpath postgresql42.jar Baserunner command which was proposed on one of the answers here, but it gava me Could not find or load main class Baseloader error.
If I type just java Baseloader it gives Exception in thread "main" java.lang.UnsupportedClassVersionError.
What I'm doing wrong and how to fix it?
EDIT: I've changed the java version in cmd: now java -version gives 1.8.0_73'.Then I checked which version Netbeans used: System.out.println(System.getProperty("java.version")) it also gave 1.8.0_73.
In environmental variables the JAVA_HOME is C:\Program Files\Java\jdk1.8.0_73, while System.out.println(System.getProperty("java.home")) gives C:\Program Files\Java\jdk1.8.0_73\jre. Can that difference be the case?
EDIT2: Changed JAVA_HOME to C:\Program Files\Java\jdk1.8.0_73\jre with no result.
Also javac -version gives the same newest 1.8.0_73 now.

Classpath for MySQL connector to JDBC-can't load the driver

I was trying to connect to MySQL using JDBC API. I have downloaded the MySQL driver which is the "mysql-connector-java-5.1.28-bin jar" file. My OS is Windows 7 and I have set the Classpath of Java to following path:
"E:\Myclass"
I have copied the above jar file to this folder.
Then I have written the following code to test if I can load the driver.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
// handle the error
System.out.println("Unable to load Driver Class");
return;
}
}
}
I expect everything should be working fine but I always get the "Unable to load Driver Class". Can anyone point out where was wrong? Thanks
Note: Thanks for all your answers. I have solved the problem. Since I am using Eclipse, I have add the JAR file to the classpath of the Eclipse.
You have to include the JAR in your classpath:
java -jar yourdriver.jar LoadDriver
JARs are filesystems. They should be added to your classpath the same way you add directories. Only classes will be loaded from the classpath you specified.
Use the below cmd to run it
java -cp E:\Myclass\mysql-connector-java-5.1.28-bin.jar; LoadDriver
As mentioned the mysql jar exist # E:\Myclass\mysql-connector-java-5.1.28-bin.jar , just set in the classpath and run it

package oracle.jdbc.driver does not exist

Error in the following code ![error occured][1]
import java.sql.*;
public class DBConnect{
public static void main(String a[]) throws SQLException{
// *package oracle.jdbc.driver does not exist*
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
System.out.println("Driver is registered");
}
}
You have to add ocjdbc jar in to your class path and try it like this.
if your jar file and java source is in same location. Use a command prompt and changed directory to that location. and execute following
javac -classpath ocjdbc14.jar DBConnect.java
and see.
import java.sql.*;
import oracle.jdbc.driver.OracleDriver;
public class DBConnect{
public static void main(String a[]) {
try{
Driver d=new OracleDriver();
DriverManager.registerDriver(d);
System.out.println("Driver is registered");
}catch(SQLException e){
System.out.println("Error occured "+e.getMessage());
}
}
}
You need to Add an oracle driver jar to the project build path,
Download Ojdbc14.jar file and put it in your classpath.
You need to do following steps if you are using intellij
Download jdbc7 or any version
Add this jar on following path File>>Project Structure>>Libraries>>
Image 1
Click on Modules and add jar there too
if you still face issue then you must see following problem
Image 2
Now click on problems>>fix>>add to dependency as given below
Image 3
Hope this will fix your issue
First run the program in netbeans and add Ojdbc14.jar file into the library of the program and then it will surely execute.
After executing in NetBeans, click Clean & Build Project.... This will create a jar file and then path like
java -jar "C:\Users\s\Documents\NetBeansProjects\jdbcTest_course\dist\jdbcTest_course.jar" will be provided.
Enter this into a command prompt (cmd) and it will run.

Categories