简体   繁体   English

将Adobe CQ 5连接到外部数据库?

[英]Connecting Adobe CQ 5 to external database?

I know this question has been asked before, but I am facing trouble in my case. 我知道之前曾有人问过这个问题,但是我面临着麻烦。 I know the various steps to connect to external Database, I used this one- Convert JDBC Driver Jar to OSGi Bundle using this link I successfully did the above. 我知道连接到外部数据库的各种步骤,我使用了此功能-使用此链接将JDBC Driver Jar转换为OSGi Bundle,我成功完成了上述操作。 Then In my SlingServlet I tried to load driver but I get CLASS NOT FOUND Exception This is the JDBC Jar See bundle is Active 然后在我的SlingServlet中,我尝试加载驱动程序,但出现CLASS NOT FOUND异常,这是JDBC Jar。 在此处输入图片说明

This is my Servlet code 这是我的Servlet代码

package com.videojet.hiresite.controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;

import javax.servlet.ServletException;

import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.videojet.hiresite.customer.CustomerService;



@SlingServlet(
        paths={"/services/videojet/v1/LoginServlet"}
)
@Properties({
    @Property(name="service.pid", value="com.videojet.hiresite.controllers.LoginServlet",propertyPrivate=false),
    @Property(name="service.description",value="Validates the user", propertyPrivate=false),
    @Property(name="service.vendor",value="Zensar Tech", propertyPrivate=false)
})
public class LoginController extends SlingAllMethodsServlet{
    private static final long serialVersionUID = 1L;
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Reference
    protected CustomerService customerService;


    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {
        log.info("Do Get **************");

    }

    @Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException
    {

        log.info("Do Post **************");
        log.info("Major Man");
        String  username = request.getParameter("username");
        String password = request.getParameter("password");

        log.info("The Username :::::"+username);
        log.info("The Password ::::::"+ password);
        log.info("Very IMportant ++++++++++++++"+request.getParameter("./action"));

        try {
            //customerService.insertData(username, password);
            //customerService.validateUser(username, password); This works fine !!


            Class.forName("org.h2.Driver");

            Connection connection= DriverManager.getConnection("jdbc:h2:file:./samples/database/DATA_SERV_SAMP","wso2ds","wso2ds");
            if (connection==null)
            {
                log.error("Connection Failed ");


            }
            else{

                /*Statement  statement =connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            statement.executeQuery("select * from products where productcode")*/


                log.error("Connected");

            }



        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            log.error("VIDEOJET", e);

        }

        PrintWriter writer =response.getWriter();
        writer.print("Called ");
        writer.close();
    }



}

I am calling this servlet at a form submit(doPost) and here I want to save data in database. 我在表单Submit(doPost)处调用此servlet,在这里我想将数据保存在数据库中。 But the Driver just doesn't load WHY?? 但是驱动程序只是不加载为什么?

This is Error log. 这是错误日志。

24.04.2014 18:36:55.299 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController Do Post **************
24.04.2014 18:36:55.299 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController Major Man
24.04.2014 18:36:55.299 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Username :::::wwwwwwwwwww
24.04.2014 18:36:55.300 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController The Password ::::::w
24.04.2014 18:36:55.300 *INFO* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController Very IMportant ++++++++++++++null
24.04.2014 18:36:55.358 *ERROR* [0:0:0:0:0:0:0:1 [1398344815264] POST /services/videojet/v1/LoginServlet HTTP/1.1] com.videojet.hiresite.controllers.LoginController VIDEOJET java.lang.ClassNotFoundException: org.h2.Driver not found by com.videojet.hiresite.Controllers [235]
    at org.apache.felix.framework.ModuleImpl.findClassOrResourceByDelegation(ModuleImpl.java:787)
    at org.apache.felix.framework.ModuleImpl.access$400(ModuleImpl.java:71)
    at org.apache.felix.framework.ModuleImpl$ModuleClassLoader.loadClass(ModuleImpl.java:1768)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at com.videojet.hiresite.controllers.LoginController.doPost(com.videojet.hiresite.controllers.LoginController.java:65)
    at org.apache.sling.api.servlets.SlingAllMethodsServlet.mayService(SlingAllMethodsServlet.java:148)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
    at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
    at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:500)
    at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:45)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at com.day.cq.wcm.core.impl.WCMDebugFilter.doFilter(WCMDebugFilter.java:147)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.core.impl.WCMComponentFilter.filterRootInclude(WCMComponentFilter.java:308)
    at com.day.cq.wcm.core.impl.WCMComponentFilter.doFilter(WCMComponentFilter.java:141)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processComponent(SlingRequestProcessorImpl.java:257)
    at org.apache.sling.engine.impl.filter.RequestSlingFilterChain.render(RequestSlingFilterChain.java:49)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:64)
    at com.day.cq.wcm.core.impl.warp.TimeWarpFilter.doFilter(TimeWarpFilter.java:106)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter.doFilter(RedirectFilter.java:296)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter.doFilter(RequestProgressTrackerLogFilter.java:59)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet.doFilter(FormsHandlingServlet.java:220)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.theme.impl.ThemeResolverFilter.doFilter(ThemeResolverFilter.java:76)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at com.day.cq.wcm.core.impl.WCMRequestFilter.doFilter(WCMRequestFilter.java:89)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.rewriter.impl.RewriterFilter.doFilter(RewriterFilter.java:83)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter.doFilter(BackgroundServletStarterFilter.java:135)
    at org.apache.sling.engine.impl.filter.AbstractSlingFilterChain.doFilter(AbstractSlingFilterChain.java:60)
    at org.apache.sling.engine.impl.SlingRequestProcessorImpl.processRequest(SlingRequestProcessorImpl.java:153)
    at org.apache.sling.engine.impl.SlingMainServlet.service(SlingMainServlet.java:206)
    at org.apache.felix.http.base.internal.handler.ServletHandler.doHandle(ServletHandler.java:96)
    at org.apache.felix.http.base.internal.handler.ServletHandler.handle(ServletHandler.java:79)
    at org.apache.felix.http.base.internal.dispatch.ServletPipeline.handle(ServletPipeline.java:42)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:49)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.sling.i18n.impl.I18NFilter.doFilter(I18NFilter.java:117)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at com.adobe.granite.license.impl.LicenseCheckFilter.doFilter(LicenseCheckFilter.java:179)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.sling.security.impl.ReferrerFilter.doFilter(ReferrerFilter.java:238)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.sling.engine.impl.log.RequestLoggerFilter.doFilter(RequestLoggerFilter.java:75)
    at org.apache.felix.http.base.internal.handler.FilterHandler.doHandle(FilterHandler.java:88)
    at org.apache.felix.http.base.internal.handler.FilterHandler.handle(FilterHandler.java:76)
    at org.apache.felix.http.base.internal.dispatch.InvocationFilterChain.doFilter(InvocationFilterChain.java:47)
    at org.apache.felix.http.base.internal.dispatch.HttpFilterChain.doFilter(HttpFilterChain.java:33)
    at org.apache.felix.http.base.internal.dispatch.FilterPipeline.dispatch(FilterPipeline.java:48)
    at org.apache.felix.http.base.internal.dispatch.Dispatcher.dispatch(Dispatcher.java:39)
    at org.apache.felix.http.base.internal.DispatcherServlet.service(DispatcherServlet.java:67)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
    at com.day.j2ee.servletengine.ServletRuntimeEnvironment.service(ServletRuntimeEnvironment.java:250)
    at com.day.j2ee.servletengine.RequestDispatcherImpl.doFilter(RequestDispatcherImpl.java:315)
    at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:334)
    at com.day.j2ee.servletengine.RequestDispatcherImpl.service(RequestDispatcherImpl.java:377)
    at com.day.j2ee.servletengine.ServletHandlerImpl.process(ServletHandlerImpl.java:351)
    at com.day.j2ee.servletengine.HttpListener$Worker.run(HttpListener.java:625)
    at java.lang.Thread.run(Unknown Source)

