package oracle.jdbc.driver does not exist - java

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.

Related

i cannot use my sql connector jar file although i have added the jar file in vs code referenced libary

enter image description hereI have already added the jar file in vs code but it is showing package com mySql jdbc does not exist
I have tried all the way of adding jar file in vs code added jar file in referenced libraies also in lib folder but did not worked
code :
import java.sql.*;public class java01{
public static void main(String[] args) { try{
enter image description here
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
} catch (Exception e){
e.getStackTrace();
}
}}
Taking into consideration the screenshot you shared I pressume you do not have "MySQL connector jar" in your classpath when you compile/run this program.
So when you try to compile it
javac ./*.java
you gonna get an error
./Main.java:9: error: package com.mysql.jdbc does not exist
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
^
1 error
The solution for this is to include "MySQL connector jar" into your classpath
javac -cp ~/.m2/repository/com/mysql/mysql-connector-j/8.0.32/mysql-connector-j-8.0.32.jar ./*.java

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.

Java add PostgreSQL Driver to Class Path

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

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

Postgresql JDBC Connection Error

I've installed postgresql 9.1 for Windows but I can't connect to it using JDBC.
I've downloaded the JDBC jar file and placed it in C:\Program Files\Java\jre7\lib\postgresql-9.1-901.jdbc4.jar, my CLASSPATH is: .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip;C:\Program Files\Java\jre7\lib\postgresql-9.1-901.jdbc4.jar
This is my Java code to create the connection:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.*;
import java.io.*;
public class CreateInsert extends Object {
public static void main (String args[]) {
//Create the connection
String driverName = "org.postgresql.Driver";
String connectURL = "jdbc:postgresql://localhost/postgres";
String USERNAME = "postgres";
String PASSWORD = "password";
Connection con = null;
try {
Class.forName("org.postgresql.Driver");
con = DriverManager.getConnection(connectURL, USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
System.out.println("Error creating class: "+e.getMessage());
System.out.println("The Driver was not found, Please check driver location, classpath, username/password and server url settings");
System.exit(0);
} catch (SQLException e) {
System.out.println("Error creating connection: "+e.getMessage());
System.exit(0);
}
}
}
And I get the error "Error creating class: org.postgresql.Driver"
Any ideas as to what's wrong?
Thanks.
I'm using JCreator to compile and run.
The CLASSPATH environment variable is only used when you use java.exe command without -cp, -classpath and -jar arguments. Any other way you use to execute the Java application ignores this environment variable. This includes executing the application inside an IDE like Eclipse, Netbeans and JCreator.
In an IDE, you instead need to drop the JAR in the project and add it to the "Build Path" if not done automatically by the IDE yet, depending on the project's structure. This is often a matter of rightclicking the JAR in project and choosing "Add to Build Path" somewhere in the context menu.
Forget about using the CLASSPATH environment variable. It was a mistake by Sun. They thought to convince starters by avoiding to enter the -cp or -classpath arguments everytime for javac/java commands. But it end up to be only more confusing to starters as they interpret that environment variable as "the" classpath.
The problem is Classpath or the Driver you used.
Try to run this using -
java -cp C:\Program Files\Java\jre7\lib\postgresql-9.1-901.jdbc4.jar CreateInsert
And send the report

Categories