No Suitable Driver Found Exception: Hibernate [duplicate] - java

This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 5 years ago.
i am working on jax-rs(jersey) and want to connect to database by using hibernate. my projects works perfectly fine when i debug it as java application but as i run my project on server it gives No suitable driver found for jdbc:mysql://localhost:3306/sample
here is my hibernate.cfg.xml file
the sql exception is here
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sample
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:208)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:301)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)

You have to download the MySQL Connector/J and add it to your class path
If you are using maven you can use this dependency :
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>

Related

Not able to connect spring jpa with Oracle

Hi I am using below configuration still I am getting error.
spring.datasource.url=jdbc:oracle:thin#sca00tof.us.dell.com:1521:mfg1229
spring.datasource.username=apps
spring.datasource.password=xxxx
spring.datasource.driver-class=oracle.jdbc.driver.OracleDriver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=Oracle12cDialect
below dependency i used in pom.xml
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
</dependency>
still I am getting below error.
Caused by: java.lang.ClassNotFoundException: Could not load requested class : Oracle12cDialect
at org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Clas?s.forName(Class.java:427) ~[na:na]
enter code here
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:130) ~[hibernate-core-5.4.23.Final.jar:5.4.23.Final]
... 28 common frames omitted
WHat is the problem in my configuration. instead of driver-class i also checked with driver-class-name
I checked OracleDialect, Oracle10gDialect and Oracle12cDialect . for all the 3 i am getting same error.
there is nothing like "Oracle12cDialect" dialect. you need to write in proper way. :--
in your property file it is:
spring.jpa.properties.hibernate.dialect=Oracle12cDialect
spring.datasource.driver-class=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin#sca00tof.us.dell.com:1521:mfg1229
but it should be:--
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin#//sca00tof.us.dell.com:1521/mfg1229
if does not work then please change to:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
The dialect for Oracle12c can be use with hibernate 5.x.
You should add this to your classpath:
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.24.Final</version>
</dependency>
Please note that you need the dialect for the coreesponding Version of Oracle DB.
How ever I can't see the exact framework you are using. Please check that the obove dependency is part of your classpath. This may come with other dependencies such as JPA oder spring-data.
Also please note that your jdbc string seems to be wrong.
It should look like this:
url="jdbc:oracle:thin:#sca00tof.us.dell.com:1521:mfg1229"
See here for details: URL string format for connecting to Oracle database with JDBC

How to setup Spring JDBC Connection Pooling with Spring Boot?

Am using Spring Boot 1.5.4 with Spring JDBC.
Have a Spring Boot Microservice which uses Spring JDBC has the following issue when trying to conduct an HTTP PUT (after a bunch of users try conducting an HTTP PUT) which trickles to this Spring JDBC call:
2018-10-10 19:40:02 [http-nio-8081-exec-4] ERROR c.v.r.RepositoryImpl - Problem in updateData() method:
"org.springframework.dao.DataAccessResourceFailureException: PreparedStatementCallback; SQL [select a.user_id,b.user_id, from user a join user_profile b where a.user_id=b.user_id and a.date=?;]; No operations allowed after connection closed.; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:79)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: No operations allowed after connection closed.
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
The last packet successfully received from the server was 28,915,589 milliseconds ago. The last packet sent successfully to the server was 9 milliseconds ago.
at com.myapp.repository.RepositoryImpl.updateData(RepositoryImpl.java:74)
at com.myapp.repository.RepositoryImpl$$FastClassBySpringCGLIB$$1be9dd8e.invoke(<generated>)
... 52 common frames omitted
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2914)
at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3337)
... 83 common frames omitted
pom.xml:
<artifactId>MyService</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
</dependencies>
Am guessing that I need to setup a JDBC connection pool...
Inside my application.properties settings, (I have two different databases - one local and one remote, its losing connection with the remote database; database2):
# Local
spring.datasource.database1.url=jdbc:mysql://localhost/database1?zeroDateTimeBehavior=convertToNull
spring.datasource.database1.username=root
spring.datasource.database1.password=ret2my
spring.datasource.database1.driverClassName=com.mysql.jdbc.Driver
# Remote
spring.datasource.database2.url=jdbc:mysql://read-replica-database-production.cranmichpmc.us-west-2.rds.amazonaws.com/database2?zeroDateTimeBehavior=convertToNull
spring.datasource.database2.username=root
spring.datasource.database2.password=ret2a$$
spring.datasource.database2.driverClassName=com.mysql.jdbc.Driver
Should I add this for the second database:
spring.datasource.database2.hikari.maximum-pool-size=10
spring.datasource.database2.hikari.connection-timeout=60000
Are there other useful params that I should consider?
spring.datasource.url=jdbc:mysql://10.168.143.140:3306/database_name
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.hikari.maximum-pool-size=4
more detailed information can be found at :
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

Driver for SQL server 2012 in Java

So I have a java program and am using the following driver
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
to try to read data from a SQL server 2012 database, but I get the
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost....
If I have a SQL server 2012 database, can I not use the com.microsoft.sqlserver.jdbc.SQLServerDriver driver? Any help will be greatly appreciated.
You might be missing the driver class and you should add that to your project.
Just a quick search on the Maven Central produces this:
<!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.4.0.jre8</version>
<scope>test</scope>
</dependency>
Assuming that you run Java 8 and Maven, you can add this as a dependency to your project.

Selenium Chrome Driver: NoClassDefFoundError: com/google/common/collect/Lists Exception

I have the following code:
public static final String _DRIVER_PATH = "c:\\Users\\Public\\Downloads\\chromedriver.exe";
.....
System.setProperty("webdriver.chrome.driver", Constants._DRIVER_PATH);
ChromeOptions options = new ChromeOptions();
My dependencies are:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.4.0</version>
</dependency>
</dependencies>
Then I get on the last line:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Lists
at org.openqa.selenium.chrome.ChromeOptions.<init>(ChromeOptions.java:74)
at com.FlashMain.main(FlashMain.java:39)
Caused by: java.lang.ClassNotFoundException: com.google.common.collect.Lists
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
I run on Windows 10, ChromeDriver.exe version 2.29 and Chrome - 58.0.3029.110
You advise is appreciated.
It seems that you're using both selenium and other library that brings google common library with version that is not compatible with latest selenium.
Build dependency tree using:
mvn dependency:tree
Exclude old version by managing exclusions and add new one directly or simply try to update library that refers to old version of google common.
I encountered this recently using Selenium with Spring Boot, and the webdrivermanager maven plugin. The problem was that I used the latest webdrivermanager version, but relied on the selenium-java version provided by Spring Boot, which was a few versions back. If you find yourself in a similar scenario, the solution is quite easy, override the spring boot version for your Selenium dependency and make sure you are using consistent and appropriate versions!
Use Selenium version 2.x , Selenium 3.0 chromium implementation is different

PostgreSQL + Openshift: Possibly the wrong driver for the given database URL?

I am trying to run the liquibase update from Openshift machine that I have. It looks like
> java -jar ~/.m2/repository/org/liquibase/liquibase-core/3.1.1/liquibase-core-3.1.1.jar --driver=org.postgresql.Driver --classpath=wildfly/standalone/deployments/ROOT.war --changeLogFile=liquibase/changelog.xml --url="jdbc:$OPENSHIFT_POSTGRESQL_DB_URL/mydb" --username=$OPENSHIFT_POSTGRESQL_DB_USERNAME --password=OPENSHIFT_POSTGRESQL_DB_PASSWORD update
and I see the following error
Liquibase update Failed: liquibase.exception.DatabaseException: liquibase.exception.DatabaseException: Connection could not be created to jdbc:postgresql://admin:29asDVjpFxYl6#127.8.201.2:5432/mydb with driver org.postgresql.Driver. Possibly the wrong driver for the given database URL
What is going wrong here?
I am using the following postgreSQL dependency
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
there were 2 issues that were fixed as following
a.) using postgres 9.2 driver
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1003-jdbc4</version>
</dependency>
b.) using the following url
--url="jdbc:postgresql://$OPENSHIFT_POSTGRESQL_DB_HOST:$OPENSHIFT_POSTGRESQL_DB_PORT/mydb"

Categories

Resources