1

My Liferay portal 7 ga4 is running on wildfly 10. I've created a Liferay MVC portlet and deployed. I'm trying to connect to MySql database but getting this error: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I imported mysql-connector-java-5.1.42-bin.jar by (Right Click on project>Build Path>Configure Build Path>Add JARs. It went under "Referenced Libraries".

enter image description here

And here's the code of view.jsp

try{
    String connectionURL = "jdbc:mysql://localhost/employees";

    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;

    Class.forName("com.mysql.jdbc.Driver").newInstance();
    connection = DriverManager.getConnection(connectionURL, "root", "");
    statement = connection.createStatement();

    if(!connection.isClosed()){
        out.println("Successfully connected to MySQL server" + "<br/>");
    }

} catch(Exception ex){    

    out.println("Unable to connect to database: "+ ex);  

}   

Can someone please help me? I'm totally new to Liferay and Java.

Thank you.

Victor
  • 3,520
  • 3
  • 38
  • 58
Den
  • 369
  • 1
  • 4
  • 12
  • 1
    It does not seem to be duplicate, this issue seems to be related to gradle config and OSGi environment. Could you please show your gradle config? – Victor Oct 04 '17 at 23:39

1 Answers1

1

From the information given, you probably missing a dependency in you gradle.build

Add:

// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.42'

Right click you project, and in Gradle, refresh gradle project.

Also, bear in mind that, if you portlet is running under the new structure based on OSGi, your environment will need to have a bundle that offers you the packages used from this Jar. The one that comes with Tomcat will not be taken in to account.

As this version of MySQL is a OSGi bundle, you can drop it into your deploy folder or simply let it in your modules folder.

You can also use other versions if you want to... https://www.e-systems.tech/web/guest/blog/-/blogs/liferay-with-mysql-5-7-driver-changes


There are 3 issues associated to this question

  1. Build environment config
  2. Runtime environment config
  3. Runtime configuration

To fix 1, the mentioned Gradle config will suffice.

To fix 2, make sure MySQL's bundle is installed, you can drop it in your deploy folder or in your osgi/modules folder.

To fix 3, you will need to declare the package level dependency in your bnb.bnd file.

3 is really weird because when you are using bndtools, you normally ask the tool to detect your dependencies with

Import-Package: *

But in this case the dependency is created by a classes being loaded by name, in a string, which forces you to use an explicitly declared dependency

Import-Package: com.mysql.jdbc, *

Victor
  • 3,520
  • 3
  • 38
  • 58
  • Hi Victor, I included the dependency in build.gradle file and refresh gradle project. I can see mysql_connector jar file inside Project and External Dependencies but error is still the same "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver". – Den Oct 05 '17 at 08:53
  • Did the red exclamation mark disappear? You have 2 issues in this question, not just one – Victor Oct 05 '17 at 11:35
  • I am asking as you should now fix your runtime issue: drop the driver with you module into your deploy folder. – Victor Oct 05 '17 at 13:51
  • Yes, red exclamation mark disappeared. I also dropped the driver (.jar) and my module (.jar) into deploy folder. I also deployed mysql connector to WildFly10. – Den Oct 06 '17 at 06:13
  • 1
    perfect! but the jar in your server has nothing to do with this issue. Now, you fixed you build problem, your final step is to fix the runtime. As you dropped both in your deploy folder, your environment should be good to go. I am sorry you got in this tricky example on you first contact with this tech set... As OSGi needs to know the package dependencies, you need to declare it on your bnd.bnd -> Import-Package: * Normally this will detect all your dependencies, but in Mysql case, you are using a string to load a class, so it is not detected automatically. – Victor Oct 06 '17 at 12:37
  • I updated the answer to make it easier to explain. – Victor Oct 06 '17 at 12:49
  • 1
    After adding Import-Package: com.mysql.jdbc, * to bnd.bnd,it finally worked. Thank you so much for helping. – Den Oct 08 '17 at 04:11
  • 1
    great, tricky one! my pleasure! – Victor Oct 08 '17 at 12:33