SQLException even after loading JDBC driver correctly - java

I'm new to java and I am using Visual Studio Code for making a Java project. I'm trying to write SQL queries after loading driver in Visual Studio Code, but I'm repeatedly getting SQLException. Here is my project folder:
src
-com/folder
-db
-DBConnection.java
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(path, username, password);
} catch (ClassNotFoundException | SQLException ex) {
ex.printStackTrace();
return false;
}
Every time I run, I get the following error
Exception in thread "main" java.sql.SQLException: No suitable driver
found for jdbc:mysql:localhost:3306/assign
I'm getting correct results when I run the same program in the src folder.

The most common reason for this type of error is missing updated jar in classpath folder.
If you are using latest version of jdbc jar make sure it correctly points to classpath where the jar is located.
Hint: put some debug statement before invoking of class so that you can be sure correct jar is invoking everytime.

Related

Trying to connect to a SQLite database, keep getting no Suitable Driver Found

When I call The staement
c = DriverManager.getConnection("jdbc:sqlite:sample.db");
I get a exception saying
no Suitable Driver Found for jdbc:sqlite:sample.db
The project is running in eclipse on a windows platform.
This is what I did:
WENT TO properties-> java build path -> Add external jar
added the file sqllite-3-16-1.jar
The code
try {
// exception goes off here
c = DriverManager.getConnection("jdbc:sqlite:sample.db");
} catch(Exception e){
ted=ted+1;
} //
You need to load the driver class before trying to acquire the connection. Can you try the following:
Class.forName("org.sqlite.JDBC");
This will throw NoClassDefFoundError if the driver jar is not on the classpath.

Can't connect to MySQL database: No suitable driver found

I'm having some troubles with java and jdbc.
In particular, while my code perfectly works in a NetBeans project, when i try to execute it on a terminal or on my ubuntu vps (which is where i need it to work) i always get this exception:
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/quakes
First thing first: yes, i'm adding the jdbc .jar to the execute command and the compile command; Yes, i've even tried to add
Class.forName("com.mysql.jdbc.Driver");,
but I always get a ClassNotFoundException: com.mysql.jdbc.Driver exception
The .jar i'm using is the exact same that i use in the NetBeans project, so i know i have the right thing, and even downloading it again from the official site won't change a thing.
Yes, the database exists, and the result doesn't change if i try to connect to another db.
I also tried switching to postgresql (yes, i didn't forget to change the url), but to no avail, it still can't find the driver.
With this, i'm guessing that the actual error is in the compile/execute commands, but even them should be ok:
javac *.java <-cp mysql-connector-java-5.1.41-bin.jar > (the <> parenthesis means that i tried compiling with and without specifying the classpath);
java TAW -cp mysql-connector-java-5.1.41-bin.jar,
In case you want to see it, here's the method that tries to connect to the database:
public Connection getConnection() throws SQLException {
if (conn == null) {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+
this.dbname,this.user,this.pass);
}
return conn;
}
Does anyone have any idea on why this happens?
You need to put the classpath option for java before the name of your main class, otherwise it is regarded as program arguments:
java -cp mysql-connector-java-5.1.41-bin.jar;. TAW
You can use this method step by step to create connetion.
it is an example connectivity:
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost/feedback?"
+ "user=sqluser&password=sqluserpw");

Class Not Found Exception, even after putting the jar file and setting the classpath in Mac OS X

I am trying to make a java application which would use MySQL as database. I have successfully done this number of times using Windows but I am facing problems with Mac OS. Even after placing the "my sql connector" jar file in lib folder and setting proper classpath, I am getting an exception of
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
Screenshot of code along with exception.
Please Help.
As you want to connect to MySQL, you need to use the right Driver class - see MySQL Documentation on MySQL Connector/J
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
}
}
}
Please note, your JDBC url is also wrong, or do you try to connect via odbc? (MySQL JDBC URL for example: jdbc:mysql://localhost:3306/mydatabase , driver class: com.mysql.jdbc.Driver)

java.sql.SQLException: No suitable driver found for jdbc:sqlserver tried classpath already

The sqljdbc4.jar isn't loaded or recognized even after following the steps from:
java.sql.SQLException: No suitable driver found for jdbc:derby:
With this command the application does not start outside netbeans. Starting it only with: java -jar FXProductWatcher.jar # does not load the driver .jar
PS C:\Documents and Settings\User1\Meus
documentos\x\workspace\FXProductWatcher\dist> java -classpath
'C:\Documents and Settings\User1\Meus
documentos\x\workspace\FXProductWatcher\dist\lib\sqljdbc4.jar;FXProductWatcher.jar'
FXProductWatcher
Results in> Error not able to locate or load main class FXProductWatcher
I'm using netbeans the manifest file is being generated automatically. I tried placing Class-Path: lib/sqljdbc4.jar but with no positive result.
The manifest from netbeans has:
JavaFX-Application-Class: fxproductwatcher.FXProductWatcher
JavaFX-Class-Path: lib/sqljdbc4.jar
Created-By: JavaFX Packager
Main-Class: com/javafx/main/Main
My connection code
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(DbConnection.class.getName()).log(Level.SEVERE, null, ex);
}
String sDbUrl = "jdbc:sqlserver://remoteIP;databaseName=test";
conn = DriverManager.getConnection(sDbUrl, username, password);
return conn;
Thanks for any help or sugestion.
Brother the url which you are using is :: jdbc:sqlserver://remoteIP;databaseName=test
the ":"after sqlserver is not valid.
try this: jdbc:sqlserver//remoteIP;databaseName=test
this may work.
good luck.

JDBC connection works in IDE (eclipse), but not in .jar file

I'm really looking at a mystery here. I created a Java program in Eclipse and established a JDBC connection. The code is the following:
import java.sql.*;
public class Login {
public static void main(String[] args) {
try {
// Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection connection = DriverManager
.getConnection("jdbc:sqlserver://localhost;databaseName=testdb; integratedSecurity=true;");
//adding port 1433 doesn't make a difference
System.out.println("Connection successful");
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select * from testtable");
while (rs.next()) {
line = rs.getString(2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
When I'm running it within eclipse, everything's fine. The database and tables are good, library and authentication in the Java Built Path are set and I get the result I want, namely "Hello World", which is a string in the selected table.
But creating an executable JAR file and running it throws the exception "This driver is not configured for integrated authentication". It does not even connect, so even if there was an error in the database it wouldn't matter at this point.
Even more confusing is the fact, that some weeks earlier, I also created an executable JAR file and it worked!
OS: Windows Vista Home Premium x86
JDBC driver location: "C:\sqljdbc_4.0\enu\sqljdbc4.jar"
Native library location: "C:\sqljdbc_4.0\enu\auth\x86"
Java version: Java 7 Update 17 and JDK 7 Update 17
Previousely, I used Update 21, but I changed back to check if the driver could be the reason.
So, any suggestions? I'd be very grateful!
Check the runtime classpath of eclipse, then try to create a system level "CLASSPATH" variable and add the same path. Once the variable is set, execute it in new command prompt or reload the environment variables
Make sure your executable jar has the dependency in MANIFEST.MF for all the necessary jars (along with MainClass)
Finally, It is always a practice in Java to make the first alphabet in Capital ("Login" instead of "login")
Actual problem is that if you run your project from eclipse, it will automatically add run arguments that are defined in run/run config//arguments, so you run your program with properly set path to sqljdbc_auth.dll. But if you export your application to .jar file and just run it, you will get these errors cause there is no correct classpath to auth file.
So, if you run your app from command line with proper arguments, it should be working.

Categories