You should never use this in OSGi: 您绝对不应在OSGi中使用此功能:

Class.forName("org.h2.Driver");

It tries to load the class from your bundles classloader, but it is not there! 它尝试从您的捆绑包类加载器中加载类,但它不存在!

First as I see h2database's jar is an OSGi bundle: MANIGEST.MF 首先,我看到h2database的jar是OSGi捆绑包: MANIGEST.MF

Also see the service: registering DataSourceFactory 另请参见服务: 注册DataSourceFactory

So you can install the jar into OSGi and you can use the DataSourceFactory to create connections, which will solve your classloader issues. 因此,您可以将jar安装到OSGi中,并且可以使用DataSourceFactory创建连接,这将解决类加载器问题。

Here is an other example how to solve classloader issue: osgi-jdbc-h2 这是另一个解决类加载器问题的示例: osgi-jdbc-h2

** UPDATE ** **更新**

More info about Class.forName: nice article If I were you I would also check this: Day.com article about Connecting to SQL Databases 有关Class.forName的更多信息: 不错的文章如果我是我,还应该查看以下内容: 关于连接到SQL数据库的Day.com文章

It is a classic problem and well discribed in enterprise OSGi specification. 这是一个经典问题,在企业OSGi规范中有很好的描述。 see: Page 375, Chapter: JDBC™ Service Specification 请参见: 第375页,章节:JDBC™服务规范

Some keywords: jdbc, OSGi, class.forName, dataservicefactory 一些关键字:jdbc,OSGi,class.forName,dataservicefactory

I resolved the Issue myself. 我自己解决了问题。 I used an older jar OJDBC14 instead of OJDBC6. 我使用了较旧的jar OJDBC14而不是OJDBC6。 And I found out that when these Jars are converted to OSGi bundel and loaded in Felix they are dependend on some other jars, Example rt.jar that comes with JRE. 我发现,当这些Jar转换为OSGi Bundel并加载到Felix中时,它们依赖于其他一些jar,例如JRE附带的Example rt.jar。 this jar need to be converted to OSgi bundle and deplyoed with other dependent jars. 该jar需要转换为OSgi捆绑软件,并与其他相关的jar一起删除。

And the Imported packages Manifest of OJDBC mus have this. 而OJDBC的清单导入清单具有此功能。

Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: javax.security.auth.callback,
 javax.crypto.spec,
 javax.naming.directory,
 javax.sql.rowset.spi,
 javax.transaction.xa,
 javax.security.auth.kerberos,
 javax.security.cert,
 org.ietf.jgss,
 org.xml.sax,
 javax.xml.transform,
 javax.xml.transform.stax,
 javax.net,
 javax.management,
 org.xml.sax.helpers,
 javax.security.auth,
 javax.xml.transform.sax,
 javax.xml.stream,
 org.w3c.dom,
 javax.net.ssl,
 javax.naming.spi,
 javax.sql,
 javax.crypto,
 javax.xml.transform.dom,
 javax.naming,
 javax.xml.transform.stream,
 javax.xml.parsers,
 javax.sql.rowset

IT alsor requires some Apache Lucene jars ... Weired but worked fine !! IT还需要一些Apache Lucene罐子……很奇怪,但是效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM