Reputation: 991
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
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!"
Upvotes: 8