Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hybris Database Connection

For some reason we need to run database native query instead of flexible query. For running those queries we need DB connection so how can we get the jdbcTemplate or DataSource object from Hybris.

like image 992
Deepesh Uniyal Avatar asked Feb 05 '23 02:02

Deepesh Uniyal


1 Answers

This is an example of a script groovy that can achieve this :

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import de.hybris.platform.util.Utilities;
import de.hybris.platform.core.Registry;

Connection conn = null;
PreparedStatement pstmt = null;

try
{
    conn = Registry.getCurrentTenant().getDataSource().getConnection();

    pstmt = conn.prepareStatement("your sql query here...");

    pstmt.execute();

}
catch (final SQLException e)
{
    LOG.error("Error!!");
}
finally
{
    Utilities.tryToCloseJDBC(conn, pstmt, null);
}

return "Groovy Rocks!"


Edit : find more details in this article https://www.stackextend.com/hybris/run-native-sql-query-hybris/
like image 66
Mouad EL Fakir Avatar answered May 17 '23 10:05

Mouad EL Fakir