"Losing" dependency why building jar with maven [duplicate] - java
I'm trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps).
The app itself talks to Postgres just fine, so I know that the database is up, user can access it, all that good stuff. What I'm trying to do is a database query in a JSP that I've added. I've used the config example in the Tomcat datasource example pretty much out of the box. The requisite taglibs are in the right place -- no errors occur if I just have the taglib refs, so it's finding those JARs. The postgres jdbc driver, postgresql-8.4.701.jdbc3.jar is in $CATALINA_HOME/common/lib.
Here's the top of the JSP:
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/mmas">
select current_validstart as ValidTime from runoff_forecast_valid_time
</sql:query>
The relevant section from $CATALINA_HOME/conf/server.xml, inside the <Host> which is in turn within <Engine>:
<Context path="/gs2" allowLinking="true">
<Resource name="jdbc/mmas" type="javax.sql.Datasource"
auth="Container" driverClassName="org.postgresql.Driver"
maxActive="100" maxIdle="30" maxWait="10000"
username="mmas" password="very_secure_yess_precious!"
url="jdbc:postgresql//localhost:5432/mmas" />
</Context>
These lines are the last in the tag in webapps/gs2/WEB-INF/web.xml:
<resource-ref>
<description>
The database resource for the MMAS PostGIS database
</description>
<res-ref-name>
jdbc/mmas
</res-ref-name>
<res-type>
javax.sql.DataSource
</res-type>
<res-auth>
Container
</res-auth>
</resource-ref>
Finally, the exception:
exception
org.apache.jasper.JasperException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver"
[...wads of ensuing goo elided]
The infamous java.sql.SQLException: No suitable driver found
This exception can have basically two causes:
1. JDBC driver is not loaded
In case of Tomcat, you need to ensure that the JDBC driver is placed in server's own /lib folder.
Or, when you're actually not using a server-managed connection pool data source, but are manually fiddling around with DriverManager#getConnection() in WAR, then you need to place the JDBC driver in WAR's /WEB-INF/lib and perform ..
Class.forName("com.example.jdbc.Driver");
.. in your code before the first DriverManager#getConnection() call whereby you make sure that you do not swallow/ignore any ClassNotFoundException which can be thrown by it and continue the code flow as if nothing exceptional happened. See also Where do I have to place the JDBC driver for Tomcat's connection pool?
Other servers have a similar way of placing the JAR file:
GlassFish: put the JAR file in /glassfish/lib
WildFly: put the JAR file in /standalone/deployments
2. Or, JDBC URL is in wrong syntax
You need to ensure that the JDBC URL is conform the JDBC driver documentation and keep in mind that it's usually case sensitive. When the JDBC URL does not return true for Driver#acceptsURL() for any of the loaded drivers, then you will also get exactly this exception.
In case of PostgreSQL it is documented here.
With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL™, this takes one of the following forms:
jdbc:postgresql:database
jdbc:postgresql://host/database
jdbc:postgresql://host:port/database
In case of MySQL it is documented here.
The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional:
jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...]
In case of Oracle it is documented here.
There are 2 URL syntax, old syntax which will only work with SID and the new one with Oracle service name.
Old syntax jdbc:oracle:thin:#[HOST][:PORT]:SID
New syntax jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE
See also:
Where do I have to place the JDBC driver for Tomcat's connection pool?
How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception
How should I connect to JDBC database / datasource in a servlet based application?
What is the difference between "Class.forName()" and "Class.forName().newInstance()"?
Connect Java to a MySQL database
I've forgot to add the PostgreSQL JDBC Driver into my project (Mvnrepository).
Gradle:
// http://mvnrepository.com/artifact/postgresql/postgresql
compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4'
Maven:
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.0-801.jdbc4</version>
</dependency>
You can also download the JAR and import to your project manually.
url="jdbc:postgresql//localhost:5432/mmas"
That URL looks wrong, do you need the following?
url="jdbc:postgresql://localhost:5432/mmas"
I faced the similar issue.
My Project in context is Dynamic Web Project(Java 8 + Tomcat 8) and error is for PostgreSQL Driver exception: No suitable driver found
It got resolved by adding Class.forName("org.postgresql.Driver") before calling getConnection() method
Here is my Sample Code:
try {
Connection conn = null;
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://" + host + ":" + port + "/?preferQueryMode="
+ sql_auth,sql_user , sql_password);
} catch (Exception e) {
System.out.println("Failed to create JDBC db connection " + e.toString() + e.getMessage());
}
I found the followig tip helpful, to eliminate this issue in Tomcat -
be sure to load the driver first doing a Class.forName("
org.postgresql.Driver"); in your code.
This is from the post - https://www.postgresql.org/message-id/e13c14ec050510103846db6b0e#mail.gmail.com
The jdbc code worked fine as a standalone program but, in TOMCAT it gave the error -'No suitable driver found'
No matter how old this thread becomes, people would continue to face this issue.
My Case: I have the latest (at the time of posting) OpenJDK and maven setup. I had tried all methods given above, with/out maven and even solutions on sister posts on StackOverflow. I am not using any IDE or anything else, running from bare CLI to demonstrate only the core logic.
Here's what finally worked.
Download the driver from the official site. (for me it was MySQL https://www.mysql.com/products/connector/). Use your flavour here.
Unzip the given jar file in the same directory as your java project. You would get a directory structure like this. If you look carefully, this exactly relates to what we try to do using Class.forName(....). The file that we want is the com/mysql/jdbc/Driver.class
Compile the java program containing the code.
javac App.java
Now load the director as a module by running
java --module-path com/mysql/jdbc -cp ./ App
This would load the (extracted) package manually, and your java program would find the required Driver class.
Note that this was done for the mysql driver, other drivers might require minor changes.
If your vendor provides a .deb image, you can get the jar from /usr/share/java/your-vendor-file-here.jar
Summary:
Soln2 (recommend)::
1 . put mysql-connector-java-8.0.28.jar file in the <where you install your Tomcat>/lib.
Soln1::
1 . put mysql-connector-java-8.0.28.jar file in the WEB-INF/lib.
2 . use Class.forName("com.mysql.cj.jdbc.Driver"); in your Servlet Java code.
Soln1 (Ori Ans) //-20220304
In short:
make sure you have the mysql-connector-java-8.0.28.jar file in the WEB-INF/lib
make sure you use the Class.forName("com.mysql.cj.jdbc.Driver");
additional notes (not important), base on my trying (could be wrong)::
1.1 putting the jar directly inside the Java build path doesnt work
1.2. putting the jar in Data management > Driver Def > MySQL JDBC Driver > then add it as library to Java Build path doesnt work.
1.3 => it has to be inside the WEB-INF/lib (I dont know why)
1.4 using version mysql-connector-java-8.0.28.jar works, only version 5.1 available in Eclipse MySQL JDBC Driver setting doesnt matter, ignore it.
<see How to connect to MySql 8.0 database using Eclipse Database Management Perspective >
Class.forName("com.mysql.cj.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver");
both works,
but the Class.forName("com.mysql.jdbc.Driver"); is deprecated.
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.
<see https://www.yawintutor.com/no-suitable-driver-found-for-jdbcmysql-localhost3306-testdb/ >
If you want to connect to a MySQL database, you can use the type-4 driver named Connector/} that's available for free from the MySQL website. However, this driver is typically included in Tomcat's lib directory. As a result, you don't usually need to download this driver from the MySQL site.
-- Murach’s Java Servlets and JSP
I cant find the driver in Tomcat that the author is talking about, I need to use the mysql-connector-java-8.0.28.jar.
<(striked-out) see updated answer soln2 below>
If you're working with an older version of Java, though, you need to use the forName method of the Class class to explicitly load the driver before you call the getConnection method
Even with JDBC 4.0, you sometimes get a message that says, "No suitable driver found." In that case, you can use the forName method of the Class class to explicitly load the driver. However, if automatic driver loading works, it usually makes sense to remove this method call from your code.
How to load a MySQL database driver prior to JDBC 4.0
Class.forName{"com.mysql.jdbc.Driver");
-- Murach’s Java Servlets and JSP
I have to use Class.forName("com.mysql.cj.jdbc.Driver"); in my system, no automatic class loading. Not sure why.
<(striked-out) see updated answer soln2 below>
When I am using a normal Java Project instead of a Dynamic Web Project in Eclipse,
I only need to add the mysql-connector-java-8.0.28.jar to Java Build Path directly,
then I can connect to the JDBC with no problem.
However, if I am using Dynamic Web Project (which is in this case), those 2 strict rules applies (jar position & class loading).
<see TOMCAT ON ECLIPSE java.sql.SQLException: No suitable driver found for jdbc:mysql >
Soln2 (Updated Ans) //-20220305_12
In short:
1 . put mysql-connector-java-8.0.28.jar file in the <where you install your Tomcat>/lib.
eg: G:\pla\Java\apache-tomcat-10.0.16\lib\mysql-connector-java-8.0.28.jar
(and for an Eclipse Dynamic Web Project, the jar will then be automatically put inside in your project's Java build path > Server Runtime [Apache Tomcat v10.0].)
Additional notes::
for soln1::
put mysql-connector-java-8.0.28.jar file in the WEB-INF/lib.
use Class.forName("com.mysql.cj.jdbc.Driver"); in your Servlet Java code.
this will create an WARNING:
WARNING: The web application [LearnJDBC] appears to have started a thread named [mysql-cj-abandoned-connection-cleanup] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
<see The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread >
and that answer led me to soln2.
for soln2::
put mysql-connector-java-8.0.28.jar file in the <where you install your Tomcat>/lib.
this will create an INFO:
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
you can just ignore it.
<see How to fix "JARs that were scanned but no TLDs were found in them " in Tomcat 9.0.0M10 >
(you should now understand what Murach’s Java Servlets and JSP was talking about: the jar in Tomcat/lib & the no need for Class.forName("com.mysql.cj.jdbc.Driver");)
to kinda fix it //-20220307_23
Tomcat 8.5. Inside catalina.properties, located in the /conf directory set:
tomcat.util.scan.StandardJarScanFilter.jarsToSkip=\*.jar
How to fix JSP compiler warning: one JAR was scanned for TLDs yet contained no TLDs?
It might be worth noting that this can also occur when Windows blocks downloads that it considers to be unsafe. This can be addressed by right-clicking the jar file (such as ojdbc7.jar), and checking the 'Unblock' box at the bottom.
Windows JAR File Properties Dialog:
As well as adding the MySQL JDBC connector ensure the context.xml (if not unpacked in the Tomcat webapps folder) with your DB connection definitions are included within Tomcats conf directory.
A very silly mistake which could be possible resulting is adding of space at the start of the JDBC URL connection.
What I mean is:-
suppose u have bymistake given the jdbc url like
String jdbcUrl=" jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
(Notice there is a space in the staring of the url, this will make the error)
the correct way should be:
String jdbcUrl="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC";
(Notice no space in the staring, you may give space at the end of the url but it is safe not to)
Run java with CLASSPATH environmental variable pointing to driver's JAR file, e.g.
CLASSPATH='.:drivers/mssql-jdbc-6.2.1.jre8.jar' java ConnectURL
Where drivers/mssql-jdbc-6.2.1.jre8.jar is the path to driver file (e.g. JDBC for for SQL Server).
The ConnectURL is the sample app from that driver (samples/connections/ConnectURL.java), compiled via javac ConnectURL.java.
I was using jruby, in my case I created under config/initializers
postgres_driver.rb
$CLASSPATH << '~/.rbenv/versions/jruby-1.7.17/lib/ruby/gems/shared/gems/jdbc-postgres-9.4.1200/lib/postgresql-9.4-1200.jdbc4.jar'
or wherever your driver is, and that's it !
I had this exact issue when developing a Spring Boot application in STS, but ultimately deploying the packaged war to WebSphere(v.9). Based on previous answers my situation was unique. ojdbc8.jar was in my WEB-INF/lib folder with Parent Last class loading set, but always it says it failed to find the suitable driver.
My ultimate issue was that I was using the incorrect DataSource class because I was just following along with online tutorials/examples. Found the hint thanks to David Dai comment on his own question here: Spring JDBC Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver]
Also later found spring guru example with Oracle specific driver: https://springframework.guru/configuring-spring-boot-for-oracle/
Example that throws error using org.springframework.jdbc.datasource.DriverManagerDataSource based on generic examples.
#Config
#EnableTransactionManagement
public class appDataConfig {
\* Other Bean Defs *\
#Bean
public DataSource dataSource() {
// configure and return the necessary JDBC DataSource
DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:oracle:thin:#//HOST:PORT/SID", "user", "password");
dataSource.setSchema("MY_SCHEMA");
return dataSource;
}
}
And the corrected exapmle using a oracle.jdbc.pool.OracleDataSource:
#Config
#EnableTransactionManagement
public class appDataConfig {
/* Other Bean Defs */
#Bean
public DataSource dataSource() {
// configure and return the necessary JDBC DataSource
OracleDataSource datasource = null;
try {
datasource = new OracleDataSource();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
datasource.setURL("jdbc:oracle:thin:#//HOST:PORT/SID");
datasource.setUser("user");
datasource.setPassword("password");
return datasource;
}
}
I was having the same issue with mysql datasource using spring data that would work outside but gave me this error when deployed on tomcat.
The error went away when I added the driver jar mysql-connector-java-8.0.16.jar to the jres lib/ext folder
However I did not want to do this in production for fear of interfering with other applications. Explicity defining the driver class solved this issue for me
spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
You will get this same error if there is not a Resource definition provided somewhere for your app -- most likely either in the central context.xml, or individual context file in conf/Catalina/localhost. And if using individual context files, beware that Tomcat freely deletes them anytime you remove/undeploy the corresponding .war file.
For me the same error occurred while connecting to postgres while creating a dataframe from table .It was caused due to,the missing dependency. jdbc dependency was not set .I was using maven for the build ,so added the required dependency to the pom file from maven dependency
jdbc dependency
For me adding below dependency to pom.xml file just solved like magic! I had no mysql connector dependency and even adding mssql jdbc jar file to build path did not work either.
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.0.jre11</version>
</dependency>
In my case I was working on a Java project with Maven and encountered this error.
In your pom.xml file make sure you have this dependencies
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
</dependencies>
and where you create connection have something like this
public Connection createConnection() {
try {
String url = "jdbc:mysql://localhost:3306/yourDatabaseName";
String username = "root"; //your my sql username here
String password = "1234"; //your mysql password here
Class.forName("com.mysql.cj.jdbc.Driver");
return DriverManager.getConnection(url, username, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
faced same issue. in my case ':' colon before '//' (jdbc:mysql://localhost:3306/dbname) was missing, and it just fixed the problem.
make sure : and // are placed properly.
I ran into the same error. In my case, the JDBC URL was correct, but the issue was with classpath. However, adding MySQL connector's JAR file to the -classpath or -cp (or, in the case of an IDE, as a library) doesn't resolve the issue. So I will have to move the JAR file to the location of Java bytecode and run java -cp :mysql_connector.jar to make this work. If someone runs into the same issue as mine, I'm leaving this here.
I encountered this issue by putting a XML file into the src/main/resources wrongly, I deleted it and then all back to normal.
Related
Get error java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=ABC;integratedSecurity=true on Production
I created a Web Application using Java, JSP,Tomcat via Eclipse. When running the Application on my Windows development Env, it works fine. database connection is fine (sqljdbc42.jar in Lib via Eclipse) The test db is on my Window development PC Link: http://localhost:8080/App/ To test the db connection on Production is working right, I have a Java file (not related to my project, but using same db driver and url. It's just for test DB connection on production), it connects to db which is located on production correctly. However, after I upload my war file (from local development environment) to Tomcat WebApps folder, I got the error in title, the db on production is not connected successfully. I use the following drivers and url for db connection drivers=com.microsoft.sqlserver.jdbc.SQLServerDriver url=jdbc:sqlserver://localhost;databaseName=ABC;integratedSecurity=true In the above url string, I have tried localhost as above, also tried production IP address, Server name, localhost:1433, localhost\\MSSQLSERVER, all get the same error I have below in ClassPath in production site: C:\sqljdbc42.jar;C:\microsoft-mssqlserver.jar;C:\Program Files\Java\jdk1.8.0_201\bin;C:\Program Files\Java\jre1.8.0_201\bin;. Thank you in advance.
You should check the classloader paths for tomcat server. Regarding the docs, your server will hope finding sqlserver driver jar (sqljdbc42.jar) in the following places: Bootstrap classes of your JVM ($JAVA_HOME/jre/lib/ext) /WEB-INF/classes of your web application /WEB-INF/lib/*.jar of your web application System class loader classes (described through CLASSPATH environment variable) Common class loader classes ($CATALINA_BASE/lib and $CATALINA_HOME/lib) So, best options you can try are: copying the jar to $JAVA_HOME/jre/lib/ext (#1) or configuring your build so that, the jar is copied in /WEB-INF/lib/ (#3) Number#2 is suitable for java classes; does not fit your case. Number#4 and #5 are more likely for the use of tomcat's own structure, but still it is perfectly legal if you want to put the jar in. Further Edit: For some reason, there are cases Tomcat may prevent sqlserver driver for the sake of preventing memory leaks. So, if you're sure the jar is on the classpath by applying the solutions mentioned in beforehand this post; you may also try to check with adding the following to server/.xml to disable that leak check: <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" />
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null' [duplicate]
I'm trying to add a database-enabled JSP to an existing Tomcat 5.5 application (GeoServer 2.0.0, if that helps). The app itself talks to Postgres just fine, so I know that the database is up, user can access it, all that good stuff. What I'm trying to do is a database query in a JSP that I've added. I've used the config example in the Tomcat datasource example pretty much out of the box. The requisite taglibs are in the right place -- no errors occur if I just have the taglib refs, so it's finding those JARs. The postgres jdbc driver, postgresql-8.4.701.jdbc3.jar is in $CATALINA_HOME/common/lib. Here's the top of the JSP: <%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> <%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <sql:query var="rs" dataSource="jdbc/mmas"> select current_validstart as ValidTime from runoff_forecast_valid_time </sql:query> The relevant section from $CATALINA_HOME/conf/server.xml, inside the <Host> which is in turn within <Engine>: <Context path="/gs2" allowLinking="true"> <Resource name="jdbc/mmas" type="javax.sql.Datasource" auth="Container" driverClassName="org.postgresql.Driver" maxActive="100" maxIdle="30" maxWait="10000" username="mmas" password="very_secure_yess_precious!" url="jdbc:postgresql//localhost:5432/mmas" /> </Context> These lines are the last in the tag in webapps/gs2/WEB-INF/web.xml: <resource-ref> <description> The database resource for the MMAS PostGIS database </description> <res-ref-name> jdbc/mmas </res-ref-name> <res-type> javax.sql.DataSource </res-type> <res-auth> Container </res-auth> </resource-ref> Finally, the exception: exception org.apache.jasper.JasperException: Unable to get connection, DataSource invalid: "java.sql.SQLException: No suitable driver" [...wads of ensuing goo elided]
The infamous java.sql.SQLException: No suitable driver found This exception can have basically two causes: 1. JDBC driver is not loaded In case of Tomcat, you need to ensure that the JDBC driver is placed in server's own /lib folder. Or, when you're actually not using a server-managed connection pool data source, but are manually fiddling around with DriverManager#getConnection() in WAR, then you need to place the JDBC driver in WAR's /WEB-INF/lib and perform .. Class.forName("com.example.jdbc.Driver"); .. in your code before the first DriverManager#getConnection() call whereby you make sure that you do not swallow/ignore any ClassNotFoundException which can be thrown by it and continue the code flow as if nothing exceptional happened. See also Where do I have to place the JDBC driver for Tomcat's connection pool? Other servers have a similar way of placing the JAR file: GlassFish: put the JAR file in /glassfish/lib WildFly: put the JAR file in /standalone/deployments 2. Or, JDBC URL is in wrong syntax You need to ensure that the JDBC URL is conform the JDBC driver documentation and keep in mind that it's usually case sensitive. When the JDBC URL does not return true for Driver#acceptsURL() for any of the loaded drivers, then you will also get exactly this exception. In case of PostgreSQL it is documented here. With JDBC, a database is represented by a URL (Uniform Resource Locator). With PostgreSQL™, this takes one of the following forms: jdbc:postgresql:database jdbc:postgresql://host/database jdbc:postgresql://host:port/database In case of MySQL it is documented here. The general format for a JDBC URL for connecting to a MySQL server is as follows, with items in square brackets ([ ]) being optional: jdbc:mysql://[host1][:port1][,[host2][:port2]]...[/[database]] » [?propertyName1=propertyValue1[&propertyName2=propertyValue2]...] In case of Oracle it is documented here. There are 2 URL syntax, old syntax which will only work with SID and the new one with Oracle service name. Old syntax jdbc:oracle:thin:#[HOST][:PORT]:SID New syntax jdbc:oracle:thin:#//[HOST][:PORT]/SERVICE See also: Where do I have to place the JDBC driver for Tomcat's connection pool? How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception How should I connect to JDBC database / datasource in a servlet based application? What is the difference between "Class.forName()" and "Class.forName().newInstance()"? Connect Java to a MySQL database
I've forgot to add the PostgreSQL JDBC Driver into my project (Mvnrepository). Gradle: // http://mvnrepository.com/artifact/postgresql/postgresql compile group: 'postgresql', name: 'postgresql', version: '9.0-801.jdbc4' Maven: <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.0-801.jdbc4</version> </dependency> You can also download the JAR and import to your project manually.
url="jdbc:postgresql//localhost:5432/mmas" That URL looks wrong, do you need the following? url="jdbc:postgresql://localhost:5432/mmas"
I faced the similar issue. My Project in context is Dynamic Web Project(Java 8 + Tomcat 8) and error is for PostgreSQL Driver exception: No suitable driver found It got resolved by adding Class.forName("org.postgresql.Driver") before calling getConnection() method Here is my Sample Code: try { Connection conn = null; Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection("jdbc:postgresql://" + host + ":" + port + "/?preferQueryMode=" + sql_auth,sql_user , sql_password); } catch (Exception e) { System.out.println("Failed to create JDBC db connection " + e.toString() + e.getMessage()); }
I found the followig tip helpful, to eliminate this issue in Tomcat - be sure to load the driver first doing a Class.forName(" org.postgresql.Driver"); in your code. This is from the post - https://www.postgresql.org/message-id/e13c14ec050510103846db6b0e#mail.gmail.com The jdbc code worked fine as a standalone program but, in TOMCAT it gave the error -'No suitable driver found'
No matter how old this thread becomes, people would continue to face this issue. My Case: I have the latest (at the time of posting) OpenJDK and maven setup. I had tried all methods given above, with/out maven and even solutions on sister posts on StackOverflow. I am not using any IDE or anything else, running from bare CLI to demonstrate only the core logic. Here's what finally worked. Download the driver from the official site. (for me it was MySQL https://www.mysql.com/products/connector/). Use your flavour here. Unzip the given jar file in the same directory as your java project. You would get a directory structure like this. If you look carefully, this exactly relates to what we try to do using Class.forName(....). The file that we want is the com/mysql/jdbc/Driver.class Compile the java program containing the code. javac App.java Now load the director as a module by running java --module-path com/mysql/jdbc -cp ./ App This would load the (extracted) package manually, and your java program would find the required Driver class. Note that this was done for the mysql driver, other drivers might require minor changes. If your vendor provides a .deb image, you can get the jar from /usr/share/java/your-vendor-file-here.jar
Summary: Soln2 (recommend):: 1 . put mysql-connector-java-8.0.28.jar file in the <where you install your Tomcat>/lib. Soln1:: 1 . put mysql-connector-java-8.0.28.jar file in the WEB-INF/lib. 2 . use Class.forName("com.mysql.cj.jdbc.Driver"); in your Servlet Java code. Soln1 (Ori Ans) //-20220304 In short: make sure you have the mysql-connector-java-8.0.28.jar file in the WEB-INF/lib make sure you use the Class.forName("com.mysql.cj.jdbc.Driver"); additional notes (not important), base on my trying (could be wrong):: 1.1 putting the jar directly inside the Java build path doesnt work 1.2. putting the jar in Data management > Driver Def > MySQL JDBC Driver > then add it as library to Java Build path doesnt work. 1.3 => it has to be inside the WEB-INF/lib (I dont know why) 1.4 using version mysql-connector-java-8.0.28.jar works, only version 5.1 available in Eclipse MySQL JDBC Driver setting doesnt matter, ignore it. <see How to connect to MySql 8.0 database using Eclipse Database Management Perspective > Class.forName("com.mysql.cj.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver"); both works, but the Class.forName("com.mysql.jdbc.Driver"); is deprecated. 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. <see https://www.yawintutor.com/no-suitable-driver-found-for-jdbcmysql-localhost3306-testdb/ > If you want to connect to a MySQL database, you can use the type-4 driver named Connector/} that's available for free from the MySQL website. However, this driver is typically included in Tomcat's lib directory. As a result, you don't usually need to download this driver from the MySQL site. -- Murach’s Java Servlets and JSP I cant find the driver in Tomcat that the author is talking about, I need to use the mysql-connector-java-8.0.28.jar. <(striked-out) see updated answer soln2 below> If you're working with an older version of Java, though, you need to use the forName method of the Class class to explicitly load the driver before you call the getConnection method Even with JDBC 4.0, you sometimes get a message that says, "No suitable driver found." In that case, you can use the forName method of the Class class to explicitly load the driver. However, if automatic driver loading works, it usually makes sense to remove this method call from your code. How to load a MySQL database driver prior to JDBC 4.0 Class.forName{"com.mysql.jdbc.Driver"); -- Murach’s Java Servlets and JSP I have to use Class.forName("com.mysql.cj.jdbc.Driver"); in my system, no automatic class loading. Not sure why. <(striked-out) see updated answer soln2 below> When I am using a normal Java Project instead of a Dynamic Web Project in Eclipse, I only need to add the mysql-connector-java-8.0.28.jar to Java Build Path directly, then I can connect to the JDBC with no problem. However, if I am using Dynamic Web Project (which is in this case), those 2 strict rules applies (jar position & class loading). <see TOMCAT ON ECLIPSE java.sql.SQLException: No suitable driver found for jdbc:mysql > Soln2 (Updated Ans) //-20220305_12 In short: 1 . put mysql-connector-java-8.0.28.jar file in the <where you install your Tomcat>/lib. eg: G:\pla\Java\apache-tomcat-10.0.16\lib\mysql-connector-java-8.0.28.jar (and for an Eclipse Dynamic Web Project, the jar will then be automatically put inside in your project's Java build path > Server Runtime [Apache Tomcat v10.0].) Additional notes:: for soln1:: put mysql-connector-java-8.0.28.jar file in the WEB-INF/lib. use Class.forName("com.mysql.cj.jdbc.Driver"); in your Servlet Java code. this will create an WARNING: WARNING: The web application [LearnJDBC] appears to have started a thread named [mysql-cj-abandoned-connection-cleanup] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: <see The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread > and that answer led me to soln2. for soln2:: put mysql-connector-java-8.0.28.jar file in the <where you install your Tomcat>/lib. this will create an INFO: INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time. you can just ignore it. <see How to fix "JARs that were scanned but no TLDs were found in them " in Tomcat 9.0.0M10 > (you should now understand what Murach’s Java Servlets and JSP was talking about: the jar in Tomcat/lib & the no need for Class.forName("com.mysql.cj.jdbc.Driver");) to kinda fix it //-20220307_23 Tomcat 8.5. Inside catalina.properties, located in the /conf directory set: tomcat.util.scan.StandardJarScanFilter.jarsToSkip=\*.jar How to fix JSP compiler warning: one JAR was scanned for TLDs yet contained no TLDs?
It might be worth noting that this can also occur when Windows blocks downloads that it considers to be unsafe. This can be addressed by right-clicking the jar file (such as ojdbc7.jar), and checking the 'Unblock' box at the bottom. Windows JAR File Properties Dialog:
As well as adding the MySQL JDBC connector ensure the context.xml (if not unpacked in the Tomcat webapps folder) with your DB connection definitions are included within Tomcats conf directory.
A very silly mistake which could be possible resulting is adding of space at the start of the JDBC URL connection. What I mean is:- suppose u have bymistake given the jdbc url like String jdbcUrl=" jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC"; (Notice there is a space in the staring of the url, this will make the error) the correct way should be: String jdbcUrl="jdbc:mysql://localhost:3306/web_customer_tracker?useSSL=false&serverTimeZone=UTC"; (Notice no space in the staring, you may give space at the end of the url but it is safe not to)
Run java with CLASSPATH environmental variable pointing to driver's JAR file, e.g. CLASSPATH='.:drivers/mssql-jdbc-6.2.1.jre8.jar' java ConnectURL Where drivers/mssql-jdbc-6.2.1.jre8.jar is the path to driver file (e.g. JDBC for for SQL Server). The ConnectURL is the sample app from that driver (samples/connections/ConnectURL.java), compiled via javac ConnectURL.java.
I was using jruby, in my case I created under config/initializers postgres_driver.rb $CLASSPATH << '~/.rbenv/versions/jruby-1.7.17/lib/ruby/gems/shared/gems/jdbc-postgres-9.4.1200/lib/postgresql-9.4-1200.jdbc4.jar' or wherever your driver is, and that's it !
I had this exact issue when developing a Spring Boot application in STS, but ultimately deploying the packaged war to WebSphere(v.9). Based on previous answers my situation was unique. ojdbc8.jar was in my WEB-INF/lib folder with Parent Last class loading set, but always it says it failed to find the suitable driver. My ultimate issue was that I was using the incorrect DataSource class because I was just following along with online tutorials/examples. Found the hint thanks to David Dai comment on his own question here: Spring JDBC Could not load JDBC driver class [oracle.jdbc.driver.OracleDriver] Also later found spring guru example with Oracle specific driver: https://springframework.guru/configuring-spring-boot-for-oracle/ Example that throws error using org.springframework.jdbc.datasource.DriverManagerDataSource based on generic examples. #Config #EnableTransactionManagement public class appDataConfig { \* Other Bean Defs *\ #Bean public DataSource dataSource() { // configure and return the necessary JDBC DataSource DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:oracle:thin:#//HOST:PORT/SID", "user", "password"); dataSource.setSchema("MY_SCHEMA"); return dataSource; } } And the corrected exapmle using a oracle.jdbc.pool.OracleDataSource: #Config #EnableTransactionManagement public class appDataConfig { /* Other Bean Defs */ #Bean public DataSource dataSource() { // configure and return the necessary JDBC DataSource OracleDataSource datasource = null; try { datasource = new OracleDataSource(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } datasource.setURL("jdbc:oracle:thin:#//HOST:PORT/SID"); datasource.setUser("user"); datasource.setPassword("password"); return datasource; } }
I was having the same issue with mysql datasource using spring data that would work outside but gave me this error when deployed on tomcat. The error went away when I added the driver jar mysql-connector-java-8.0.16.jar to the jres lib/ext folder However I did not want to do this in production for fear of interfering with other applications. Explicity defining the driver class solved this issue for me spring.datasource.driver-class-name: com.mysql.cj.jdbc.Driver
You will get this same error if there is not a Resource definition provided somewhere for your app -- most likely either in the central context.xml, or individual context file in conf/Catalina/localhost. And if using individual context files, beware that Tomcat freely deletes them anytime you remove/undeploy the corresponding .war file.
For me the same error occurred while connecting to postgres while creating a dataframe from table .It was caused due to,the missing dependency. jdbc dependency was not set .I was using maven for the build ,so added the required dependency to the pom file from maven dependency jdbc dependency
For me adding below dependency to pom.xml file just solved like magic! I had no mysql connector dependency and even adding mssql jdbc jar file to build path did not work either. <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>9.4.0.jre11</version> </dependency>
In my case I was working on a Java project with Maven and encountered this error. In your pom.xml file make sure you have this dependencies <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.11</version> </dependency> </dependencies> and where you create connection have something like this public Connection createConnection() { try { String url = "jdbc:mysql://localhost:3306/yourDatabaseName"; String username = "root"; //your my sql username here String password = "1234"; //your mysql password here Class.forName("com.mysql.cj.jdbc.Driver"); return DriverManager.getConnection(url, username, password); } catch (SQLException | ClassNotFoundException e) { e.printStackTrace(); } return null; }
faced same issue. in my case ':' colon before '//' (jdbc:mysql://localhost:3306/dbname) was missing, and it just fixed the problem. make sure : and // are placed properly.
I ran into the same error. In my case, the JDBC URL was correct, but the issue was with classpath. However, adding MySQL connector's JAR file to the -classpath or -cp (or, in the case of an IDE, as a library) doesn't resolve the issue. So I will have to move the JAR file to the location of Java bytecode and run java -cp :mysql_connector.jar to make this work. If someone runs into the same issue as mine, I'm leaving this here.
I encountered this issue by putting a XML file into the src/main/resources wrongly, I deleted it and then all back to normal.
using JDBC within dynamic web application
I have a dynamic web application using struts2 that works perfectly. I have a JDBC application that works perfectly (all it does is print a table out in the console). After I put the two together all that was left was to add authentication to the run configuration and I'd be done... I've already put the sqljdbc_auth.dll to the WEB-INF/lib I've tried to add this line to Tomcat v7.0 Server at localhost -Djava.library.path="C:\Program Files (x86)\jdbc\sqljdbc_4.0\enu\auth\x86" but it doesn't do anything This driver is not configured for integrated authentication. ClientConnectionId:6495... It's the line that got my JDBC application working just fine. Would there be some syntax error I'm missing or another method for me to pass an argument that I'm not aware of?
It looks like you are using the Microsoft SQL Server Driver which is pure java (type 4) so you dont need to mess with java.library.path. Set the Classpath as described here Alternatively include the jdbc driver in WEB-INF/lib within your WAR file.
drivers loaded in apache-tomcat-6.0.36/lib but still get No suitable driver found
I am learning web services in eclipse helios with axis 2 and apache tomcat. I have two dynamic web projects with java classes that successfully connect to two databases when I run them as simple java classes in eclipse. (I have added the external jars to the external build path for the project). But when I run either on the server, I get an error: No suitable driver found. I know that I need to load the necessary drivers into apache-tomcat-6.0.36/lib and I have done so (and re-started the server). (see No suitable driver found). I use this statement to create the driver in my Java class: Class.forName("org.postgresql.Driver"); //throws class not found exception w/message "com.postgresql.jdbc.Driver" conn = DriverManager.getConnection(url, user, password); Why doesn't apache "see" the .jar drivers in its /lib folder? Some older tutorials say to put the .jars in common/lib--but I don't see that folder in my apache tomcat directory structure. What can I do to debug this problem?
Depending on the version of Java (and thus, the JDBC version) you use, you might need to call Class.forName() before calling DriverManager.getConnection(...). This forces the JVM to load the class, so that JDBC knows the class is a driver for your type of connection. Without that, JDBC knows no driver for your type of database and thus spits out the "No suitable driver found". If you're running your class in Eclipse using Java 7 (and thus JDBC 4.0) the drivers that are found in your class path are automatically loaded. With Java versions before 7 (and thus JDBC before 4.0), you have to register your driver by hand, like explained. See the JDBC tutorial for details.
If this line is uncommented in ${CATALINA_HOME}/conf/server.xml: <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> Change it to: <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false" /> This returns Tomcat to pre-6.0.35 functionality, where it correctly registered the type 4 JDBC drivers in ${CATALINA_HOME}/lib.
Cannot connect to a mysql database with JSP, Glassfish
I have previous JSP experience but with using Tomcat and Resin and I would like to connect to a mySQL database using Glassfish and hoped that more or less copy and pasting the code would work. The code is: try { Class.forName("org.gjt.mm.mysql.Driver"); } catch (Exception E) { System.out.println("First: " + E); } Connection conn = DriverManager.getConnection("jdbc:mysql://xxx.xx.xx.xx:xxxx/DBName", "Username", "Password"); The errors I get when I look into my server logs are [#|2012-03-09T13:50:21.900+0000|INFO|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=67;_ThreadName=Thread-1;|First: java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver|#] [#|2012-03-09T13:50:22.009+0000|INFO|glassfish3.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=67;_ThreadName=Thread-1;|java.sql.SQLException: No suitable driver found for jdbc:mysql://xxx.xx.xx.xx.xx/SmarterStudents|#] I have put the mysql-connector-java-5.0.7-bin.jar into the domain/lib folder and threw it into the WEB-INF/lib folder just to be safe and it still wouldn't work for me. I'm at my wit's end now, I just don't know what to do. D:
You're using the old and deprecated driver class name for the very first releases of the MySQL JDBC driver org.gjt.mm.mysql.Driver when it was still a hobby project, while you're using one of the more recent releases of the MySQL JDBC driver which has the driver class name com.mysql.jdbc.Driver. Fix the classname accordingly. Class.forName("com.mysql.jdbc.Driver"); Make sure that you're reading the official MySQL JDBC driver documentation instead of some random and heavily outdated resource/book/tutorial. As to the placement of the JAR file, when you're managing the connections yourself in your web application, the JAR can be placed in both server's own /lib or webapp's /WEB-INF/lib. But when you're letting the server manage the connections (which will usually use a shared connection pool which is way much faster), then the JAR must be placed in server's own /lib folder. The one in the deployed webapp(s) will be ignored anyway.
Try working with JdbcOdbcDriver , but generally they say it should be used as a last resort. try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:db","root","root"); }catch(Exception e){ e.printStackTrace(); } This should work for MySQL database, you need to install the connector for this as well, and the username and password generally for MySQL is root and root, respectively.