Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver - java

I am new in java Jdbc I am going to write simple jdbc program `
import java.sql.*;
class jdbcDemo
{
public static void main(String[] args) throws Exception
{
Class.forName("oracle.jdbc.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:#localhost:1521:xe","Scott","tiger");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from rk");
while(rs.next())
{
System.out.println(rs.getInt(1)+"---"+rs.getString(2)+"---"+rs.getInt(3));
}
con.close();
}
}
after Successful compiling program
At runtime i found following error
I had used editplus editor
and also i had created rk table in oracle 12c database
need help to fix issue

You need to provide the class for oracle driver. It will be in a jar which needs to be in class path. Since you are running from command prompt, this needs to be passed to run the class successfully.

As you load class of oracle driver in your code :
Class.forName("oracle.jdbc.OracleDriver");
you just need to add oracle jdbc drivers(ojdbc6 or ojdbc7 ) into your project .
you can download this jar file from thislink :
JDBC DOWNLOAD LINK

Related

Java JDBC Driver stops working after initial execution [duplicate]

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 1 year ago.
I am currently making a backend reporting system (for a voting system assignment) using Java on VS Code, I am connecting to a MySQL database using the JDBC library in order to do calculations and stats and so on. So what happens is that once I create a project file and include the mysql-connector-java-8.0.25.jar in the referenced libraries, I can connect to the DB and retrieve data from the tables just fine, but after a few executions I no longer get output and it shows me the error "java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver".
Can anyone tell me why this is happening and how to fix this? There are no changes that I know of taking place in the Environment Variables (at least from what I can see in Windows path list) unless something is being overwritten somewhere or that it's a bug of some sort. Any advice would be greatly helpful, I've been unable to figure this out all day
This is what my ReportSystem.java looks like...
import java.sql.*;
public class ReportSystem
{
public static void main(String[] args)
{
//Test driver connection/registration
try
{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ElectionDB","<username>","<password>");
Statement stmt = conn.createStatement();
ResultSet result = stmt.executeQuery("SELECT * FROM ElectionDB.Votes");
int typeColumn = 1;
int districtColumn = 2;
//Output results line by line
while(result.next())
{
System.out.println(result.getString(typeColumn));
System.out.println(result.getString(districtColumn));
}
//Remember to close the connection
conn.close();
}
catch (Exception e)
{
System.out.println(e);
}
}
}
My file structure as in the directory is as follows:
ReportSystem
>src > ReportSystem.java
> ReportSystem.class
>lib
>.vscode > settings.json
The JRE system library used is: [jdk-16.0.1]
The Referenced Libraries contains: [mysql-connector-java-8.0.25.jar]
Screenshot for context Project Setup in VS Code
I'm able to get result after running code 20 times continuously by clicking the run button. The only difference is the JDBC connection string, which is copied directly by right clicking the MYSQL connection:
So in my project, the connection string is like:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=username","<username>","<password>");
OR
You could try
String url="jdbc:mysql://localhost:3306/ElectionDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC"
For your reference, OS Infomation:
MySQL: 8.0 // mysql-connector-java-8.0.25.jar
VSCode: 1.56.2 //
java.home: JDK16 // Debugger for Java: 0.33.1

How to fix java.sql.SQLException. No suitable driver for jdbc [duplicate]

This question already has answers here:
Getting the following error - No suitable driver found for jdbc:postgresql://localhost: 5432/testDBMS
(4 answers)
Closed 3 years ago.
I'm getting an error java.sql.SQLException No suitable driver for jdbc:derby:books when I try to run a file from command line. In Eclipse, everything works fine. I read a book "Java, How To Program" Deitel&Deitel and the file is an example from it. When I try to compile program from command line it shows no error, but the problem is with running. Please help
public class DisplayAuthors {
public static void main(String args[]) {
final String DATABASE_URL = "jdbc:derby:books";
final String SELECT_QUERY =
"SELECT authorID, firstName, lastName FROM authors";
String user="deitel";
String password="deitel";
try (
Connection connection = DriverManager.getConnection(
DATABASE_URL, user,password);
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(SELECT_QUERY)) {
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.printf("Table Authors database books:%n%n");
for (int i = 1; i <= numberOfColumns; i++) {
System.out.printf("%-8s\t", metaData.getColumnName(i));
}
System.out.println();
while (resultSet.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
System.out.printf("%-8s\t", resultSet.getObject(i));
}
System.out.println();
}
}
catch (SQLException sqlException) {
sqlException.printStackTrace();
}
}
}
Command line execution:
javac DisplayAuthors.java
java DisplayAuthors
You are running in the command line your class file without the dependencies.
In your eclipse IDE you maybe have a derby.jar or similar dependencies and eclipse adds all automatically to the execution. Is required to add all the dependencies when you are executing directly from the command line.
If you are note creating a runnable jar with dependencies in MANIFEST.MF and you are trying to execute the class directly is required to add the -cp parameter with the path to all the dependencies:
Example:
java -cp Derby.jar;. DisplayAuthors
Summing that the Derby.jar and your class are in the same place and there is no more dependencies to add.
More information about:
Java Command line (Oracle Java9 SE)
Differences between "java -cp" and "java -jar"?
java.sql.SQLException. No suitable driver for jdbc
The above error jumps when JDBC DriverManager can't find any suitable driver for the given connection URL. Either the JDBC driver isn't loaded at all before connecting the DB, or the connection URL is wrong.
The URL should be like this,
jdbc:derby://localhost:1527/dbname;create=true;
or
jdbc:derby:books;create=true;
Use create=true if you want the database to be created if it doesn't exist.
And finally, check that Derby JAR file is on the classpath. If you can't find it, then you can download the JAR from here and add to the project Library folder.
For Apache Derby, the driver class name is org.apache.derby.jdbc.ClientDriver. So put that as follows,
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection connection = DriverManager.getConnection(DATABASE_URL, user, password);
Make sure your URL, username and the password is correct, and try to run your code.

Why I obtain this SQLException (No suitable driver) when I try to create a Connection object for an Oracle database?

I have to create a very simple batch Java application (an application that run into shell) and that perform some query on an Oracle database using JDBC and I never done it.
I am following this tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/JDBC_Basic.html
So I have done in this way to allocate a new Connection object for my application:
import java.sql.*;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World !!!");
String partitaIVA = args[0];
String nomePDF = args[1];
Connection conn = null;
Statement stmt = null;
try {
// Step 1: Allocate a database "Connection" object
conn = DriverManager.getConnection("jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1", "myUserName", "myPswd"); // Oracle DB
} catch(SQLException ex) {
ex.printStackTrace();
}
}
}
The problem is that when I run the application it seems that the SQLException is thrown because enter into the catch block and print the following error message in the console:
java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:#XXX.XXX.XXX.XXX:1521:eme1
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at Main.main(Main.java:16)
Why? What cause this problem and how can I fix this issue? What am I missing?
Tnx
You need to have an appropriate ojdbc.jar in you classpath. E.g. see Oracle JDBC ojdbc6 Jar as a Maven Dependency
The problem should be that the linked tutorial describes how to connect to a Mysql, but you're trying to connect to an oracle - therefore you need the oracle driver in your classpath.
How about following oracle's documentation for its Driver. http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html
In the above code, you didn't registered the driver class.
Class.forName ("oracle.jdbc.OracleDriver");
If you are following instructions from the given link.
In chapter 2.2 you have instructions how to instal MySql drivers.
If you are using an Oracle database, then you'll need to instal Oracle JDBC drivers.
If you don't want affect your JDK installation like in tutorial, you can load driver dynamicaly

How to connect to db by piping password

Im trying to connect to database using java...
I used the following snipphet but am unable to connect to db
p = Runtime.getRuntime().exec("ssh -p 4645 username#example.com|send password");
for connecting a database you don't need to use Runtime.getRuntime() its is use to execute system command in java for connecting any database in java you have to use its jar file like for and example i want to connect my code to the mysql database the for that i need mysql jar file and calling its lib in my code like:
import java.sql.*;
class demo
{
public static void main(String ar[])throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver")
Connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root", "password");
Statement st = con.createStatement();
/*
your sql query here
*/
}
}

Getting Error when connection to MySQL database

I have downloaded the following:
mysql-essential-5.1.65-win32 from this MySQL Dev link
MySQL Connector mysql-connector-java-5.1.21.zip from this link
Now I have started programming with Eclipse. I have made simple java class like below,
public class MySQLAccess {
private static Connection connect = null;
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public static void main(String[] args){
try{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
}catch (Exception e) {
// TODO: handle exception
System.out.println("Error : "+e);
}
}
}
I have also made a folder "lib" in my Java project and I have put that mysql-connector jar over there. But when I run this program it can't find mysql I get the following error in the console :
Erro : java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Can someone please tell me where I have made the mistake? Thank you
Putting the full jar-file path in your classpath and restarting cmd (if you are running from cmd) should work-
See here- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
If it your java application project then you put jar file into jdk library path ext folder.
eg:
C:\Program Files\Java\jdk1.6.0_13\jre\lib\ext
you will get path from your project class path .

Categories

Resources