Expand my Community achievements bar.

SOLVED

error while connecting database

Avatar

Level 2

[img]sling.PNG[/img],

hi , i tried to connect database, the above code when i use in jsp its working fine . but now it showing error like bunble  and framework cannot be resolved   

BundleContext dspService = FrameworkUtil.getBundle(DataSourcePool.class).getBundleContext(); .

previously instead of this line i used sling,getService();

if that also it shows sling cannot resolved error, and i changed this , now also i am getting error, if you have any idea help me, thanks in advance 

1 Accepted Solution

Avatar

Correct answer by
Level 10

See: 

http://helpx.adobe.com/experience-manager/using/datasourcepool.html

it steps you through successfully using a DataSourcePool by injecting it:

@Reference
   private DataSourcePool source;

In your code - you are not injecting a DataSourcePool.

  

View solution in original post

3 Replies

Avatar

Correct answer by
Level 10

See: 

http://helpx.adobe.com/experience-manager/using/datasourcepool.html

it steps you through successfully using a DataSourcePool by injecting it:

@Reference
   private DataSourcePool source;

In your code - you are not injecting a DataSourcePool.

  

Avatar

Level 2

package com.adobe.test;
import com.day.commons.datasource.poolservice.DataSourcePool;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import javax.servlet.ServletException;
import java.io.IOException;
import org.apache.felix.scr.annotations.Reference;
import java.io.*;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
@SlingServlet(paths={"/bin/mydummyservlet"})
public class servlet extends SlingSafeMethodsServlet{
private static final long serialVersionUID = 1L;
@Reference 
private DataSourcePool dspService;
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {

PrintWriter out = response.getWriter();
out.println(request.getParameter("firstname"));
try{ 
DataSource ds = (DataSource) dspService.getDataSource("mysql");   
     if(ds != null) {
        out.println("Obtained the datasource!");
         final Connection connection = ds.getConnection();
          final Statement statement = connection.createStatement();
          final ResultSet resultSet =statement.executeQuery("SELECT * from cq"); 
         out.println(resultSet);
          int r=0;
          while(resultSet.next()){
             r=r+1;
          } 
          resultSet.close();
          out.println("Number of results:"+r);
      } 
}
catch (Exception e2) 
{
    out.println(e2);
}
}
}

 

hi, i used data source pool , and its the bundled compiled successfully, but still i got error like  

Cannot serve request to /bin/mydummyservlet/ in org.apache.sling.servlets.get.DefaultGetServlet

Avatar

Level 10

The above article that talked about using a DataSourcePool was not a Sling Servlet - it was Java classes exposed as an AEM service:

@Component

@Service
public class CustomerServiceImp implements CustomerService {
 
    protected final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
     
    @Reference
   private DataSourcePool source;
     
   //Returns a connection using the configured DataSourcePool 
   private Connection getConnection()
   {
            DataSource dataSource = null;
            Connection con = null;
            try
            {
                //Inject the DataSourcePool right here! 
                dataSource = (DataSource) source.getDataSource("Customer");
                con = dataSource.getConnection();

You invoked it from a JSP file like this:

<%@include file="/libs/foundation/global.jsp"%>
<%@ page import="org.apache.sling.commons.json.io.*,org.w3c.dom.*" %><%
String filter = request.getParameter("filter");
   
//create a CustomerService instance
com.adobe.cq.sql.CustomerService cs = sling.getService(com.adobe.cq.sql.CustomerService.class);
 
String XML = cs.getCustomerData(filter); 
   
     
//Send the data back to the client
JSONWriter writer = new JSONWriter(response.getWriter());
writer.object();
writer.key("xml");
writer.value(XML);
   
writer.endObject();
%>

 

If you are interested in using a DataSourcePool in a Sling Servlet (as opposed to an AEM Service) and using an AJAX call to invoke the Servlet -- see this AEM community article:

Injecting a DataSourcePool into Adobe Experience Manager Sling Servlets