We can directly use the Liferay Database connection while writing our own portlets. By following the below procedure.
step 1- import com.liferay.portal.kernel.util.InfrastructureUtil;
which will available under portlet-kernal jar file.
Step 2- import javax.sql.DataSource;
liferay uses DataSource mechanism for maintaining the database connections.
step 3- Connection connection = null;
DataSource dataSource = null;
create above variable for getting and storing datasource and connection to the database.
step 4- dataSource = InfrastructureUtil.getDataSource();
connection = dataSource.getConnection();
This is it here we are having the liferay datasource and from that datasource object we
will get the connection object.
Note: In general way it will raises sql exceptions so dont forget to handle the exceptions. ;)
No comments:
Post a Comment