Foren

How to connect MySql from Liferay 7 portlet

Denvazi Knoz, geändert vor 6 Jahren.

How to connect MySql from Liferay 7 portlet

New Member Beiträge: 7 Beitrittsdatum: 26.09.17 Neueste Beiträge
Hi,

I'm new to Liferay. I created a Liferay mvc-portlet and trying to connect to MySql database.
I included mysql-connector-java-5.1.38.jar inside Referenced Libraries.
I put below codes to 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);  
        
    }   
%&gt;

But I'm receiving "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver " error.
Can someone please help me suggest which part I missed out?

Regards,
Den
thumbnail
Andrew Jardine, geändert vor 6 Jahren.

RE: How to connect MySql from Liferay 7 portlet

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
Hi Denvazi,

Assuming that your portal is using a MySQL database, you shouldn't need to provide the lib for MySQL database connections, it should already be there and available globally. If you really want to include it though, then I'm not sure you are going about it the right way. When you say Referenced Libraries, are you talking about your bundle definition? or are you talking about in your IDE?

If you are referring to your IDE, that will only make the libraries visible to your editor to allow you to build -- it won't make them available in the runtime. If you are talking about your runtime, then can you provide the contents of your gradle.build and your bnd file for us please.
Denvazi Knoz, geändert vor 6 Jahren.

RE: How to connect MySql from Liferay 7 portlet

New Member Beiträge: 7 Beitrittsdatum: 26.09.17 Neueste Beiträge
Hi Andrew,

Thank you for replying.
My portal is using default hypersonic database. Not MySql.

I included MySql connector (.jar) as Java Build Path inside Eclipse.
Here is my project explorer view and attached is bnd file and gradle.build.

Really appreciate your help.

Thank you,
Den
bnd.bnd and gradle.build
thumbnail
Andrew Jardine, geändert vor 6 Jahren.

RE: How to connect MySql from Liferay 7 portlet (Antwort)

Liferay Legend Beiträge: 2416 Beitrittsdatum: 22.12.10 Neueste Beiträge
You might be using hypersonic, but that doesn't mean that the runtime doesn't already have the jar loaded. By default, the bundle does include the jar with the drivers. That is how you are able to connect the portal to MySQL doing nothing more than a few lines of configuration. Check in the TOMCAT_HOME/lib/ext and you should find a mysql.jar there.

As for your configuration -- the refeerences, I don't think those are going to be added to your module (unless you have something specific in your BND) so while you are able to build because eclipse is able to find the dependencies, the issue looks like the runtime might not. I'm not seeing the attachment. Can you try again?

... maybe have a look at this post from David: https://web.liferay.com/web/user.26526/blog/-/blogs/osgi-module-dependencies
Denvazi Knoz, geändert vor 6 Jahren.

RE: How to connect MySql from Liferay 7 portlet (Antwort)

New Member Beiträge: 7 Beitrittsdatum: 26.09.17 Neueste Beiträge
I did the following steps and finally it's working. Thanks for helping.
1. Put mysql-connector-java-5.1.44-bin.jar to "deploy" folder.
2. Added
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.44'
to build.gradle.
3.
Import-Package: com.mysql.jdbc, *
to bnd.bnd
4. Right Click on the project>Gradle>Refresh Gradle Project.
Anonymous Anonymous, geändert vor 5 Jahren.

RE: How to connect MySql from Liferay 7 portlet

Regular Member Beiträge: 127 Beitrittsdatum: 16.09.19 Neueste Beiträge
Hi Knoz,

Thanks for the suggestion it helped to resolve the issue quickly.
others who facing similar issue can use the below URL to download the mySql connector directly:
mySql connector 5.1.44