Deepesh Uniyal
Deepesh Uniyal

Reputation: 991

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.

Upvotes: 4

Views: 2642

Answers (1)

Mouad EL Fakir
Mouad EL Fakir

Reputation: 3749

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/

Upvotes: 8

Related Questions