Skip to content

Instantly share code, notes, and snippets.

@yupadhyay
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yupadhyay/eaf86274c6b49060714a to your computer and use it in GitHub Desktop.
Save yupadhyay/eaf86274c6b49060714a to your computer and use it in GitHub Desktop.
MySQL Connection To CQ
<%@page session="false"%><%
%><%@ page import="com.day.commons.datasource.poolservice.DataSourcePool" %><%
%><%@ page import="javax.sql.DataSource" %><%
%><%@ page import="java.sql.Connection" %><%
%><%@ page import="java.sql.SQLException" %><%
%><%@ page import="java.sql.Statement" %><%
%><%@ page import="java.sql.ResultSet"%><%
%><%@ page import="java.sql.PreparedStatement"%>
%><%@ page import="java.sql.DriverManager"%>
DataSourcePool dspService = sling.getService(DataSourcePool.class);
try {
DataSource ds = (DataSource) dspService.getDataSource("mysql_datasource");
if(ds != null) {
%><p>Obtained the datasource!</p><%
%><%
out.println("Trying to get connection Connection done");
final Connection connection = ds.getConnection();
out.println("Connection done");
final Statement statement = connection.createStatement();
out.println("Create Statement done");
final ResultSet resultSet = statement.executeQuery("some-query'");
int r=0;
while(resultSet.next()){
r=r+1;
}
resultSet.close();
%><p>Number of results: <%=r%></p><%
}
}catch (Exception e) {
%><p>error! <%=e.getMessage()%></p><%
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment