This question already has answers here:
How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
(13 answers)
The infamous java.sql.SQLException: No suitable driver found
(21 answers)
Closed 3 years ago.
I've been working on a web project with Java EE and PostgreSQL and it runs perfectly. I tried moving it to another machine, but now it's not connecting to the database anymore.
I tried multiple solutions but nothing works so far:
adding postgresql.jar to java build path
moving it to the /lib file of tomcat server
Hers is my connection to database class:
public class DBConnexion {
private static Connection con=null;
private DBConnexion(){
try
{
try {
Class.forName("org.postgresql.Driver");
//getConnection(url:dataBase name, owner name , password)
con=(Connection)DriverManager.getConnection("jdbc:postgresql:septentrion", "postgres","123");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
public static Connection getInstance()
{
if(con==null)
new DBConnexion();
return con;
}
}
Depending on your dependency manager, you will have to add postgresql as your dependency. For example if you are using Maven then
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.6</version>
</dependency>
If you are not using anything(which I think is the case), then you will have to manually download the jar file and put it in your classpath.
EDIT: Since you do not use any dependency manager,
if you want to compile com.example.Foo that depends on lib/postgresql-42.2.6.jar you might use the following incantation:
javac -classpath lib/postgresql-42.2.6.jar com/example/Foo.java
Related
Since yesterday I have been trying to figure out how to fix this SQLException, and at this point I'm empty. I have checked dozens of sites, the whole documentation for Microsoft and Azure on how to connect with databases, and I keep getting the same error.
I've added a CLASSPATH like CLASSPATH =.;C:\Program Files\Microsoft JDBC Driver 8.2 for SQL Server\sqljdbc_8.2\enu\mssql-jdbc-8.2.2.jre11.jar as informed on Using the JDBC Driver documentation. I've downloaded the JDBC driver from Microsoft themselves and dumped the files on a folder in C:\Program Files\Microsoft JDBC DRIVER 8.4 for SQL Server as their documentation suggests. My maven and java are working fine, and I have an example class that I've got from SQLExamples by Microsoft themselves, this one:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// As informed on:
// https://www.microsoft.com/en-us/sql-server/developer-get-started/java/windows/step/2.html
public class App {
public static void main(String[] args) {
String connectionUrl = "jdbc:sqlserver:[AzureAddressHere];"
+ "database=[DatabaseHere];"
+ "user=[UserHere];"
+ "password=[PasswordHere]";
try {
// Load SQL Server JDBC driver and establish connection.
System.out.print("Connecting to SQL Server ... ");
try (Connection connection = DriverManager.getConnection(connectionUrl)) {
System.out.println("Done.");
}
} catch (SQLException e) {
System.out.println();
e.printStackTrace();
}
}
}
I've also added the references to the jar file to the pom.xml dependencies list:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>8.4.1.jre11</version>
</dependency>
I've also found a mention in a website that the jar file sits in Maven's library folder, so I've copied the jar file to the lib folder as well.
Yet, whenever I try to run the app, I get the java.sql.SQLException: No suitable driver found for [AzureInformation].
Any ideas on what I could be missing here?
Turns out that the issue was the lack of two // in the address. facepalm
So, I just set this line to: String connectionUrl = "jdbc:sqlserver://[AzureAddressHere];" and it finally connected.
To be fair to myself here, this error is so completely unclear and unhelpful.
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"
This question already has answers here:
Missing artifact com.microsoft.sqlserver:sqljdbc4:jar:4.0
(11 answers)
Closed 6 years ago.
How do I install MSSQL driver in my project with Eclipse's maven? (m2e)? And also make it not conflict with Vaadin? When I install the MSSQL driver locally following these instructions, during the compile and runtime, it says "Could not find archetype from Vaadin-addons."
I have the code:
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://server;databaseName=dbname;user=username;password=password");
String sql = "Select * from Table1";
stmt = con.createStatement( );
rs = stmt.executeQuery(sql);
while (rs.next()){
contractorsList.addBean(new Contractor(rs.getString(1),
rs.getString(2),
rs.getString(3),
rs.getString(4),
rs.getString(5),
rs.getString(6)));
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}finally{
try { con.close(); } catch (SQLException e) {}
try { rs.close(); } catch (SQLException e) {}
try { stmt.close(); } catch (SQLException e) {}
I'm getting the following error:
java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:450)
at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:403)
at java.lang.Class.forName0(Native Method)
Which means that it can't find my driver, I think. So I'm following this tutorial on installing the MS SQL driver. I download the driver from microsoft and then I unzip it here:
C:\SQLDriver
In Eclipse, on my project folder, I right click > Debug As > Debug Configurations >
mvn install:install-file -Dfile=C:\SQLDriver\sqljdbc_4.0\enu\sqljdbc4.jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0.2206.100 -Dpackaging=jar
Then I add in the following in my POM
<properties>
<!–….other versions–>
<sqlserver.version>4.0.2206.100</sqlserver.version>
</properties>
...
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqlserver.version}</version>
</dependency>
I get an error that it can't find my dependency, So I look up the question here, and I found out that I simply need to add sqljdbc4 to my classpath.
I have no idea what that means but google took me here So I attempt to follow the instructions.
In System Properties > Advanced > Environment Variable under Variables, I add the following:
I searched here and it looks like someone else asked the same question
I honestly don't know what people mean by add it to the classpath. Do they mean paste the jars in the same folder as the project? Either way, I just found a .classpath file in my project and added these:
How do I get maven to use this microsoft sql driver?
[Edit] When going to Libraries > Maven Dependencies, right click > build path > configure build path, I see this:
Which, when I click, it doesn't let me edit the path of that file. I'm not sure where to edit it.
You will need to download the source code and the java docs for all the maven dependencies. You can do that by right clicking the maven as discussed here:
https://stackoverflow.com/a/22352526/1475228
Then only you can run the code.
Also look here. This is the one that helped mine.
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)
I am trying to open a MySQL connection but run into this error.
java.lang.ClassNotFoundException: com.mysql.jdbc.driver
There are plenty of questions online about this but I cannot seem to make it work.
I currently using gradle to compile 'com.oracle:ojdbc14:10.2.0.4.0' and I have added the connector library using the build in maven importer.
My code looks like this:
private static final String DRIVER = "com.mysql.jdbc.driver";
...
try {
try {
Class.forName(DRIVER).newInstance();
} catch (Exception e)
{
e.printStackTrace();
}
...
}
I tried to add ojdbc-14.jar to libs and reference that in gradle without success. I also use some groovy code in the build.settings but without success, I'm not sure how to work with that either.
A pic of my IDE:
The error is in your DRIVER string. It should be
DRIVER = "com.mysql.jdbc.Driver"